Structures in C++
Dr. Muhammad Ahsan Ansari
User Defined Data Types
Class
Member of class:
Variables
Functions
Structure
Member of Structure
Variables
Problem with Array
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.
Structure is a collection of variables of different data
types under a single name. it is similar to a class in
that, both holds a collection of data of different data
Problem without using structure
So far we have only used data types which have been
defined by C++ such as int, double and char etc.
It is possible to create our own data types.
A user defined data types is called a structure, class etc.
A structure can contain both in data types and another
structure.
The concept of structure is pretty much the same as
arrays except that in an array, all the data is of the same
types but in a structure, the data can be of different types.
Problem without using structure
For example: you want to stroe some information about
a person: his/her name, citizenship number and salary.
You can easily create different variables name, cityNo,
salary to store these information separately.
However, in the future, you would want to store
information about multiple persons.
It would be big mess!
A better approach will be to have a collection of all
related information under a single name Person, and use
it for every person.
What a structure is?
“A structure is a collection of variables under a
single name. these variables can be of different
types, and each has a name that is used to select it
from the structure”
There is always a requirement in most of our
data processing applications that the relevant data
should be grouped and handles as a group.
In structure, we introduce a new data type.
What a structure is?
A structure can contain any data types including array
and another structure as well.
It provides a simple method of abstraction and grouping
Each variable declared inside structure is called member
of structure.
A structure can be assigned to, as well as passed to and
returned from functions
Structures are declared using the keyword strict.
What a structure is?
What a structure is?
What a structure is?
Steps to Create Structure
Declare Structure
Initialize Members of Structure
Access Structure Elements
Declaration of a structure
1. Declare Structure
• Struct keyword is used for creating a structure.
2. Initialize Members of Structure
• By Struct keyword
• By declaring variable at the time of defining
structure.
Declaration of a structure
1. Declare Structure
• Struct keyword is used for creating a structure.
2. Initialize Members of Structure
• By Struct keyword
• By declaring variable at the time of defining
structure.
Declaration of a structure
1. 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}.
2. Finally, the closed braces end with a
semicolon denoted as; following the
statement. The above structure declaration
Declaration of a structure
• Structures are syntactically declare with:
• Keyword struct
• Followed by the name of the structure
• The data, contained in the structure, is
defined in the curly braces
• All the variables that we have been using
can be part of structure
Declaration of a structure
• Syntax:
struct type_name
{
member_type1 member_name1;
member_type1 member_name1;
member_type1 member_name1;
.
.
.
}
Approach 1 (Declaration of a structure)
Note: Memory is not allocated at the time of its
declaration. Memory is allocated when we declare
structure variable.
Approach 1 (Declaration of a structure)
Note: Memory is not allocated at the time of its
declaration. Memory is allocated when we declare
structure variable.
Declaring variables of Type struct
• The most efficient method of dealing with
structure variables is to define the structure
globally.
• This tells “the whole world”, namely main and
any functions in the program, that a new data
types exists. To declare a structure globally, place
it BEFORE int main().
• The structure variables can then defined locally
in main, for example.
Declaring variables of Type struct
Declaring variables of Type struct
Accessing Structure Members
• To access any member of a structure, we use
the member access operator { . }.
• The member access operator is coded as a
period between variable name and the
structure member that we wish to access.
• Remember we would use struct keyword to
define variables of structure type.
Accessing Structure Members
• Suppose, you want to access age of
structure variable student1 and assign 50 to
it. We can perform this task by using
following code below:
student1. age = 50;
• Tasking input as:
cin >> student1.age;
Example 1
• C++ Program to assign data to member of a
structure variable and display it.
Example 1
Example 1
Output of the Previous Program
Initializing Structures
Like normal variable structures can be
initialized at the time of declaration.
Initialization of structure is almost similar to
initializing array. The structure object is
followed by equal sign and the list of values
enclosed in braces and each value is separated
with comma.
Example
Person p1 = {“Adil Aslam”, 20, 1000}
Initializing Structures (1st Way)
Initializing Structures (2nd Way)
Example (Initializing Structures)
Output of the Previous Program
Structure Variable in Assignment Statement
S1 = S2;
The statement assigns the value of each
member of S2 to the corresponding member of
S1. Note that one structure variable can be
assigned to another only when they are of the
same structure type, otherwise compiler will
give an error.
Problem Statement
• Write a program which declares two variables of a structure
and copies the contents of first variable into the second
variable.
Solution
Solution
Output of the Previous Program