Lesson 4

You might also like

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

Lesson 4: Structure data type

4.1. Introduction
A structure is a data structure used to package several related variables into one unit.
The size of the structure is equal to the total size of all members.
4.2. Lesson objectives
By the end of this lesson the learner will be able to
• Define and explain applications of structure data type
• Explain advantages and disadvantages of structure
• Declare and use structure data type
4.3. Lesson outline
This lesson is structured as follows:
4.1. Introduction
4.2. Lesson objectives
4.3. Lesson outline
4.4. Declaration of structure
4.5. Access structure members
4.6. Using structure members
4.7. Revision questions
4.8. Summary
4.9. Suggested reading

4.4. Declaration of structure The structure is declared as

struct structure_name//declaring structure


{
follows: struct:
Typ1 member1; Type
2 member2; ….
The keyword for } object1, object2,..; //declaring structure

declaring a structure data structure. structure_name: The

identifier of the structure data structure.


Type: The data type of a given structure member.
Member: The identifier of a structure member.
{}: Brackets to enclose structure member declarations.
Object(s): Instances of structure structure(variables).
4.5. Accessing structure members:
Use dot notation Syntax:
Object_name.member;

Example: mary.age;

4.6. Using structure members


Once declared, the structure members can be used in the program just like any other
variable.
Example: Program to enter name, address,age and salary and display the results using
structure.
#include <iostream>
#include<string> using
namespace std;
//declares a structure of type Employee struct
employee
{ string name[2]; //declare member name
string address; //declare member address
int age; //declare member age
double salary; //declare member salary
}emp1,emp2; //declare variable emp1 and emp2(objects)
int main()
{
cout<<"Please do not press space bar"<<endl;
cout<<"Enter the name of the first employee:"<<endl;
//accessing members
cin>>emp1.name[0];
cout<<"Enter the name of the second employee:"<<endl;
cin>>emp2.name[1];
cout<<"Enter the address of the first employee:"<<endl;
cin>>emp1.address;
cout<<"Enter the address of the second employee:"<<endl;
cin>>emp2.address;
cout<<"Enter the age of the first employee:"<<endl;
cin>>emp1.age;
cout<<"Enter the age of the second employee:"<<endl;
cin>>emp2.age;
cout<<"Enter the salary of the first employee:"<<endl;
cin>>emp1.salary;
cout<<"Enter the salary of the second employee:"<<endl;
cin>>emp2.salary;
cout<<"\n-----------------------------------------"<<endl;
cout<<"Name"<<" :"<<"Age"<<":"<<"Address"<<" :
"<<"Salary"<<endl; cout<<emp1.name[0]<<":
"<<emp1.age<<":"<<emp1.address<<":
"<<emp1.salary<<endl;
cout<<emp2.name[1]<<":"<<emp2.age<<":"<<emp2.address<<":
"<<emp2.salary<<endl; return
0;
}

Advantages:
• They are easy to program
• They are easy to access at the same time
• Different variables can be declared under one name.
Disadvantages:
• Wastage of memory space
• All members are public by default
4.7. Revision questions
a)Explain two advantages and two disadvantages of structures.
b) Write a c++ program that uses structure as follows; create structure employee
whose members are age, height, weight and gender. Create objects
Henry,Charles for the structure.
4.8. Summary
In this lesson we have learnt about structure data structure. We have observed that the
structure allows several types of variables to use the same identifier. This strategy
ensures the variables are easy to access. However, we did observe that this structure
leads to wastage of memory space and that the members can be accessed publicly.
4.9. Suggested reading
[1]. Data structures using C and C++, 2nd Edition by Yedidyah Langsam, Aaron
J.Augenstein and Aaron M.Tenebaum: Pubslisher: Pearson.
[2]. Data structures and algorithms in c++ by Michael T.Goodrich,Robertio
Tamassia and David Mount: Publisher: Wiley
[3]. Fundamentals of data structures in c++ by Ellis Horowitz,Sartaj Sahni and
Dinesh Mehta.
Publisher:Galgotia
[4]. Introduction to data structures and algorithms with c++ by Glenn W.Rowe .
Publisher: Prentice Hall.

You might also like