Professional Documents
Culture Documents
Basics: Structures - Struct
Basics: Structures - Struct
Structures - Struct
Structs (Structure)
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;};
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;
}
};