Structures in C++
A structure is a set of diverse types of data
- that may have different lengths
- grouped together under a unique declaration
struct model_name { //Name of the structure type
type1 element1;
type2 element2;
type3 element3;
. .
} object_name; //(Optional) variable of type model_name
//declaring x to be of data type Student
Student x;
rollno 4 bytes
Semester 4 bytes
name 30 bytes
….
Student x;
x.rollno =10;
cout<<x.rollno;
gets(x.name);
Arrays of Structures
student students_list[100];
cin >> students_list[2].rollno;
cout << students_list[2].rollno;
Student Example
const int SIZE=10; //Observe the usage of const
struct student
{
int rollno;
int semester;
char name[30];
} StudentArray[SIZE]; //Optional
Student: using functions
void init_students(), display_students();
Void main ()
{
init_students();
display_students();
}
void init_students()
{
For (int i=0;i<SIZE;i++)
{
StudentArray[i].rollno=0;
StudentArray[i].semester=0;
StudentArray[i].name[0]= ‘\0’;
}
}
void display_students()
{
for(int i=0;i<SIZE;i++)
{
cout<< StudentArray[i].rollno;
cout<<StudentArray[i].semester;
cout<<StudentArray[i].name;
}
}
Passing Structures to functions By value
// define a structure type
struct sample {
int a
} ;
void f1(sample p);
int main()
{
sample arg; // declare arg
arg.a = 1000;
f1(arg);
return 0;
}
void f1(sample p)
{
cout << p.a;
}
Passing Structures to functions by reference
// define a structure type
struct sample {
int a
} ;
void f1(sample &p);
int main()
{
sample arg; // declare arg
arg.a = 1000;
f1(arg);
cout<<arg.a; //value of a?
return 0;
}
void f1(sample &p)
{
p.a=10;
}
Structure: Member Functions
- Function input_student works with a structure variable
- Why not include it within the structure?
struct student
{
int rollno;
int semester;
char name[30];
void input_student()
{
cin>>rollno;
cin>>semester;
gets(name);
}
} ;
student student_variable;
student_variable.input_student(); // access the member function
Assigning Structures
struct stype {
int a, b;
};
int main()
{
stype svar1, svar2;
svar1.a = svar1.b = 10;
svar2.a = svar2.b = 20;
cout << “Structures before assignment.\n”;
cout << “svar1: ” << svar1.a << ‘ ‘ << svar1.b;
cout << ‘\n’;
cout << “svar2: ” << svar2.a << ‘ ‘ << svar2.b;
cout << “\n\n”;
svar2 = svar1; // assign structures
cout << “Structures after assignment.\n”;
cout << “svar1: ” << svar1.a << ‘ ‘ << svar1.b;
cout << ‘\n’;
cout << “svar2: ” << svar2.a << ‘ ‘ << svar2.b;
return 0;
}
Returning structure
// define a structure type
struct sample {
int a
} sample f1() repeat
{
sample x;
cin>>x.a;
return x
}
int main()
{
sample obj;
obj=f1();
cout<<arg.a;
return 0;
}
Example: Performing operations on structure
struct Distance
{
int feet;
float inches;
};
- Distance add(Distance, Distance);
- void disp(Distance);
Addition of two structure variables?
- We need to write a function to perform the addition
- + symbol is not enough
Example: Adding two structure variables
int main()
{
Distance d1, d2, d3; //define three lengths
//get length d1 from user
cout << “\nEnter feet: “; cin >> d1.feet;
cout << “Enter inches: “; cin >> d1.inches;
//get length d2 from user
cout << “\nEnter feet: “; cin >> d2.feet;
cout << “Enter inches: “; cin >> d2.inches;
d3 = add(d1, d2); //d3 is sum of d1 and d2
Example: Display a structure variable
cout << endl;
disp(d1); cout << “ + “; //display all lengths
disp(d2); cout << “ = “;
disp(d3); cout << endl;
return 0;
}
Example: function for Adding two structure variables
// adds two structures of type Distance, returns sum
Distance add( Distance dd1, Distance dd2 )
{
Distance dd3; //define a new structure for sum
dd3.inches = dd1.inches + dd2.inches; //add the inches
dd3.feet = 0; //(for possible carry)
if(dd3.inches >= 12.0) //if inches >= 12.0,
{ //then decrease inches
dd3.inches -= 12.0; //by 12.0 and
dd3.feet++; //increase feet
} //by 1
dd3.feet += dd1.feet + dd2.feet; //add the feet
return dd3; //return structure
}
Example: function to display structure variable
// display structure of type Distance in feet and inches
void disp( Distance dd )
{
cout << dd.feet << “\’-” << dd.inches << “\””;
}
Example: Program output
Enter feet: 4
Enter inches: 5.5
Enter feet: 5
Enter inches: 6.5
4’-5.5” + 5’-6.5” = 10’-0”