2_Structures
2_Structures
1
Structure
• A Structure is a container, it can hold a bunch of
things.
– These things can be of any type.
3
Defining the Structure
• Structure definition tells how Syntax:
the structure is organized: it
struct StructName
specifies what members the {
structure will contain. DataType1 Identifier1;
DataType2 Identifier2;
.
.
.
Syntax & Example };
Example:
struct Product
{
int ID;
string name;
float price;
};
4
Structure Definition Syntax
5
Structure members in memory
Memory is only allocated
when we create struct type
variables
4 Bytes
modelnumber
struct part
{ 4 Bytes
int partnumber
modelnumber;
int partnumber;
float cost;
}; cost 4 Bytes
6
Declaring a Structure variable
• Structure variable can be created after the definition
of a structure. The syntax of the declaration is:
StructName Identifier;
Example:
part processor;
part keyboard;
7
Another way of Declaring a Structure variable
Employee e1;
9
Initializing Structure Variables
• The syntax of initializing structure is:
10
Structure Variable Initialization with Declaration
struct Student
{
string firstName;
string lastName;
char courseGrade;
int marks;
};
void main( )
{
Student s1= {“M”, “Umar”, ‘A’, 94} ;
}
student s1;
s1.firstName = “ABC”;
s1.lastName = “XYZ”;
s1.courseGrade = ‘A’;
s1.marks = 93;
12
Assigning Values to Structure Variables
• After creating structure variable, values to structure
members can be assigned using cin
student s1;
cin>>s1.firstName;
cin>>s1.lastName;
cin>>s1.courseGrade;
cin>>s1.marks ;
13
Assigning one Structure Variable to another
• A structure variable can be assigned to another
structure variable only if both are of same type
Example:
studentType Student1 = {“Amir”, “Ali”, ‘A’, 98} ;
studentType student2 = Student1;
14
Array of Structures
An array of structure is a type of array in which each
element contains a complete structure.
struct Book
{
int ID;
int Pages;
float Price;
};
Book Library[100]; // declaration of array of structures
ID
Library[0]
Pages Price ID
Library[1]
Pages Price
… ID
Library[99]
Pages Price
15
Initialization of Array of Structures
struct Book
{
int ID;
int Pages;
float Price;
};
Book b[3]; // declaration of array of structures
17
Array as Member of Structures
• A structure may also contain arrays as members.
struct Student
{
int RollNo;
float Marks[3];
};
struct B
{ record
char ch; v1
A v1; ch x y
};
B record; 20
Initializing/Assigning to Nested Structure
struct A{ void main() // Input
int x; {
float y; B record;
}; cin>>record.ch;
cin>>record.v2.x;
struct B{ cin>>record.v2.y;
char ch; }
A v2;
}; void main() // Assignment
{
B record;
void main() // Initialization record.ch = ‘S’;
{ record.v2.x = 100;
B record = {‘S’, {100, 3.6} }; record.v2.y = 3.6;
} }
21
Accessing Structures with Pointers
• Pointer variables can be used to point to structure
type variables too.
• The pointer variable should be of same type, for
example: structure type
struct Rectangle {
int width;
int height;
};
void main( )
{
Rectangle rect1={22,33};
Rectangle* rect1Ptr = &rect1; 22
}
Accessing Structures with Pointers
• How to access the structure members (using
pointer)?
– Use dereferencing operator (*) with dot (.) operator
struct Rectangle {
int width;
int height;
};
void main( )
{
Rectangle rect1={22,33};
Rectangle* rectPtr = &rect1;
cout<< (*rectPrt).width<<(*rectPrt).height; 23
}
Accessing Structures with Pointers
• Is there some easier way also?
– Use arrow operator ( -> )
struct Rectangle {
int width;
int height;
};
void main( )
{
Rectangle rect1={22,33};
Rectangle* rectPtr = &rect1;
cout<< rectPrt->width<<rectPrt->height;
24
}
Anonymous Structure
• Structures can be anonymous:
• Anonymous structure definition must have a structure
type variable.
25
Other stuff you can do with a struct
• You can also associate functions with a structure
(called member functions)
void print_ave( ) {
cout << "Name: " << name << endl;
cout << "Average: " << ave << endl;
}
};
Using the member function
StudentRecord stu;
stu.print_ave( );
Structures and Functions
• Structures can be passed in a function:
1. Pass-by-value
2. Pass-by-reference
3. Pass-by-reference (using pointers)
29
Structures and Functions – By Value
30
31
Structures and Functions – By Value
• Problem: copy of a large structure takes time and a lot
of memory
32
Structures and Functions
• Structures can be returned by a function
33
Structures Vs. Classes
• Members in Structures are default public
• Members in Classes are default private
34
Practice Question 1
• Define a structure called “car”. The member elements
of the car structure are:
• string Model;
• int Year;
• float Price
35
Practice Question 2
• Write a program that implements the following using C++
struct. The program should finally displays the values stored
in a phone directory (for 10 people)
PhoneDirectory
City Country
36