Structure in C++
Structure in C++
Ž •
Д İ
Я v
P
Structure in C++Æ
и
α Khadim
Instructor Name:ตFaiza
คใ
ℓ
รц
α D
м м
H α
ตυ
Data Types
• •
Ž •
İ
C++ programming language which has the ability to
Д
v
divide the data into different types. The type of a
Я
P
take on. The various data types are Æ
variable determine the what kind of values it may
α и
• Simple Data type
ใ ต
ℓค
Integer, Real, Void, Char
รц
• Structured Data type
α D Array, Strings
м м
α
• User Defined Data type
H
ต υ Enum, Structures, Unions
Structure Data Type
• •
Ž •
A structure is a user defined data type that İgroups
Д
logically related data items of different vdata types
into a single unit. All the elements ofÆ
Я
P 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
α D
of employee in company that is name, Employee ID,
м м
salary, address, phone number is stored in structure
H α
υ
data type.
ต
• •
Structures
Ž •
Д İ
Я v
• A Structure is a collection of related data
P Æ
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, D
array is homogeneous since it can
м м
α
contain only data of the same type.
υ H
ต
• •
Structures
Ž •
Д İ
Я
• Structures hold data that belong together. v
• Examples: Æ
P major, gender,
start year, …
α и
– Student record: student id, name,
ใ ต
– Bank account: account number, name, currency,
balance, …
ℓ ค
ร ц
– Address book: name, address, telephone number,
…
α D
м м
• In database applications, structures are called
α
records.
H
ต υ
• •
Structures
Ž •
Д İ
Я v
• Individual components of a struct type are
called members (or fields).
P Æ
α и
• Members can be of different types (simple,
array or struct).
ใ ต
ℓค
รц
• A struct is named as a whole while individual
D
members are named using field identifiers.
α
м
• Complex data structures can be formed by
м
α
defining arrays of structs.
H
ต υ
• •
struct basics
Ž •
Д İ
• Definition of a structure: Я v
struct <struct-type>{ Æ
PEach identifier
<type> <identifier_list>;
α и
ต
<type> <identifier_list>; defines a member
ใ
... of the structure.
ℓค
} ;
• Example: รц
α D
м
struct Date {
The “Date” structure
м
int day;
α
int month; has 3 members,
H
int year; day, month & year.
ต υ} ;
• •
struct examples
Ž •
Д İ
• Example:
Я v
struct StudentInfo{
int Id; TheP Æ
“StudentInfo”
int age;
char Gender;
α и
structure has 4 members
double CGA;
ใ ต of different types.
ℓค
};
• Example:
รц
α D
struct StudentGrade{
char Name[15];
м
char Course[9]; The “StudentGrade”
αм
int Lab[5];
int Homework[3];
structure has 5
H
members of
υ
int Exam[2];
ต
}; different array types.
• •
struct examples
Ž •
Д İ
• Example:
Я v
struct BankAccount{
char Name[15]; Æ
P has simple,
The “BankAcount”
int AcountNo[10];
α и
structure
ต
double balance; array and structure
ใ
Date Birthday; types as members.
ℓค
};
• Example: รц
α D
struct StudentRecord{ The “StudentRecord”
м м
char Name[15];
int Id; structure has 4
υ
char Gender;
ต
};
• •
struct basics
Ž •
Д İ
<struct-type> <identifier_list>; Я
v
• Declaration of a variable of struct type:
• Example: P Æ
α и
StudentRecord Student1, Student2;
ใ ต
ค
Student1 and Student2 are variables of StudentRecord type.
ℓ
รц
Name Name
Student1
α DId Gender Id Gender Student2
м м Dept Dept
H α
ต υ
• •
Structure
Ž •
Д İ
Я v
• The members of a struct type variable are
Æ
accessed with the dot (.) operator:
P
α и
<struct-variable>.<member_name>;
ใ ต
ℓค
รц
α D
м м
H α
ต υ
#include<iostream>
• •
•
struct stdudent{
Example
İŽ
Д
int roll;
char name[30];
char dept[20];
Я v
char gender[10];
P Æ
}stu={101, "umer","cs","male"};
α и
int main()
ใ ต
ℓค
รц
cout<<"your roll number is:\n"<<stu.roll;
cout<<"your name is :"<<stu.name;
α D
cout<<"\nyour dept is:"<<stu.dept;
м
cout<<"\nyour gender is:"<<stu.gender;
α
return 0; м
}
υ H
ต
struct student{
• •
int roll; Example
Ž •
char name[30];
Д İ
v
char dept[20];
char gender[10];
Я
}stu={101, "umer","cs","male"};
P Æ
и
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;
D
cout<< "\nyour gender is:"<<stu.gender;
α
cout<<endl<<"enter your roll number:";
м
м
cin>>stu2.roll;
α
cout<<"enter your name";
H
υ
cin>>stu2.name;
//
ต cin.get(stu2.name,30); //due to space handling
return 0;
#include<iostream>
struct student{
• •
int roll;
Array within structure
Ž •
İ
char name[20];
int marks[5]; //output
v Д
Я
}; cout<<"roll number is:“<<stu2.roll;
int main() cout<<"\nname is: “<<stu2.name;
{
P Æ
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;
α D
cout<<"enter your marks of five
м
subjects");
м
for(i=1; i<=5;i++)
{
H α
υ
cout<<"subject“<<i;
ต
cin>>stu2.marks[i]);
sum=sum+stu2.marks[i];
• •
Array of structure
Ž •
Д İ
v
• We can make array of structure type variable
Я
P Æ
α и
0 1 2
ใ ต
… 98 99
ℓ ค
• An ordinary array: One type of data
รц
α D
м м
0 1 2 … 98 99
•υAn
α
Harray of structs: Multiple types of data in
ต each array element.
•
#include<iostream>
using namespace std;
Example • •
Ž
struct student{
int roll;
Д
//print values for 5 students
İ
v
cout<<"detail for students are";
char name[20];
Я
for(i=0;i<5;i++)
int marks[5];
Æ
{
P
}; 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++)
D
}
{
мα
cout<<"enter your roll number
м
\n"<<i+1;
H α cin>>stu2[i].roll;
υ
cout<<endl<<"enter your name";
ต
cin>>stu2[i].name;
}
Passing Structure to Function
• •
Ž •
int main ( )
Д İ
{
Я v
struct employee e1;
P Æ
cout<<“Enter the employee id of employee”;
cin>>e1.emp_id; α и
ใ ต
ℓค
cout<<“Enter the name of employee”;
รц
cin>>e1.name;
D
cout<<“Enter the salary of employee”;
α
м
cin>>e1.salary;
м
α
printdata (e1); //function calling struct variable
H
}ต υ
as argument
Passing Structure to Function
• •
Ž •
void printdata( struct employee emp)
Д İ
{
Я v
Æ
cout<<“\nThe employee id of employee is :
P
и
%d”<< emp.emp_id;
ตα
cout<<“\nThe name of employee is :”<<
ใ
ℓค
emp.name;
รц
cout<<“\nThe salary of employee is : ”<<
D
emp.salary;
α
}
м м
H α
ต υ
Function Returning Structure
• •
•
Žtype
Д İ
The function can return a variable of structure
Я v
like a integer and float variable. The program to
return a structure from function. Æ
P
struct person
α и
ใ ต
{
char name[20];ц ℓ ค
int age; D
ร
м α
м
float salary;
}; Hα
ต υ
• •
Function Returning Structure
Ž •
Д İ
struct person getdata();
Я v
Æ
void main()
{
P
α и
struct person p;
ใ ต
ℓค
p=getdata();
รц
cout<<displaying information";
cout<<“name”<<p.name;
α D
cout<<"age”<<p.age;
м
cout<<“salary“<<p.salary;
м
α
getch();
}
υ H
ต
Function Returning Structure
• •
Ž •
struct person getdata()
Д İ
{
Я v
struct person p1;
P Æ
cout<<"name";
α и
cin>>p1.name;
ใ ต
ℓค
cout<<"age";
cin>>p1.age;
รц
D
cout<<"salary";
α
м
cin>>p1.salary;
м
H α
return p1;
}
ต υ
Union Data Type
• •
Ž •
İ
• A union is a user defined data type like structure. The
Д
unit. Я v
union groups logically related variables into a single
P Æ
• The union data type allocate the space equal to
α и
space need to hold the largest data member of
union. ใ ต
ℓค
รц
• The union allows different types of variable to share
D
same space in memory.
α
м
• There is no other difference between structure and
м
α
union than internal difference. The method to
H
ต υ
declare, use and access the union is same as
structure.
• •
Application of union in C
Ž •
Д İ
Я v
• Unions can be used when no more than one
P Æ
member need be accessed at a time. That
и
way, you can save some memory instead of
α
using a struct.
ใ ต
ℓค
รц
α D
м м
H α
ต υ
Defining of Union
• •
Ž •
İ
A union has to defined, before it can used. The
syntax of defining a structure is
vД
Я
union <union_name>
P Æ
{
α и
ใ ต
<data_type> <variable_name>;
ℓ ค
ร ц
<data_type> <variable_name>;
……..
α D
м м
<data_type> <variable_name>;
}; Hα
ต υ
Example of Union
• •
Ž •
The union of Employee is declared as
Д İ
Я v
union employee
P Æ
{
α и
int emp_id;
ใ ต
char name[20];
ℓค
float salary;
รц
α D
char address[50];
м м
int dept_no;
H α
int age;
ต
};υ
Memory Space Allocation
• •
8000
Ž •
emp_id, dept_no, age
Д İ
v
8002
Я
salary
Æ
8004
name
P
α и
ต
8022
ค ใ
ℓ
address
รц
D
8050
мα
α м
υH employee
ต
• •
•
#include<isotream>
Example
İŽ
union student
Д
{
v
int roll; //2byte
Я
char name[10]; //10byte
}stu1;
P Æ
int main()
α и
ต
{
ใ
ℓค
cout<<“enter your roll";
รц
cin>>stu1.roll;
cout<<“enter your name“;
D
cin>>stu1.name;
мα
cout<<"details are“;
cout<<"roll number“<<stu1.roll; //unexpected
м
cout<<"name is“<<stu1.name;
α
υ H
getch();
ต
}
Difference between Structures & Union
• •
Ž •
Д İ
Structure union
Я v
• In the structure all the
P Æ
• In union only one of union
и
members are accessed at any member can be accessed at
point of time
ตα any given time.
• The memory occupied by
ใ • Memory occupied by union
ℓค
structure variable is the sum of variable is equal to space
รц
sizes of all the members. hold by the largest data
member of a union.
D
• The elements of a structure
α
are stored at contiguous
м
• Share memory and Overlap
values
м
memory locations.
α
• Separate space for each
H
• Size of memory depends on
υ
memory larger data type
ต
• •
Ž •
Д İ
Structure
Я v
• In the structure all the
P Æ
и
members are accessed at any
point of time
ตα
• The memory occupied by
ใ
ℓค
structure variable is the sum of
รц
sizes of all the members.
D
• The elements of a structure
α
are stored at contiguous
м
м
memory locations.
α
• Separate space for each
H
υ
memory
ต
Application of Structures
• •
Ž •
Structure is used in database management to
Д İ
Я v
maintain data about books in library, items in store,
P Æ
employees in an organization, financial accounting
α и
ต
transaction in company. Beside that other
ใ
application are
ℓค
รц
1) Changing the size of cursor.
α D
м
2) Clearing the contents of screen.
α м
3) Drawing any graphics shape on screen.
υ H
4) Receiving the key from the keyboard.
ต
Application of Structures
• •
Ž •
Д İ
5) Placing cursor at defined position on screen.
Я v
6) Checking the memory size of the computer.
P Æ
7) Finding out the list of equipments attach to
α и
computer.
ใ ต
ℓค
8) Hiding a file from the directory.
รц
9) Sending the output to printer.
α D
м
10) Interacting with the mouse.
α м
11) Formatting a floppy.
υ H
12) Displaying the directory of a disk.
ต
Summary
• •
• A structure is a user defined data typeİŽthat •
groups logically related data items of Д
data types into a single unit. Я v different
ต α
ค ใ
• The value of one structure variable is assigned to
ц ℓ
another variable of same type using assignment
statement.
ร
D
• An array of variables of structure is created.
• A variable αof structure type is defined as a
м м
H α
member
structure.
of other structure type called nested
ต υ