Structure in C++
Instructor Name: Faiza Khadim
Data Types
C++ programming language which has the ability to
divide the data into different types. The type of a
variable determine the what kind of values it may
take on. The various data types are
• Simple Data type
Integer, Real, Void, Char
• Structured Data type
Array, Strings
• User Defined Data type
Enum, Structures, Unions
Structure Data Type
A structure is a user defined data type that groups
logically related data items of different data types
into a single unit. All the elements of a structure are
stored at contiguous memory locations. A variable of
structure type can store multiple data items of
different data types under the one name. As the data
of employee in company that is name, Employee ID,
salary, address, phone number is stored in structure
data type.
Structures
• A Structure is a collection of related data
items, possibly of different types.
• A structure type in C++ is called struct.
• A struct is heterogeneous in that it can be
composed of data of different types.
• In contrast, array is homogeneous since it can
contain only data of the same type.
Structures
• Structures hold data that belong together.
• Examples:
– Student record: student id, name, major, gender,
start year, …
– Bank account: account number, name, currency,
balance, …
– Address book: name, address, telephone number,
…
• In database applications, structures are called
records.
Structures
• Individual components of a struct type are
called members (or fields).
• Members can be of different types (simple,
array or struct).
• A struct is named as a whole while individual
members are named using field identifiers.
• Complex data structures can be formed by
defining arrays of structs.
struct basics
• Definition of a structure:
struct <struct-type>{
<type> <identifier_list>; Each identifier
<type> <identifier_list>; defines a member
... of the structure.
} ;
• Example:
struct Date {
int day;
The “Date” structure
int month; has 3 members,
int year; day, month & year.
} ;
struct examples
• Example:
struct StudentInfo{
int Id; The “StudentInfo”
int age;
char Gender; structure has 4 members
double CGA; of different types.
};
• Example:
struct StudentGrade{
char Name[15];
char Course[9]; The “StudentGrade”
int Lab[5]; structure has 5
int Homework[3];
int Exam[2]; members of
}; different array types.
struct examples
• Example:
struct BankAccount{
char Name[15];
The “BankAcount”
int AcountNo[10]; structure has simple,
double balance; array and structure
Date Birthday; types as members.
};
• Example:
struct StudentRecord{ The “StudentRecord”
char Name[15];
int Id; structure has 4
char Dept[5]; members.
char Gender;
};
struct basics
• Declaration of a variable of struct type:
<struct-type> <identifier_list>;
• Example:
StudentRecord Student1, Student2;
Student1 and Student2 are variables of StudentRecord type.
Name Name
Student1 Id Gender Id Gender Student2
Dept Dept
Structure
• The members of a struct type variable are
accessed with the dot (.) operator:
<struct-variable>.<member_name>;
#include<iostream>
struct stdudent{
Example
int roll;
char name[30];
char dept[20];
char gender[10];
}stu={101, "umer","cs","male"};
int main()
cout<<"your roll number is:\n"<<stu.roll;
cout<<"your name is :"<<stu.name;
cout<<"\nyour dept is:"<<stu.dept;
cout<<"\nyour gender is:"<<stu.gender;
return 0;
}
struct student{
int roll;
char name[30];
Example
char dept[20];
char gender[10];
}stu={101, "umer","cs","male"};
int main(){
struct student stu2;
cout<<"your roll number is:\n"<<stu.roll;
cout<<" your name is :"<<stu.name;
cout<< "\nyour dept is:"<<stu.dept;
cout<< "\nyour gender is:"<<stu.gender;
cout<<endl<<"enter your roll number:";
cin>>stu2.roll;
cout<<"enter your name";
cin>>stu2.name;
// cin.get(stu2.name,30); //due to space handling
return 0;
#include<iostream>
struct student{
int roll;
char name[20];
Array within structure
int marks[5]; //output
}; cout<<"roll number is:“<<stu2.roll;
int main() cout<<"\nname is: “<<stu2.name;
{ for(i=1;i<=5;i++)
struct student stu2; {
int i, sum=0; cout<<"\nsubject :\n“<<stu2.marks[i]);
}
cout<<"enter your roll number:\n";
cout<<"total marks are:“<<sum;
cin>>stu2.roll;
cout<<"enter your name"; }
cin>>stu2.name;
cout<<"enter your marks of five
subjects");
for(i=1; i<=5;i++)
{
cout<<"subject“<<i;
cin>>stu2.marks[i]);
sum=sum+stu2.marks[i];
Array of structure
• We can make array of structure type variable
0 1 2 … 98 99
• An ordinary array: One type of data
0 1 2 … 98 99
• An array of structs: Multiple types of data in
each array element.
#include<iostream>
using namespace std;
struct student{ Example //print values for 5 students
int roll;
cout<<"detail for students are";
char name[20];
for(i=0;i<5;i++)
int marks[5]; {
}; cout<<endl<<"detail for
int main() student"<<i+1;
{ cout<<"\nroll num
"<<stu2[i].roll;
struct student stu2[5]; cout<<"\nname is
int i, sum=0; "<<stu2[i].name;
cout<<"add detail for five students"; }
for(i=0;i<5;i++)
}
{
cout<<"enter your roll number
\n"<<i+1;
cin>>stu2[i].roll;
cout<<endl<<"enter your name";
cin>>stu2[i].name;
}
Passing Structure to Function
int main ( )
{
struct employee e1;
cout<<“Enter the employee id of employee”;
cin>>e1.emp_id;
cout<<“Enter the name of employee”;
cin>>e1.name;
cout<<“Enter the salary of employee”;
cin>>e1.salary;
printdata (e1); //function calling struct variable
as argument
}
Passing Structure to Function
void printdata( struct employee emp)
{
cout<<“\nThe employee id of employee is :
%d”<< emp.emp_id;
cout<<“\nThe name of employee is :”<<
emp.name;
cout<<“\nThe salary of employee is : ”<<
emp.salary;
}
Structure
• In the structure all the
members are accessed at any
point of time
• The memory occupied by
structure variable is the sum of
sizes of all the members.
• The elements of a structure
are stored at contiguous
memory locations.
• Separate space for each
memory
Application of Structures
Structure is used in database management to
maintain data about books in library, items in store,
employees in an organization, financial accounting
transaction in company. Beside that other
application are
1) Changing the size of cursor.
2) Clearing the contents of screen.
3) Drawing any graphics shape on screen.
4) Receiving the key from the keyboard.
Application of Structures
5) Placing cursor at defined position on screen.
6) Checking the memory size of the computer.
7) Finding out the list of equipments attach to
computer.
8) Hiding a file from the directory.
9) Sending the output to printer.
10) Interacting with the mouse.
11) Formatting a floppy.
12) Displaying the directory of a disk.
Summary
• A structure is a user defined data type that
groups logically related data items of different
data types into a single unit.
• The elements of a structure are stored at
contiguous memory locations.
• The value of one structure variable is assigned to
another variable of same type using assignment
statement.
• An array of variables of structure is created.
• A variable of structure type is defined as a
member of other structure type called nested
structure.