Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

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 << employee1.name <<endl;
cout << employee1.age <<endl;
cout << employee1.salary<<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. cin.get(p1.name, 50);
12. cout << "Enter class Strength: ";
13. cin >> p1.strength;
14. cout << "Enter Average: ";
15. cin >> p1.average;
16.
17. cout << "\nDisplaying Information." << endl;
18. cout << "Name: " << p1.name << endl;
19. cout <<"Strength: " << p1.strength << endl;
20. cout << "Average: " << p1.average;
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: "<< m.name;
9. cout<<"\n Average of module: "<< m.average;}

10. int main(){


11. module m1;
12. m1.average = 85;
13. m1.name = "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: "<< M1.name;
10. cout<<"\nID: "<<M1.id;
11. cout<<"\nAverage Marks: "<<M1.a.average ;
12. cout<<"\nHighest Marks: "<< M1.a.highest ;
13. cout<<"\nLowest Marks: "<< M1.a.lowest ; }

14. int main() {


15. module M;
16. M.name="Introduction to Programming"; //adding values to the member of Employee structure
M.id= "CS-200";
17. M.a.average =80.57; //Adding values to the member of salary structure M.a.highest =95;

18. M.a.lowest =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;
second.print();
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;
A.print();

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
second.print();
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 ?
third.print();
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;
third.print();
}
point()  
{ //or you can call this in this way too
x = 0; point fourth(2,3);
y = 0; fourth.print();
}
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()
{
d.print(); //USING THE ALREADY DEFINED FUNCTION THERE IN DATE METHOD
cout<<"Name"<<name<<endl;
}
};

//following goes in the main program


person p;
p.print();
How to access ‘date’ variables
p.d.day = 13;
p.d.month = 3;
p.d.year = 1999;
p.name = "Sherlock";
p.print();
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();

You might also like