1. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming FundamentalsLecture 11: Programming Fundamentals
Lecture 11
Structure
1
2. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Structure Concept
• Although arrays greatly improved our ability to
store data, there is one major drawback to their
use ... each element (each box) in an array must
be of the same data type.
• It is often desirable to group data of different
types and work with that grouped data as one
entity.
• We now have the power to accomplish this
grouping with a new data type called a structure.
2
3. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Structure Concept
• A structure is a collection of variable types
grouped together.
• You can refer to a structure as a single variable,
and to its parts as members of that variable by
using the dot (.) operator.
• The power of structures lies in the fact that once
defined, the structure name becomes a user-
defined data type and may be used the same
way as other built-in data types, such as int,
double, char.
3
4. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
How to declare and create a Structure
• The structure is declared by using the keyword
struct followed by structure name, also called a
tag.
• Then the structure members (variables) are
defined with their type and variable names inside
the open and close braces "{"and "}“ and must be
closed with semicolon.
• The above structure declaration is also called a
Structure Specifier.
4
5. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
• Three variables:
– custnum of type int,
– salary of type int,
– commission of type float
–Are structure members and the structure name is Customer. This
structure is declared as follows:
5
6. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
How to Access Structure Members
• Similar to variable declaration.
– For variable declaration, data type is defined followed by
variable name.
– For structure variable declaration, the data type is the name
of the structure followed by the structure variable name.
• In the above example, structure variable cust1
is defined as:
6
7. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
• A programmer wants to assign 2000 for the
structure member salary in the above example
of structure Customer with structure variable
cust1 this is written as:
7
8. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
• Write a program that declares a structure to store Roll No., Marks, average and grade of student. The
program should define a structure variable, inputs the values and then displays values.
main()
{
struct Student
int rno;
int marks;
float avg;
char grade;
};
Student scr;
cout<<"Enter Roll No. ";
cin>>scr.rno;
cout<<"Enter Marks ";
cin>>scr.marks;
cout<<"Enter Average ";
cin>>scr.avg;
cout<<"Enter Grade ";
cin>>scr.grade;
8
cout<<"You enter the following data "<<endl;
cout<<"Roll No. "<<scr.rno<<endl;
cout<<"Marks "<<scr.marks<<endl;
cout<<"Average "<<scr.avg<<endl;
cout<<"Grade "<<scr.grade<<endl;
getch();
}
9. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Program to define a structure with 5
members. The first member be a student
name and other be marks obtained in
subjects. Assign values to a members during
their declaration. Calculate total marks and
then print the numbers and the total marks of
students.
9
10. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
struct result
{
Char name[15];
int s1, s2, s3, s4;
};
result student = {“Ali”, 70, 40, 90, 78};
int total = student. s1 + student.s2 + student.s3 + student.s4;
cout<<“Name of Student : “ << student.name<<endl;
cout<<“Total Marks :“<<student.total<<endl;
10
11. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Write a program that declare a structure to
store day, month and year of birth date. It
input three values and displays date of birth in
dd/mm/yy format.
11
12. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
struct birth
{
int day;
Int month;
float year;
};
birth b;
cout<<“ Enter day of birth “;
cin>>b.day;
cout<<“ Enter month of birth “;
cin>>b.month;
cout<<“ Enter year of birth “;
cin>>b.year;
cout<<“your date of birth is”<<b.day<<“/”<<b.month<<“/”<<b.year;
12
13. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Write a program that declare a structure to
store Book ID, price and pages of a book. It
then display record of most costly book.
13
14. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
14
struct book
{
int id;
Int pages;
float price;
};
book b1, b2;
cout<<“ Enter id, pages and price of book 1“;
cin>>b1.id>>b1.pages>>b1.price;
cout<<“ Enter id, pages and price of book 2“;
cin>>b2.id>>b2.pages>>b2.price;
cout<<“ The most costly book is “;
if(b1.price>b2.price)
{
cout<<“First Book”<<b1.id<<b1.pages<<b1.price<< “ has high cost”;
}
Else
{
cout<<“Second Book”<<b2.id<<b2.pages<<b2.price<< “ has high cost”;
}
15. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Array type Member Structure
• The member of structure may be of different
types.
• These can also be simple variables or array
variables.
struct record
{
char name [15];
int sub[4];
};
15
16. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
struct record
{
char name [15];
int sub[4];
};
16
Ahmed
Roll No.
0 1 2 3 4
17. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
17
Write a program that declare a structure to
store 3 Books ID, price and pages. It then
display record of most costly book.
18. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
struct book
{
int id;
Int pages;
float price;
};
book b[3];
int i, max, m
for(i = 0; i<3; i++)
{
cout<<“ Enter book ID“;
cin>>b[i].id;
cout<<“ Enter pages of book “;
cin>>b[i].pages;
cout<<“ Enter the price of book“;
cin>>b[i].price;
}
max = b[i].price;
m = 0;
for(i = 0; i<3; i++)
if(b[i].price>max)
{
max = b[i].price;
m = i;
}
cout<<“Costly book is ”;
cout <<b[m].id<<endl;
cout<<b[m].pages<<endl;
cout<<<<b[m].price”;
}
18
19. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
struct result
{
char name[15];
int sub[4];
};
result student;
int i, total;
Cout<< “Enter student name”;
Cin>> student.name;
for(i=0; i<=3; i++)
{
cout<< “Enter the marks of subject “<<I + 1<< “ = “;
cin>> student.sub[i];
}
total = student.sub[0] + student.sub[1] +
student.sub[2] + student.sub[3] ;
cout<<“Name of Student“
<<student.name<<endl;
for(i=0; i<=3; i++)
{
cout<<"Marks
"<<student.sub[i]<<endl;
}
cout<<“Total Marks "<<total<<endl;
}
19
20. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
Write a program that declare a structure to
store roll no. and marks of five subjects. It
then display Roll No., marks and average
marks.
20
21. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Struct Test
{
int rno;
int marks[5];
};
Test r;
int i; total = 0;
float avg = 0;
cout<< “Roll Number “;
cin>> r.rno[i];
for( i = 0; i<5; i++)
{
cout<< “Enter Marks “;
cin>> r.marks[i];
total = total + r.marks[i];
}
avg = total/5;
cout<< “Roll No. “<<r.rno<<endl;
cout<<“Total Marks “<<total<<endl;
cout<<“Average “<<avg;
}
21
22. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
NESTED STRUCTURE
22
23. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
• A nested structure is created when the
member of a structure is itself a structure.
23
24. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
struct info
{
Char s_name;
Char f_name;
Char city;
Int age;
};
Struct p_data
{
Info s1;
Info s2;
Float x;
};
P_data rec
24
25. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
struct result
{
int marks;
char grades;
};
struct record
{
int rno;
result r;
};
record rec;
cout<<"Enter Roll No. ";
cin>>rec.rno;
cout<<"Enter Marks ";
cin>>rec.r.marks;
cout<<"Enter grade ";
cin>>rec.r.grade;
cout<<"Roll No. "<<rec.rno<<endl;
cout<<"Marks "<<rec.r.marks<<endl;
cout<<"Grade "<<rec.r.grade<<endl;
25
26. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Write a program using nested structure that
display the data of a person in following
format.
26
Phonebook
Name City Phone Birthday
Day Month Years
27. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
struct date
{
int day;
int month;
int year;
};
struct phonebook
{
char name[20];
char city[15];
int tel;
date birthday;
};
struct phonebook abc;
cout<<“Enter Name “;
cin>>abc.name;
cou<<“Enter City “;
cin>>abc.city;
cout<<“Enter phone No. “;
cin>>abc.tel;
cout<<“Enter Date of Birth in following format (DD/MM/YY) “;
cin>>abc.birthday>>abc.month>>abc.year;
cout<<“ The Entry made is “;
cout<<abc.name << “----”<<abc.city<<“----”<<abc.tel;
cout<<“Birthday is”<<abc.birthday.day << “----”<<
abc.birthday.month<< “----”<< abc.birthday.year
27
28. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
ENUMERATION
28
29. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Classification of Data Types
29
• An enumeration is a user-defined type
consisting of a set of named constants called
enumerators.
30. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
• An enumeration provides context to describe a range of values. The
following example shows an enumeration that contains the four
suits in a deck of cards.
– enum Suit { Diamonds, Hearts, Clubs, Spades };
• Every name of the enumeration becomes an enumerator and is
assigned a value that corresponds to its place in the order of the
values in the enumeration. By default, the first value is assigned 0,
the next one is assigned 1, and so on. You can set the value of an
enumerator.
– enum Suit { Diamonds = 1, Hearts, Clubs, Spades };
• The enumerator Diamonds is assigned the value 1. This affects the
values that are assigned to subsequent enumerators; Hearts is
assigned the value 2, Clubs is 3, and so on.
30
31. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Basic Data Types
C++ Data Types
User-defined Type Built-in Type Derived Type
Integral Type Void Floating Type
structure
union
class
enumeration
array
function
pointer
reference
int char float double
31
32. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Enumerated Data Type:
Enumerated data type provides a way for attaching
names to numbers.
enum keyword automatically enumerates a list of
words by assigning them values 0, 1, 2, and so on.
For Example:
• enum shape {circle, square, triangle};
• enum colour {red, blue, green, yellow};
• enum position {off, on};
32
33. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Syntax
ď‚· The syntax for declaring an enumeration data type is:
– value1, value2, … are identifiers called enumerators
– value1 < value2 < value3 <...
OR
enum typeName{obj1, obj2, ...};
33
34. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
enum year {january, feburary, march, april,
may, june, july, august, september, october,
november, december};
year y;
y = march;
cout<<“ The value of y is “<<y;
34
Output:
The value of y is 2
35. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example Continue
Example No. 2
Example No. 3
35
36. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example Continue
• .
Example No. 5
Example No. 4
36