OOP - Lecture 5 Structures
OOP - Lecture 5 Structures
Spring 2022
Hira Naveed
Lecture # 5 Structures
Abstract Data Type
struct structName
{
dataType field1;
dataType field2;
. . .
};
Example struct Declaration
double gpa;
};
gpa
Creating struct Variables
struct Student
{ int studentID;
string name;
short yearInSchool;
double gpa;
} student1;
Creating struct Variables Two Ways
struct Employee struct Student
{
string firstName; {
string lastName; int studentID;
string address;
double salary; string name;
int deptID; short yearInSchool;
};
double gpa;
Employee e1; } s1, s2;
11
Accessing Structure Members
struct Student
{
int studentID = 0;
string name = ““;
short yearInSchool = 1;
double gpa = 1.0;
};
Accessing Structure Members
void main{ struct PayRoll {
int empNumber;
emp1.empNumber = 489; string name;
double hours;
emp1.name = “Jill Smith”;
double payRate;
double grossPay;
emp1.hours = 23;
} emp1;
emp1.payRate = 20;
…
21
Arrays of Structures
24
Array as Member of Structures
• A structure may also contain arrays as members.
struct Student
{
int RollNo;
float Marks [3];
};
struct Student {
int age;
int marks;
int arr[3];
};
void main() {
Student s[3] = { 1,2,3,4,5,{},7,8,9,10,11 };
for (int i = 0; i < 3; i++) {
cout << s[i].age << endl;
cout << s[i].marks << endl;
cout << s[i].arr[0] << " " << s[i].arr[1] << " " <<
s[i].arr[2] << endl;;
cout<<“-----------”<<endl; }
}
27
Array as Member of Structures
struct Student {
int age;
int marks;
int arr[3];
};
void main() {
Student s[3] = { 1,2,3,4,5,6,7,8,9,10,11 };
for (int i = 0; i < 3; i++) {
cout << s[i].age << endl;
cout << s[i].marks << endl;
cout << s[i].arr[0] << " " << s[i].arr[1] << " " <<
s[i].arr[2] << endl;;
cout<<“------------”<<endl; }
}
28
Nested Structure
• A structure can be a member of another structure: called
nested structure
struct A
{
int x;
double y;
};
struct B
{
char ch; record
A v2; v2
}; ch x y
B record;
29
Initializing/Assigning to a Nested
Structure void main() // Input
{
struct A{ B record;
int x; cin >> record.ch;
float y; cin >> record.v2.x;
}; cin >> record.v2.y;
}
struct B{
char ch; void main()
A v2; //Assignment
}; {
void main() B record;
//Initialization record.ch = ‘S’;
{ record.v2.x = 100;
B record ={‘S’,{100, 3.6}}; record.v2.y = 3.6;
} } 30
Pointers to Structures
Student *stuPtr;
void main( )
{
Rectangle rect1 = {22,33};
Rectangle* rect1Ptr = &rect1;
}
32
Accessing Structures with Pointers
void main( )
{
Rectangle rect1 = {22,33};
Rectangle* rect1Ptr = &rect1;
cout<<(*rectPtr1).width << endl;
cout<<(*rectPtr1).height << endl;
} 33
Accessing Structures with Pointers
void main( )
{
Rectangle rect1 = {22,33};
Rectangle* rect1Ptr = &rect1;
cout<< rectPtr1->width << endl;
cout<< rectPtr1->height << endl;
} 34
Anonymous Structure
36
Other Stuff You Can Do With a struct
37
Quick Example
struct StudentRecord {
string name; // student name
int marks[5]; // test grades
double ave; // final average
void print_ave( ) {
cout << "Name: " << name << endl;
cout << "Average: " << ave << endl;
}
};
38
Using a Member Function
StudentRecord stu;
stu.print_ave( );
39
Structures as Function Arguments
//function definition
float computeGPA(float gpa){
……………
}
//function call
computeGPA(s1.gpa);
Structures as Function Arguments
1. Pass-by-value
2. Pass-by-reference
3. Pass-using pointers
Structures as Function Arguments –
Pass by Value
struct Rectangle { A copy of the struct
box is created and
double length; saved in the function
double width; parameter r
double area;
};
void changeRect(Rectangle r) {
r.length = 5;
r.width = 6;
r.area = 30;
}
void main(){
Rectangle box = {1, 2, 2};
changeRect(box); }
Structures as Function Arguments –
Pass by Value
void changeRect(Rectangle r) {
r.length = 5;
r.width = 6;
r.area = 30;
}
void main(){
Rectangle box = {1, 2, 2};
changeRect(box);
}
Structures as Function Arguments –
Pass by const Reference
void showRect(const Rectangle &r) {
cout << r.length << endl;
cout << r.width << endl;
cout << r.area << endl;
}
void main(){
Rectangle box = {1, 2, 2};
showRect(box);
}
Output: 1
2
2
Returning a Structure from a Function
Student getStudentData()
{
Student tempStu;
return tempStu;
}
Practice Question 1
51
Practice Question 1
struct Car {
string model;
int year;
float price;
};
void main() {
Car showroom[30]; //array of cars
for (int i = 0; i < 30; i++) {
cin >> showroom[i].model;
cin >> showroom[i].year;
cin >> showroom[i].price;
}
for (int i = 0; i < 30; i++) {
if (showroom[i].price > 500000) {
cout << showroom[i].model<<" "<< showroom[i].year <<" "
<<showroom[i].price;
}
}
}
52
Practice Question 2
ContactInfo
City Country
53
Practice Question 2
struct Address {
string city;
string country; };
struct ContactInfo {
string name;
long int number;
Address address; };
void main() {
ContactInfo phonebook[10];
for (int i = 0; i < 10; i++) {
cin >> phonebook[i].name;
cin >> phonebook[i].number;
cin >> phonebook[i].address.city;
cin >> phonebook[i].address.country;
}
for (int i = 0; i < 30; i++) {
cout << phonebook[i].name << " " << phonebook[i].number << " "
<< phonebook[i].address.city << " " << phonebook[i].address.country
<< endl;;
} }
54