PREV NEXT
What are Structures?
- In C++, structure is a combination or group of different data elements that are grouped together under a single name.
- The data elements are known as data members who have different data type and length.
- It is a user defined data type that allows you to combine data items of different kinds.
- The structures are used to define records like of we want to display employee records or student record.
Suppose if we want to keep the record of an employee than the attributes we need are:
- Employee Name
- Employee Code
- Designation
- Salary
How to define a structure?
- To define the structure you need to use the struct statement. The struct statement defines different data type for different data members.
Let us have a look at the syntax that how the structure is declared:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
struct type_name{ data type data_member1; data type data_member2; . . . data type data_memberN; }object name; |
For example:
1 2 3 4 5 6 7 8 9 10 11 |
struct Employee{ char Name[10]; char Designation[10]; int Code; int Salary; }; |
In the above example, we have defined the data members with the size given in array.
How to access the data members of structure?
- You can access any data member of a structure by using the member access operator (.).
- The member access operator is valid between the structure name and the structure member that are to be accessed. And to define variables you will use struct keyword.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <cstring> using namespace std; struct Employee { char Name[10]; char Designation[50]; int Code; int Salary; }; int main() { struct Employee Emp1, Emp2; // Declaring variable Emp1, Emp2 of type Employee |
// employee 1 details
strcpy( Emp1.Name, “ABC”);
strcpy( Emp1.Designation, “Manager”);
Emp1.Code = 6495;
Emp1.Salary = 40000;
// employee 2 details
strcpy(Emp2.Name, “XYZ”);
strcpy(Emp2.Name, “Analyst”);
Emp2.Code = 6467;
Emp2.Salary = 20000;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Printing information of employee1 cout << "Employee 1 Name : " << Emp1.Name <<endl; cout << "Employee 1 Designation : " << Emp1.Designation <<endl; cout << "Employee 1 Code : " << Emp1.Code <<endl; cout << "Employee 1 Salary : " << Emp1.Salary <<endl; // Printing information of employee2 cout << " Employee 2 Name: " << Emp2.Name <<endl; cout << " Employee 2 Designation: " << Emp2.Designation <<endl; cout << " Employee 2 Code: " << Emp2.Code <<endl; cout << " Employee 2 Salary: " << Emp2.Salary <<endl; return 0; } |
Advantages of Structures
The advantages of structures in C++ are:
- The structure data members are public by default.
- It is backwardly-compatible with C code.
What are Nested Structures?
- Nested Structures are those where you declare structure within structure. A structure includes another structure like its member.
For example,
- you have two structures Subjects and Student. To make Subjects nested to Student, you have to declare Subjects structure before and outside Student structure and create an object of Subjects structure inside Student structure.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include<iostream> using namespace std; struct Subjects { char Name[25]="C++"; char Author[25]="XYZ"; int Code=9000; }; struct Student { int Id= 25; char Name[25]="ABC"; struct Subjects Sub; }; int main() { int i; Student S; } cout << "\nDetails of Student"; cout << "\n\tStudent Id : " << S.Id; cout << "\n\tStudent Name : " << S.Name; cout << "\n\tStudent Subject Name : " << S.Sub.Name; cout << "\n\tStudent Subject Author : " << S.Sub.Author; cout << "\n\tStudent Subject Code : " << S.Sub.Code; } |
Array of Structures
- As studied above that structure is a collection of different data type and an object of structure represents a single record in memory.
- So, if you want to add more records of structure type than you have to create an array of structure or object. As you know the array is a collection of similar data type therefore, array can be of structure type.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include<iostream> using namespace std; struct Student { int Id; char Name[25]; int Age; }; void main() { int i; Student S[ 5 ]; //Statement 1 for(i=0;i<5;i++) { cout << "\nEnter details of " << i+1 << " Student"; cout << "\n\tEnter Student Id : "; cin >> S[i].Id; cout << "\n\tEnter Student Name : "; cin >> S[i].Name; cout << "\n\tEnter Student Age : "; cin >> S[i].Age; } cout << "\nPrint details of Student"; for(i=0;i<5;i++) cout << "\n"<< S[i].Id <<"\t"<<S[i].Name <<"\t" << S[i].Age; } |
Pointers to Structures
As we have pointers to int, char, float and other data types, in the same way you have the pointers pointing to structures. And these pointers are known as structure pointers.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include <iostream> #include <cstring> using namespace std; void printdetails(struct Employee *emp ); struct Employee { char Name[10]; char Designation[50]; int Code; int Salary; }; int main() { struct Employee Emp1; // Declaring variable Emp1, Emp2 of type Employee struct Employee Emp2; // employee 1 details strcpy( Emp1.Name, "ABC"); strcpy( Emp1.Designation, "Manager"); Emp1.Code = 6495; Emp1.Salary = 40000; // employee 2 details strcpy(Emp2.Name, "CDE"); strcpy(Emp2.Designation, "Analyst"); Emp2.Code = 6467; Emp2.Salary = 20000; // Printing information of employee1 printdetails(&Emp1); // Printing information of employee2 printdetails(&Emp2); return 0; } void printdetails(struct Employee *emp) { cout<< “Employee Name:” <<emp->Name <<endl; cout<< “Employee Designation:”<<emp->Designation <<endl; cout<< “Employee Code:”<<emp->Code <<endl; cout<< “Employee Salary:”<<emp->Salary <<endl; } |