Basics
Structures - Struct
Structs (Structure)
C++ struct, short for C++ Structure, is an user-
defined data type available in C++.
It allows a user to combine data items of
(possibly) different data types under a single
name.
Struct - Declaration
struct structure_name
{
//data_type variable 1
//data_type variable 2
//data_type variable 3
... struct Employee
}; {
char name[50];
int age;
float salary;
};
Struct - Initialization
struct Employee
{
char name[50]; What is employee1 here ?
int age;
float salary; employee1 is an instance of Employee out of
}; many
How to access the values in employee1 ???
int main() {
struct Employee employee1 = {“Yasir”, 32, 75000};
return 0;
}
#include <iostream>
using namespace std;
Try This …
struct Employee
{
char name[50];
int age;
float salary;
};
int main() {
struct Employee employee1 = {"Yasir", 32, 75000};
cout << [Link] <<endl;
cout << [Link] <<endl;
cout << [Link]<<endl;
return 0;
}
Use of DOT operator
1. #include <iostream>
2. using namespace std;
3.
4.
struct Person{
char name[50];
Structure Example
5. int strength;
6. float average;
7. };
8. int main(){
9. Person p1;
10. cout << "Enter Course Name: ";
11. [Link]([Link], 50);
12. cout << "Enter class Strength: ";
13. cin >> [Link];
14. cout << "Enter Average: ";
15. cin >> [Link];
16.
17. cout << "\nDisplaying Information." << endl;
18. cout << "Name: " << [Link] << endl;
19. cout <<"Strength: " << [Link] << endl;
20. cout << "Average: " << [Link];
21. return 0;
22. }
6
1. #include<iostream>
2. using namespace std;
3. struct Person {
4.
5.
string name;
int strength;
Structure and pointer
6. float average;};
7. int main(){
8. Person p1,p2; Dot operator cannot be used with pointers
9. Person* psn1;
10. Person* psn2; Pointers use -> (arrow) operator
11. psn1=&p1;
12. psn2=&p2;
13. psn1->name="CS-200";
14. psn1->strength= 120;
15. psn1->average=78.5;
16. psn2->name="DSA";
17. psn2->strength= 150;
18. psn2->average=80.8;
19. cout<<"\nName of first course is: "<<psn1->name;
20. cout<<"\nThe course strength is: "<<psn1->strength;
21. cout<<"\nThe course average is: "<<psn1->average;
22. cout<<"\nName of second course is: "<<psn2->name;
23. cout<<"\nThe course strength is: "<<psn2->strength;
24. cout<<"\nThe course average is: "<<psn2->average;
25. return 0;} 7
1. #include<iostream>
2. using namespace std;
Structure as Function
3. struct module{ Argument
4. string name;
5. int average;};
6. //declaring function. Structure object is the parameter
7. void display(struct module m){
8. cout<<"\n Name of module: "<< [Link];
9. cout<<"\n Average of module: "<< [Link];}
10. int main(){
11. module m1;
12. [Link] = 85;
13. [Link] = "CS - 200";
14. //calling the function using structure object
15. display(m1);
16. return 0;
17. }
8
• A nested structure means, structure inside a structure, same
as nested if-else statements
• For example:
1. struct structure1
Nested Structures
2. {
3. Member1 of structure 1;
4. Member2 of structure 1;
5. }; How to access members of
structure 1 through
Structure 2 ?
6. struct structure2
7. { First access the member of
8. Member1 of structure 2; structure2 and then access
9. Member2 of structure 2; members of structure1
10. struct structure1 obj;
11. }; Hence two dots will be used.
9
1. #include<iostream>
2. using namespace std; What will be the output of the following code?
Is there any errors in the code?
3. struct assignments{
4. int average , highest , lowest;};
5. struct module{
6. string name , id;
7. struct assignments a; }; //declaring structure salary as a nested structure
8. void display(struct module M1){
9. cout<<"\nName: "<< [Link];
10. cout<<"\nID: "<<[Link];
11. cout<<"\nAverage Marks: "<<[Link] ;
12. cout<<"\nHighest Marks: "<< [Link] ;
13. cout<<"\nLowest Marks: "<< [Link] ; }
14. int main() {
15. module M;
16. [Link]="Introduction to Programming"; //adding values to the member of Employee structure
[Link]= "CS-200";
17. [Link] =80.57; //Adding values to the member of salary structure [Link] =95;
18. [Link] =69;
19. display(M); //calling display function
20. return 0; 10
21. }
Functions as Member of Structure
Let’s Take a Function “Print”
void print()
{
cout<<"Printing the points:";
cout<<"x:"<<x<<" y:"<<y<<endl;
}
Function in STRUCTS
struct point
{
int x; Function within struct point
int y;
void print()
{
cout<<"Printing the points:";
cout<<"x:"<<x<<" y:"<<y<<endl;
}
};
How to call the function – dot operator
Calling a Function for instance “Second”
struct point
{
int x;
int y;
void print()
{
cout<<"Printing the points:";
cout<<"x:"<<x<<" y:"<<y<<endl;
}
};
int main(){
point second;
second.x = 2;
second.y = 9;
[Link]();
return 0;}
#include <iostream>
using namespace std;
struct point
{
Using Pointers
int x;
int y;
void print()
{
cout<<"Printing the points:"<<endl;
cout<<"x:"<<x<<endl;
cout<<"y:"<<y<<endl;
}
};
int main(){
point A;
A.x = 2;
A.y = 9;
[Link]();
point *ptr;
ptr = &A;
ptr->x;
ptr->y;
ptr->print();
return 0;}
Calling a Function for instance “Second”
What will be the output here?
struct point
{
int x; x and y may display GARBAGE
int y;
void print()
{
Initialize the variables
cout<<"Printing the points:";
cout<<"x:"<<x<<" y:"<<y<<endl; Any Problem ???
}
}; Write a Function - Problem
int main(){
point second;
second.x = 2;
Function has to be called
second.y = 9; every time
[Link]();
return 0;} Any Solution ?
Constructors
Constructors – Similar to Functions
point()
{
x = 0;
y = 0;
}
STRUCTS
struct point
with CONSTRUCTOR
{
int x;
int y;
void print()
{ What will happen now ?
cout<<"Printing the points:";
Print Function
cout<<"x:"<<x<<" y:"<<y<<endl;
} And
point()
{ Print Constructor
x = 0;
y = 0; x:0 y:0
}
};
point third; What happening ?
[Link]();
Automatically calls Constructor
whenever
an instance of struct is
declared
Parameterized Constructors
struct point
{
int x;
int y;
void print() //you can now call this function this way
{ too
cout<<"Printing the points:"; point third;
cout<<"x:"<<x<<" y:"<<y<<endl;
[Link]();
}
point()
{ //or you can call this in this way too
x = 0; point fourth(2,3);
y = 0; [Link]();
}
point(int p,int q)
{
x = p;
y = q;
}
};
More on Structs
struct person
{
int bday_year;
Int bday_day;
Int bday_month;
string name;
person()
{
Is there a better way ?
bday_year = 0;
bday_day = 0;
bday_month = 0;
name = "None";
}
void print()
{
cout<<”Year"<<bday_year<<endl;
cout<<”Month"<<bday_month<<endl;
cout<<”Day"<<bday_day<<endl;
cout<<"Name"<<name<<endl;
}
};
struct date
{ Define a STRUCT ‘date’
int day;
int month;
int year;
date()
{
day = 0;
month = 0;
year = 0;
}
date(int d,int m,int y)
{
day = d;
month = m;
year = y;
}
void print()
{
cout<<"Day:"<<day<<endl;
cout<<"Month:"<<month<<endl;
cout<<"Year:"<<year<<endl;
}
};
STRUCT within STRUCT
struct structure1
{
----------
----------
};
struct structure2
{
----------
----------
structure1 obj;
};
Example
Use ‘date’ in ‘person’
struct person
{
date d;//d's constructor automatically called//
string name;
person()
{
name = "None";
}
void print()
{
[Link](); //USING THE ALREADY DEFINED FUNCTION THERE IN DATE METHOD
cout<<"Name"<<name<<endl;
}
};
//following goes in the main program
person p;
[Link]();
How to access ‘date’ variables
[Link] = 13;
[Link] = 3;
[Link] = 1999;
[Link] = "Sherlock";
[Link]();
struct inventory
{ Arrays of STRUCT
int part_no;
float cost;
float price;
void print()
{
cout<<"part_no"<<part_no<<endl;
cout<<"cost"<<cost<<endl;
cout<<"price"<<price<<endl;
}
};
// in the main program
inventory table[4];
table[0].part_no=123;
table[0].cost=10;
table[0].price=15;
table[0].print();
Setting Values
//default constrcutor automatically called
inventory table[4];
//check
table[0].print();
table[1].print();
table[2].print();
table[3].print();
//Setting value through main program
table[0].part_no=12;
table[0].cost=10;
table[0].price=120;
table[0].print();
Setting Values
//Alternate 1 way through main program
table[1] = {.part_no=11,.cost=10,.price=100};
table[1].print();
//Alternate 2 way through main program
table[1] = {11,10,100};
table[1].print();
//Alternate 3 through parameterized constructor
table[0]=inventory(1,1.0,1.0);
table[1]=inventory(2,2.0,2.0);
table[0].print();
table[1].print();