Faculty of Computer Science
Object Oriented Programming Language
Lecturer: Lutfullah Haqnesar
OOP Chapter 02
Chapter 3
Structure and Union Data Type
OOP Chapter 02
Learning outcomes:
• Structure
• Union
• Difference between structure & union
Structure Chapter 02
What is Security?
• Structure is a collection of variables of different data types under a single
name.
• Structures are a way to group several related variables into one place.
• Unlike an array, a structure can contain many different data types (int,
string, bool, etc.).
• The data items in a structure are called the members of the structure.
Structure Chapter 02
What is Security?
Syntax of Structure:
struct structure_name
{
// member declarations.
};
• To create a structure, use the struct keyword and declare each of its
members inside curly braces.
Structure example Chapter 02
What is Security?
struct Student
{
char name[20];
int id;
int age;
}
• In the above examle, Student is a structure that contains three variables
name, id, and age.
Structure Chapter 02
What is Security?
How to create the instance of Structure?
• Structure variable can be defined as:
Structure_name instance_name;
Example:
Student s;
• Here, s is a structure variable of type Student.
• When the structure variable is created, the memory will be allocated.
Structure Chapter 02
What is Security?
How to create the instance of Structure (con…)?
struct Student
{
char name[20];
int id;
int age;
}
• Student structure contains one char variable and two integer variable.
• Therefore, the memory for one char variable is 1 byte and two ints will be
2*4 = 8. The total memory occupied by the s variable is 9 byte.
Structure Chapter 02
What is Security?
How to access the variable of Structure?
• The variable of the structure can be accessed by using the instance of the
structure followed by the dot (.) operator and then the field of the
structure.
Example:
s.id = 4;
Structure Example 1 Chapter 02
What is Security?
#include <iostream>
using namespace std;
struct Rectangle {
int width, height;
};
int main() {
Rectangle rec;
rec.width=8;
rec.height=5;
cout<<"Area of Rectangle is: "<<(rec.width * rec.height)<<endl;
return 0;
}
Structure Example 2 Chapter 02
What is Security?
#include <iostream>
using namespace std;
struct car {
string brand;
string model;
int year;
};
int main() {
car myCar1;
myCar1.brand = "BMW";
myCar1.model = "X5";
myCar1.year = 1999;
cout << myCar1.brand << " " << myCar1.model << " " << myCar1.year;
return 0; }
Structure Example 3 Chapter 02
What is Security?
#include <iostream>
using namespace std; cin>>p1.name;
struct Person cout << "Enter age: ";
{ cin >> p1.age;
char name[50]; cout << "Enter salary: ";
int age; cin >> p1.salary;
float salary; cout << "Displaying Information." << endl;
}; cout << "Name: " << p1.name << endl;
int main() { cout <<"Age: " << p1.age << endl;
Person p1; cout << "Salary: " << p1.salary;
cout << "Enter Full name: "; return 0;
}
Union Chapter 02
What is Security?
• Union is a user-defined datatype. Similarly to the structure, the union can
contain different types of data types.
• All the members of union share same memory location.
• Size of union is decided by the size of largest member of union.
• If you want to use same memory location for two or more members,
union is the best for that.
• The keyword “union” is used to define unions in C language.
Union example Chapter 02
What is Security?
#include <iostream> << G1.m1 << endl;
using namespace std; cout << "The next value stored "
union un { << "after removing the previous value : "
int m1; << G1.m2 << endl;
char m2; cout << "The Final value value "
float m3; << "at the same allocated memory space : "
}; << G1.m3 << endl;
int main() { cout<<&G1.m1<<endl;
un G1, G2, G3; cout<<&G1.m2<<endl;
G1.m1 = 34; cout<<&G1.m3<<endl;
G1.m2 = 'a'; return 0;
G1.m3 = 34.34; }
cout << “First value at allocated memory:"
Structure and Union Chapter 02
What is Security?
Struct Union
The struct keyword is used to define a The union keyword is used to define
structure. union.
The size of a structure is equal to the sum The size of a union is equal to the size of
of the sizes of each data member. its largest data member size.
Each variable member occupied a unique Variables members share the memory
memory space. space of the largest size variable.
Changing the value of a member will not Changing the value of one member will
affect other variables members. also affect other variables members.
All variable members store some value at Only one data member stores a value at
any point in the program. any particular instance in the program.
It allows accessing and retrieving any data It allows accessing and retrieving any one
member at a time. data member at a time.
OOP Chapter 02
What is Security?
End of Chapter