Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

CHAPTER TWO 1

STRUCTURE
IN C++
MEKONEN M.
content 2
CONTENT
• Structure
• Array of structure
• Nested structure
• Union
• Enumeration
• typedef

2
Structure 3
Contents Introduction
• A simple variable can store only one value at a time.
structure • A structure is a collection of simple variables.
Array of structure
• The variables in a structure can be of different data types that can be referenced
Nested structure with single name: Some can be int, some can be float, and so on.
Union • The structures are used to join simple variables together to form a bit complex
Enumeration variables.
typedef • The data items in a structure are called the members, elements or fields of the
structure.
• However, a structure may consist of different data types. The user can define a
new data type that may contain different types of data with the help structures.
• The difference between an array and a structure is that array contains set of
variables of same data type. However, a structure may consist of different data
types.
• The user can define a new data type that may contain different types of data
with the help structures. 3
Structure 4
Contents Declaration of structures
• In A structure is declared by using C++ keyword struct followed by the
structure structure name followed by a pair of curly braces {}.
Array of structure • The structure members are defined with their data-type inside the opening and
Nested structure closing curly braces.
Union • The closing brace is ended with a semicolon. The declaration tells the compiler
Enumeration about the details of the structure. Keyword “struct”
typedef • Syntax Struct Person Structure name
struct structName
{ or tag
{
dataType1 identifier1; Char name[15]

structure members
Braces delimit
dataType2 identifier2; :
dateTypeN identifierN;
int age
Members
}; float salary
• Note: No memory is set a }; Semicolon
side for this structure terminates
4
definition
Structure variable 5
Contents Defining a structure variable
• The structure definition tells how the structure is organized: It specifies what members the
structure will have
structure
• The structure variable can be defined after the declaration of a structure.
Array of structure • The process of defining a structure variable is same as defining a variable of basic types such as
Nested structure int and char.
• The definition tells the compiler to allocate memory space for the variable.
Union • The compiler automatically allocates sufficient memory according to the elements of the
Enumeration structure.
• Syntax:
typedef Struct Person
structName Identifier;
Example: (Assuming previously defined struct Person ) {
Person p1; Char name[15] p1
• The structure variable p1 will
occupy bytes in the memory as follows. int age
• char name (2 bytes) float salary
• int age (4 bytes)
• float salary (2 bytes) };
• Total number of bytes p1 contains
in memory is 8 bytes
Person p1;
5
Structure variable 6
Contents Accessing member of structure variable
structure • Any member of a structure variable can be accessed by using dot operator
Array of structure or member access operator(.).
Nested structure • The name of the structure variable is written on the left side of the dot
Union operator.
Enumeration • The name of member variable is written on right side of the dot operator.
typedef • Syntax:
StructureVariable.MemberVariable;
Example:
Person person1;
Person person2;
Person person3;
person.name = “Abebe”; 6
Structure example 7
#include<iostream>
Contents Using namespace std;
/*******Structure definition*******/
structure struct person //declare a structure
{
Array of structure
char name[15]; //name of the person
Nested structure int age //age of the person
float salary //salary of the person
Union };
Enumeration /******* End of Structure *******/
int main()
typedef {
//define a structure variable
person person1;
//give values to structure members
person1.name= “Abebe”;
person1.age= 23;
person1.salary= 10000;
//display structure members
cout <<“Name: "<< person1.name;
cout <<"\n Age: " << person1.age;
cout <<"\n Salary :" << person1.salary << endl;
return 0;
} 7
Structure variable 8
Contents Initializing the structure variable
• The process of assigning values to structure elements is called initialization of
structure Structure variables.
Array of structure • The assignment operator = is used for initialization.
Nested structure • The declaration of structure variable is specified on the left side of assignment
Union operator.
Enumeration • The values for initialization are written on the right side of assignment
typedef operator surrounded by the curly braces.
• The values are written in the same sequence in which they are specified in
structure declaration. Struct Person
• Each value is separated by comma. {
• Syntax Char name[15]
structureName Identifier = {val1, val2, …, valN} ; int age
Example float salary
person person1 = { “Abebe”, 23, 10000}; }; 8
Structure variable 9
Contents Initializing structure variable(cont…)
structure • Unlike in the case of arrays, where the whole content of an array could
Array of structure not be copied to another one using a simple statement, in structures, you
Nested structure can assign complete structures to one another by using array notation.
Union • Syntax
Enumeration person person1 = { “Abebe”, 23, 10000};
typedef Person person2=person1 // assign person1 to person2
• The first line declares a structure variable person1 and initializes it.
• The second line declares another structure variable person2 and initializes
it another same structure variable.
• One structure variable can be assigned to another structure variable only
if both are of same type.
9
Array as a members of structure 10
Contents Array within structure
structure • Structure like normal data type, it can also store an array as well.
Array of structure • Assume a structure contain student id and mark of three subject.
Nested structure
struct student
Union
Enumeration {
typedef int idno
int marks[3] //array
}
idno Mark[0] Mark[0] Mark[0]

Marks
10
Array as a members of structure 11
Contents Accessing Array member structure
structure • Structure like normal data type, it can also store an array as well.
Array of structure • The array stored in a structure can be accessed by using the following
Nested structure
1.Name of Structure Variable – in which an array is defined.
Union
2.Dot Operator – It is used to refer the array.
Enumeration
typedef 3.Name of Array
4.Index of desired element – the index is used to access the individual
element of the array.

11
Array as a members of structure 12
Contents int main()
{
structure
student stud;
Array of structure
stud.idno=1234;
Nested structure
Union for(int j=0;j<3;j++)

Enumeration {
stud.marks[j]=1+rand()%100;
typedef
}
Cout<<“Id number:”<<stud.id<<endl;
for(int k=0;k<3;k++)
{
cout<<“mark “<<k<<stud.marks[k]<<endl;
}
return 0;}
12
Array of structure 13
Contents Array of structure
• The structure that contains an array as member variable can be initialized in
structure the same the same way as initializing a simple structure variable.
Array of structure • The values are written in braces and each value is separated by comma.
Nested structure • additionally, the values for the member array are written in nested braces, in
Union proper order.
Enumeration • Example
typedef struct student
{
int idno
int marks[3] //array
}
student stud={1001,{89,90,95}};
13
Array of structure 14
Contents Array of structure
structure
• An array is a collection of same type of data. An array can be of simple data
type such as int, char or float etc. Similarly, an array can be of user-defined
Array of structure type such as a structure.
Nested structure • An array of structure is a type of array in which each element contains a
Union complete structure. Struct Person
Enumeration {
• It can be used to store many records.
typedef Char name[15]
• Example
int age
• The above example declares a structure Person. float salary
• It defines an array pers[10] of structure person. };
• The array can store the records of three person. Person pers[3];
pers[0] pers[1] persa[2]
•1 name age salary name age salary name age salary 14
Array of structure 15
Contents Accessing Array of structure
structure • The array is accessed by using its index.
Array of structure • The structure is accessed by using dot operator.
Nested structure • An array of structures can be accessed as follows:
Union
Enumeration
• Example: Record of First person e.g. pers[0]
typedef pers[0].name=“Alemu”;
Pers[0].age=25;
pers[0].salary=50000;

15
Array of structure 16
Contents Initializing Array of structure
structure
• An array of structures can be initialized at the time of declaration by writing
the values in braces.
Array of structure • The values for each element of the array are written in separate inner braces.
Nested structure
Union
• Example Struct Person
{
Enumeration • The example declares an array of
Char name[15]
typedef structure and initializes it.
int age
• The values are written in braces with the float salary
help of nested braces, separated by commas, };
to refer each book in the Structure Array. Person pers[3]={{“John”, 26,40000},
• Each inner pair of braces is used to initialize {“X”, 36,30000} ,

one element of the array. {“Y”, 26,50000}};


16
Array of structure 17
Contents Initializing Array of structure
structure
• An array of structures can be initialized at the time of declaration by writing
the values in braces.
Array of structure • The values for each element of the array are written in separate inner braces.
Nested structure
Union
• Example Struct Person
{
Enumeration • The example declares an array of
Char name[15]
typedef structure and initializes it.
int age
• The values are written in braces with the float salary
pers[0]

help of nested braces, separated by commas, };


to refer each book in the Structure Array. Person pers[3]={{“John”, 26,40000},
• Each inner pair of braces is used to initialize {“X”, 36,30000} ,

one element of the array. pers[1] {“Y”, 26,50000}};


17
pers[2]
Nested structure 18
Contents Nested structure
• A structure within a structure is known as Nested Structure.
structure • A nested structure is created when the member of a structure is itself a structure.
Array of structure • We can write one structure inside another structure as a member of another structure.
Nested structure • Example
Union struct Date Above example defines two structures Date and
Student.
Enumeration {
The structure Date contains simple member
typedef int dat;
variables day, month, year of data type int.
int month;
Second structure Student contains three member
int year; }; variables. The first two members are of basic data
struct Student type, there is no ambiguity. But the third member
of Structure Student is itself a structure variable.
{
char name[20];
int age;
Date dateOfBirth; }; 18
Nested structure 19
Contents Accessing member of Nested structure
• The member variable of nested structure can be accessed using multiple dot
structure operators.
Array of structure • The first dot operator refers the member variable of outer structure.
Nested structure • The second dot operator refers the inner structure and so on.
Union
• Example ( Previous Slide, we have defined Nested Structure Date & Student )
Enumeration
Student abebe; // abebe further contains a structure
typedef
• We Assign values statically…
Student abebe;
abebe.name = “Abebe";
abebe.age = 25;
abebe.dateOfBirth.day = 23;
abebe.dateOfBirth.month = 3;
abebe.dateOfBirth.year = 1940;
19
Union 20
Union
Contents • Unions are similar to structures in certain aspects.
structure • Unions are used to group together variables of different data types.
Array of structure
• The individual members can be accessed using dot operator.
• The difference between structure and union is in the allocation of memory space.
Nested structure
• A structure allocates total space required for a structure variable. However, a union allocates the space required by
one element that occupies the maximum size.
Union
Enumeration • In structure each member has its own storage location whereas all the members of union uses the same location.

typedef
• The union require the bytes that are equal to the number of bytes required for the largest member.

• Syntax
union union_name
{
member_type1 member_namel;
member_type2 member_name2;
member_type3 member_name3;…
member_typeN member_nameN;
20
};
Union 21
Union
Contents • Unions are similar to structures in certain aspects.
structure • Unions are used to group together variables of different data types.
Array of structure
• The individual members can be accessed using dot operator.
• The difference between structure and union is in the allocation of memory space.
Nested structure
• A structure allocates total space required for a structure variable. However, a union allocates the space required by
one element that occupies the maximum size.
Union
Enumeration • In structure each member has its own storage location whereas all the members of union uses the same location.

typedef
• The union require the bytes that are equal to the number of bytes required for the largest member.

• Syntax
union union_name
{
member_type1 member_namel;
member_type2 member_name2;
member_type3 member_name3;…
member_typeN member_nameN;
21
};
Enumerations 22
Contents Enumerations
• Enumerations are used to create new data types.
structure • An enumeration consists of a list of values.
Array of structure • Each value has a unique number i.e. Integer CONSTANT starting from 0.
Nested structure • An enumeration may contain the values which are different from the values of fundamental
Union data types.
Enumerations • An enum declaration defines the set of all names that will be permissible values of the type.
typedef • These permissible values are called enumerators.
• Syntax
enum identifier { valuel, value2, value3 };
• Example
enum days_of_week {Mond,Tues,Wedn,Thurs, Fri, Sat, Sun};
• The above enumeration does not contain any fundamental data type in the declaration.
• The list of values in braces indicates the possible values that can be stored in a variable of type
days_of_week .
VALUES: Monday = 0, Tuesday = 1,…, Sunday = 6 22
Enumerations 23
Contents Enumerations(cont…)
structure • Each value in an enumeration is always assigned an integer value.
Array of structure • By default, the value starts from 0 and so on. However, the values can be
Nested structure assigned in different way also.
Union
• Example
Enumerations
typedef enum week {Saturday=1,Sunday, Monday,Tuesday,Wednesday,Thursday, Friday);
• The value of Sunday is 2, Monday is 3 and so on..

23
User defined datatypes using typedef 24
Contents Typedef
• It is used to give new name to the structure.
structure
• New name is used for creating instances, passing values to function, declaration
Array of structure etc…
Nested structure
• It provides and alternative name for standard data type. It is used for self
Union documenting the code by allowing descriptive name for the standard data type.
Enumeration • Syntax
typedef typedef existing_datatype new_datatype;
• Example
typedef float money;
• Now, in a program one can use datatype money instead of float
• Therefore, the following statement is valid
money amout;
24
25

You might also like