SlideShare a Scribd company logo
1 of 36
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming FundamentalsLecture 11: Programming Fundamentals
Lecture 11
Structure
1
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
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
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
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
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
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
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();
}
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
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
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
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
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
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”;
}
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
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
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.
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
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
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
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
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
NESTED STRUCTURE
22
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
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
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
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
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
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
ENUMERATION
28
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.
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
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
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
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
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
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example Continue
Example No. 2
Example No. 3
35
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example Continue
• .
Example No. 5
Example No. 4
36

More Related Content

What's hot

Relational Database Design
Relational Database DesignRelational Database Design
Relational Database DesignPrabu U
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1sumitbardhan
 
Compare between pop and oop
Compare between pop and oopCompare between pop and oop
Compare between pop and oopMd Ibrahim Khalil
 
Ak procedural vs oop
Ak procedural vs oopAk procedural vs oop
Ak procedural vs oopAbhishek Kumar
 
diffrence between procedure oriented programming & object oriented programmin...
diffrence between procedure oriented programming & object oriented programmin...diffrence between procedure oriented programming & object oriented programmin...
diffrence between procedure oriented programming & object oriented programmin...nihar joshi
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++ shammi mehra
 
Intake 37 ef1
Intake 37 ef1Intake 37 ef1
Intake 37 ef1Mahmoud Ouf
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstractionHoang Nguyen
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2Mahmoud Ouf
 
Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming Rokonuzzaman Rony
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and StructuresGem WeBlog
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesDurgesh Singh
 
Object Oriented Programming lecture 1
Object Oriented Programming lecture 1Object Oriented Programming lecture 1
Object Oriented Programming lecture 1Anwar Ul Haq
 
Mca5010 web technologies
Mca5010   web technologiesMca5010   web technologies
Mca5010 web technologiessmumbahelp
 
Architecture of Native XML Database Sedna
Architecture of Native XML Database SednaArchitecture of Native XML Database Sedna
Architecture of Native XML Database Sednamaria.grineva
 
Procedural vs. object oriented programming
Procedural vs. object oriented programmingProcedural vs. object oriented programming
Procedural vs. object oriented programmingHaris Bin Zahid
 
Information Extraction from HTML: General Machine Learning ...
Information Extraction from HTML: General Machine Learning ...Information Extraction from HTML: General Machine Learning ...
Information Extraction from HTML: General Machine Learning ...butest
 

What's hot (20)

Relational Database Design
Relational Database DesignRelational Database Design
Relational Database Design
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Array Cont
Array ContArray Cont
Array Cont
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
 
Compare between pop and oop
Compare between pop and oopCompare between pop and oop
Compare between pop and oop
 
Ak procedural vs oop
Ak procedural vs oopAk procedural vs oop
Ak procedural vs oop
 
diffrence between procedure oriented programming & object oriented programmin...
diffrence between procedure oriented programming & object oriented programmin...diffrence between procedure oriented programming & object oriented programmin...
diffrence between procedure oriented programming & object oriented programmin...
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
 
Intake 37 ef1
Intake 37 ef1Intake 37 ef1
Intake 37 ef1
 
Structures,pointers and strings in c Programming
Structures,pointers and strings in c ProgrammingStructures,pointers and strings in c Programming
Structures,pointers and strings in c Programming
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstraction
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
Object Oriented Programming lecture 1
Object Oriented Programming lecture 1Object Oriented Programming lecture 1
Object Oriented Programming lecture 1
 
Mca5010 web technologies
Mca5010   web technologiesMca5010   web technologies
Mca5010 web technologies
 
Architecture of Native XML Database Sedna
Architecture of Native XML Database SednaArchitecture of Native XML Database Sedna
Architecture of Native XML Database Sedna
 
Procedural vs. object oriented programming
Procedural vs. object oriented programmingProcedural vs. object oriented programming
Procedural vs. object oriented programming
 
Information Extraction from HTML: General Machine Learning ...
Information Extraction from HTML: General Machine Learning ...Information Extraction from HTML: General Machine Learning ...
Information Extraction from HTML: General Machine Learning ...
 

Similar to Structure

CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10trupti1976
 
CHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxCHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxGebruGetachew2
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to ProgrammingALI RAZA
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdfAnkurSingh656748
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiMuhammed Thanveer M
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referentialbabuk110
 
Student database management system
Student database management systemStudent database management system
Student database management systemSnehal Raut
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4YOGESH SINGH
 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.pptexample43
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...ANUSUYA S
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxVigneshkumar Ponnusamy
 
12th CBSE Computer Science Project
12th CBSE Computer Science Project12th CBSE Computer Science Project
12th CBSE Computer Science ProjectAshwin Francis
 
Ocs752 unit 5
Ocs752   unit 5Ocs752   unit 5
Ocs752 unit 5mgrameshmail
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patilwidespreadpromotion
 

Similar to Structure (20)

CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
CHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxCHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptx
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
 
Structures
StructuresStructures
Structures
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
12Structures.pptx
12Structures.pptx12Structures.pptx
12Structures.pptx
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 
Student database management system
Student database management systemStudent database management system
Student database management system
 
Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
Structure
StructureStructure
Structure
 
algo 1.ppt
algo 1.pptalgo 1.ppt
algo 1.ppt
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptx
 
12th CBSE Computer Science Project
12th CBSE Computer Science Project12th CBSE Computer Science Project
12th CBSE Computer Science Project
 
Ocs752 unit 5
Ocs752   unit 5Ocs752   unit 5
Ocs752 unit 5
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
 

More from ALI RAZA

Recursion
RecursionRecursion
RecursionALI RAZA
 
pseudocode and Flowchart
pseudocode and Flowchartpseudocode and Flowchart
pseudocode and FlowchartALI RAZA
 
Algorithm Development
Algorithm DevelopmentAlgorithm Development
Algorithm DevelopmentALI RAZA
 
Programming Fundamentals using C++
Programming Fundamentals using C++Programming Fundamentals using C++
Programming Fundamentals using C++ALI RAZA
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to ProgrammingALI RAZA
 
Array sorting
Array sortingArray sorting
Array sortingALI RAZA
 
Array programs
Array programsArray programs
Array programsALI RAZA
 
2D-Array
2D-Array 2D-Array
2D-Array ALI RAZA
 
Quiz game documentary
Quiz game documentaryQuiz game documentary
Quiz game documentaryALI RAZA
 
Function pass by value,function pass by reference
Function pass by value,function pass by reference Function pass by value,function pass by reference
Function pass by value,function pass by reference ALI RAZA
 
Drug Addiction 39 Slides
Drug Addiction 39 SlidesDrug Addiction 39 Slides
Drug Addiction 39 SlidesALI RAZA
 
Drug Addiction Original 51 Slides
Drug Addiction Original 51 SlidesDrug Addiction Original 51 Slides
Drug Addiction Original 51 SlidesALI RAZA
 
Passing stuctures to function
Passing stuctures to functionPassing stuctures to function
Passing stuctures to functionALI RAZA
 
Basic general knowledge
Basic general knowledgeBasic general knowledge
Basic general knowledgeALI RAZA
 
Dil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shamsDil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shamsALI RAZA
 
Pathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-completePathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-completeALI RAZA
 
Husne akhlaq
Husne akhlaqHusne akhlaq
Husne akhlaqALI RAZA
 
Parts of speech sticky note definitions and examples
Parts of speech sticky note definitions and examplesParts of speech sticky note definitions and examples
Parts of speech sticky note definitions and examplesALI RAZA
 
Quik tips
Quik tipsQuik tips
Quik tipsALI RAZA
 
Binary
BinaryBinary
BinaryALI RAZA
 

More from ALI RAZA (20)

Recursion
RecursionRecursion
Recursion
 
pseudocode and Flowchart
pseudocode and Flowchartpseudocode and Flowchart
pseudocode and Flowchart
 
Algorithm Development
Algorithm DevelopmentAlgorithm Development
Algorithm Development
 
Programming Fundamentals using C++
Programming Fundamentals using C++Programming Fundamentals using C++
Programming Fundamentals using C++
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Array sorting
Array sortingArray sorting
Array sorting
 
Array programs
Array programsArray programs
Array programs
 
2D-Array
2D-Array 2D-Array
2D-Array
 
Quiz game documentary
Quiz game documentaryQuiz game documentary
Quiz game documentary
 
Function pass by value,function pass by reference
Function pass by value,function pass by reference Function pass by value,function pass by reference
Function pass by value,function pass by reference
 
Drug Addiction 39 Slides
Drug Addiction 39 SlidesDrug Addiction 39 Slides
Drug Addiction 39 Slides
 
Drug Addiction Original 51 Slides
Drug Addiction Original 51 SlidesDrug Addiction Original 51 Slides
Drug Addiction Original 51 Slides
 
Passing stuctures to function
Passing stuctures to functionPassing stuctures to function
Passing stuctures to function
 
Basic general knowledge
Basic general knowledgeBasic general knowledge
Basic general knowledge
 
Dil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shamsDil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shams
 
Pathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-completePathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-complete
 
Husne akhlaq
Husne akhlaqHusne akhlaq
Husne akhlaq
 
Parts of speech sticky note definitions and examples
Parts of speech sticky note definitions and examplesParts of speech sticky note definitions and examples
Parts of speech sticky note definitions and examples
 
Quik tips
Quik tipsQuik tips
Quik tips
 
Binary
BinaryBinary
Binary
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Structure

  • 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