SlideShare a Scribd company logo
1 of 31
STRUCTURE 
School of Informatics, Hawassa University 11/28/2011 1
 At the end of this lecture you will able to 
 Realize how to define a structure of your own data type 
using a keyword <struct > 
 Understand how Store large data of different type with 
in a single structure name 
 Identify different ways of structure instance creation 
and initializing structure instance with data members 
 Access individual structure members using dot( . ) 
access operator 
 Use arrays of structure and able to store more records 
 Use structure as a function argument 
School of Informatics, Hawassa University 11/28/2011 2
Only one value will store at a time 
It holds one type no data type diversity 
No possibility to use as a new data type 
 Class Activity 
Why regular Variable? 
Why Array? 
discus by Their: 
It stores large amount of data at a time 
All data are of the same type 
It is not possible to use as a new data type 
 Storage capacity(amount of data store at a time) 
 Diversity of data type 
 Capacity to be new user defined data type 
Why structure? 
(5 Min) 
It stores Large amount of data at a time 
It is a collection of multi- diversified data types 
We can have data type of our own 
It is an instance of full record about one object 
School of Informatics, Hawassa University 11/28/2011 3
 Arrays allow you to define variables that 
combine several data items of the same type 
 But structure is another user defined data type 
which allows you to combine a group of data 
items(members) of different data type and 
length with in a single structure name. 
 Structures are used to represent a record, 
Suppose you want to keep track of your books in 
a library. You might want to track the following 
attributes about each book: 
Title Author 
ISBN Book_No 
School of Informatics, Hawassa University 11/28/2011 4
 To declare structure of your own data type with 
different data member we use keyword <truct> 
 General syntax 
struct Struct_Name { 
type2 DataMember 1; 
type2 DataMember 2; 
type1 DataMember 3; 
School of Informatics, Hawassa University 11/28/2011 5 
}; 
New user Defined Data type 
Keyword Struct 
Data members with 
different Data type
 Struct_Name :- we can use as a user defined 
data type we can create a new object of 
struct_name as following: 
Syntax :- 
We Use as our new data type 
struct_name Obj_Name; 
 We can also create an object of user defined 
structure during structure declaration like: 
struct Struct_Name { 
type2 DataMember 1; 
type2 DataMember 2; 
type1 DataMember 3; 
}stru1,stru2,stru3; 
School of Informatics, Hawassa University 11/28/2011 6
 Create structure called Book which has 
attributes Title, Author, ISBN, Book_No 
Struct Book { 
char Title[50]; 
char Author[20]; 
char ISBN[15]; 
int Book_No; 
School of Informatics, Hawassa University 11/28/2011 7 
}; 
Struct Book { 
char Title[50]; 
char Author[20]; 
char ISBN[15]; 
int Book_No; 
} bookObj;
 As with arrays and variables, structure members 
can also be initialized. This is performed by 
enclosing the values to be initialized inside the 
braces { and } after the structure variable name 
while it is defined. 
Struct Book { 
char Title[50]; 
char Author[20]; 
char ISBN[15]; 
int Book_No; 
} Book1 = {“C++”, ” John”, “12-34-034-234”,12}; 
School of Informatics, Hawassa University 11/28/2011 8
 The Second way to initialize structure 
Struct Book { 
char Title[50]; 
char Author[20]; 
char ISBN[15]; 
int Book_No; 
} ; 
int main() 
{ 
Book Book1 = {“C++”, ” John”, “12-34-034-234”,12}; 
School of Informatics, Hawassa University 11/28/2011 9 
}
 Structure members can’t access directly as a 
normal local or global variables … 
 To access any member of a structure, we use the 
member access operator either called dot (.) 
operator for non pointer structure variable or 
arrow operator () for pointer structure 
variables. The member access operator is coded 
as a dot between the structure variable name and 
the structure member that we wish to access. 
School of Informatics, Hawassa University 11/28/2011 10
 Syntax 
struct_var.Member1=//some operation; 
 Example 
Struct Book 
{ 
char Title[50]; 
char Author[20]; 
char ISBN[15]; 
int Book_No; 
School of Informatics, Hawassa University 11/28/2011 11 
}Book1; 
Assign Individual Member 
Book1 .Title= “C++”; 
Book1.Author= ”John”; 
Book1.ISBN= “12-34-034-234” 
Book1.Book_No=12;
? 
Example :1 
Create structure called Books which have 
four data members Title, Author, ISBN, 
Book_No that used to store three different 
Books and display all Book you initialize from 
the initialization when you instantiate an 
instance of structure Books 
School of Informatics, Hawassa University 11/28/2011 12
#include<iostream> 
using namespace std; 
struct Books { 
char Title[50]; 
char Author[25]; 
char ISBN[15]; 
int Book_No; }; 
int main() { 
Books Book1 = {"Java","Johe","123-3243-2312",1}; 
Books Book2 = {"C++","Johe","123-3243-2313",3}; 
Books Book3 = {"C#","Johe","123-3243-2314",2}; 
cout<<"tTitle tAuthor tISBN ttBook Number"<<endl; 
cout<<"t................................................................"<<endl; 
cout<<"t"<<Book1.Title<<"t"<<Book1.Author<<"t“ 
Initializing 3 Books 
With 4 data members 
for each book 
<<Book1.ISBN<<"t"<<Book1.Book_No<<endl; 
cout<<"t"<<Book2.Title<<"t"<<Book2.Author<<"t“ 
<<Book2.ISBN<<"t"<<Book2.Book_No<<endl; 
cout<<"t"<<Book3.Title<<"t"<<Book3.Author<<"t“ 
<<Book3.ISBN<<"t"<<Book3.Book_No<<endl; 
system("pause"); 
return 0; 
} 
School of Informatics, Hawassa University 11/28/2011 13 
•Printing 3 books 
data by using dot 
operator access 
operator 
Structure declaration and 
member definition
Why Arrays of Structure? 
 Arrays of a structure is a collection of structure 
 A single structure instance will represent a single 
record at a time. If we need to store large 
amount of record in a single structure we need 
to create many instances of a give structure and 
this is inefficient to manage large record 
 It is possible to work by creating individual 
structure variable for small number of records 
which is less than 10 record but … 
 In order to manage large record of same 
category we need to develop an arrays of 
structure which is an array stores different types 
of structure member variables 
School of Informatics, Hawassa University 11/28/2011 14
 Image you have structure Books with attribute 
Title, Author, ISBN, Book_No and you need to 
store 1000 Books with their corresponding 
attribute 
Is it easy to create 1000 Books Variable? 
 It is not easy! so what is the solution? … 
1. We can solve this using arrays of structure … 
2. Lets compare and contrast individual structure 
variable creation and array usage 
School of Informatics, Hawassa University 11/28/2011 15
Struct Books 
{ 
char Title[50]; 
char Author[20]; 
char ISBN[15]; 
int Book_No; 
}Book1, Book2, Book3, Book4 …Book1000; 
Individual 
structure variable 
Using arrays of structure 
Struct Books 
{ 
char Title[50]; 
char Author[20]; 
char ISBN[15]; 
int Book_No; 
} BookObj[1000]; 
School of Informatics, Hawassa University 11/28/2011 16
 To access individual elements of arrays of 
structure we need to specify the index number 
with in a square bracket of the structure 
instance like simple array operation 
 Assume that you have structure as follow 
Struct Books 
{ 
char Title[50]; 
char Author[20]; 
char ISBN[15]; 
int Book_No; 
} BookObj[10]; 
How to access the first 
three books from your book 
storage structure? 
School of Informatics, Hawassa University 11/28/2011 17
Book one information Book Two information Book Three information 
BookObj[0].Title 
BookObj[0].Author 
BookObj[0].ISBN 
BookObj[0].Book_No 
BookObj[1].Title 
BookObj[1].Author 
BookObj[1].ISBN 
BookObj[1].Book_No 
BookObj[2].Title 
BookObj[2].Author 
BookObj[2].ISBN 
BookObj[2].Book_No 
Title 
Author 
ISBN 
Book_No 
BookObj[0] 
Title 
Author 
ISBN 
Book_No 
BookObj[1] 
Title 
Author 
ISBN 
Book_No 
BookObj[2] 
School of Informatics, Hawassa University 11/28/2011 18
 Structure can pass into function as an argument 
rather than passing individual elements it is 
better to pass the whole of structure 
 We can pass structure either by reference or by 
value but it is recommended to pass structure by 
their reference because to minimize the cost of 
copying the value 
 Its default value of passing structure is by nature 
uses by reference 
School of Informatics, Hawassa University 11/28/2011 19
#include<iostream> 
using namespace std; 
struct Time 
{ 
int h, m, s; 
}; 
int ToSecond(Time now){ 
return (now.h*60*60 + now.m*60 + now.s); 
School of Informatics, Hawassa University 11/28/2011 20 
}i 
nt main(){ 
Time T; 
cout<<"Enter Time In Hour, Min and Second:"; 
while( cin>>T.h>>T.m>>T.s) 
{ 
cout<<"Total Second:"<<ToSecond(T); 
} 
system("pause"); 
return 0; 
} 
Example 
Passing structure time T
? Class activity 
 Create a structure called Books to hold records 
of 100 Books with Book detail of Title, Author, 
ISBN and Book_No. Your program will accept an 
input from the user and display all Book 
information in table format 
 Assume you will have a basic about 
 Display():- Function to give output 
 Input():- Function to accept input from keyboard 
 Main():- To call an input() function 
 Structure definition:-To declare all members 
School of Informatics, Hawassa University 11/28/2011 21
#include<iostream> 
using namespace std; 
struct Books 
{ 
char Title[50]; 
char Author[20]; 
char ISBN[15]; 
int Book_No; 
}; 
const int size = 100; 
void display(Books Book[size], int n) 
{ 
cout<<"Title"<<"t"<<"Author"<<"t“ 
<<"ISBN"<<"t"<<"Book No"<<endl; 
for(int i = 0; i < n ; i++) { 
cout<<Book[i].Title<<"t"<<Book[i].Author<<"t" 
<<Book[i].ISBN<<"t"<<Book[i].Book_No<<"t" 
<<endl; 
} 
} 
School of Informatics, Hawassa University 11/28/2011 22 
void input() 
{ 
Books Book[size]; 
cout<<"nEnter No of Books:"; 
int n; cin>>n; 
for(int i = 0;i < n ; i++) { 
cout<<"nEnter Title:“; cin>>Book[i].Title; 
cout<<"nEnter Author:“; cin>>Book[i].Author; 
cout<<"nEnter ISBN:“; cin>>Book[i].ISBN; 
cout<<"nEnter Book No:"; cin>>Book[i].Book_No; 
} display(Book,n); 
} 
int main() 
{ 
input(); 
system("pause");; 
return 0; 
} 
Input function 
(initialize struct) 
Output function 
Structure declaration 
Main function
 Structures can be implemented as the member of 
other structure. 
 This is termed as structure within structure we 
call it nested structure 
 Why we use nested structure? 
 To organize multi valid attribute as a single value 
 To eliminate data redundancy in different structures 
We collect similar data members of two or more 
structure with in a single structure name 
 To minimize coding time 
School of Informatics, Hawassa University 11/28/2011 23
struct date{ 
int day; 
int month; 
int year; 
}; 
struct Employee { 
char name[40]; 
long int Emp_ID; 
char sex[5]; 
date dob; 
Example: Using nested structure 
to manipulate multi valid attribute 
Example: Date of Birth has 3 
integer values to manipulate it 
we use Date as nested struct in 
Employee struct 
School of Informatics, Hawassa University 11/28/2011 24 
}; 
Using date structure as Member 
of Employee structure
 Assume you have struct Employee and Customer 
with their data members as following 
struct Employee{ 
string Name; 
string EID; 
int age; 
String Contact_Name; 
string Kebele; 
string Woreda; 
string PhoneMobil; 
string PhonHome; 
School of Informatics, Hawassa University 11/28/2011 25 
}; 
struct Customer{ 
string Name; 
string CustID; 
int age; 
string Kebele; 
string Woreda; 
string PhoneMobil; 
string PhonHome; 
};
 From the above structure definition address 
information is redundant both for Employee and 
customer structure 
 So to eliminate this data redundancy we 
separate address information in different 
structure name called address and both customer 
and employee will inherit from address structure 
School of Informatics, Hawassa University 11/28/2011 26
struct Employee 
{ 
string Name; 
string EID; 
int Empage; 
Address EmpAddress; 
School of Informatics, Hawassa University 11/28/2011 27 
}; 
struct Customer 
{ 
string Name; 
string CustID; 
int Custage; 
Address CustAddress; 
}; 
Struct Address 
{ 
String Contact_Name; 
string Kebele; 
string Woreda; 
string PhoneMobil; 
string PhonHome; 
}
 We can initialize a nested structure either by 
putting all elements in order of one structure or 
as block of data members 
Example:- 
Initialize all member of the above nested structure 
definition with Data : Name =Dagim, ID =EMP009, Sex = M, Date of 
Birth = 10-2-1989 
Option 1:- 
Employee Emp ={“Dagim”, “EMP009”,’M’,10,2,1989}; 
Option 2:- 
Employee Emp ={“Dagim”, “EMP009”,’M’,{10,20,1989}}; 
Option 3:- 
Emp.name=“Dagim”; 
Emp.EMP_ID=“EMP009”; 
Emp.sex=‘M’; 
Emp.dob.day=10; 
Emp.dob.month=2; 
Emp.dob.dat=1989; 
School of Informatics, Hawassa University 11/28/2011 28
 To access a nested structure data members we 
will use to dot access operator 
 Example:- 
To access a particular month of Employee birth 
date we can access as follow 
Employee Emp; 
Emp.dob.month ; 
School of Informatics, Hawassa University 11/28/2011 29
#include<iostream> 
using namespace std; 
struct date { 
int day; 
int month; 
int year; 
}; 
struct Employee { 
char First_Name[23]; 
char Last_Name[24]; 
date dob; 
} Emp= {"DEMEKE","BIRHANU",{16 , 2, 2012}}; 
int main() 
{ 
cout<<"ntFirst NametLast NametDate of Birth"<<endl; 
cout<<"nt"<<Emp.First_Name<<"tt"<<Emp.Last_Name<<"tt" 
<<Emp.dob.day<<"-"<<Emp.dob.month<<"-"<<Emp.dob.year<<endl; 
system("pause"); 
return 0; 
} 
School of Informatics, Hawassa University 11/28/2011 30
Thanks ! 
School of Informatics, Hawassa University 11/28/2011 31

More Related Content

What's hot

What's hot (20)

SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
Xpath
XpathXpath
Xpath
 
XPath
XPathXPath
XPath
 
ch9
ch9ch9
ch9
 
Java 1-contd
Java 1-contdJava 1-contd
Java 1-contd
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26
 
بيانات الدولة 1
بيانات الدولة 1بيانات الدولة 1
بيانات الدولة 1
 
Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)Indexing and Query Optimizer (Richard Kreuter)
Indexing and Query Optimizer (Richard Kreuter)
 
XPath Introduction
XPath IntroductionXPath Introduction
XPath Introduction
 
Java misc1
Java misc1Java misc1
Java misc1
 
Xpath presentation
Xpath presentationXpath presentation
Xpath presentation
 
Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27Mongoseattle indexing-2010-07-27
Mongoseattle indexing-2010-07-27
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 
06 xml processing-in-.net
06 xml processing-in-.net06 xml processing-in-.net
06 xml processing-in-.net
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Chapter2
Chapter2Chapter2
Chapter2
 
Structure & Union in C++
Structure & Union in C++Structure & Union in C++
Structure & Union in C++
 
Xslt elements
Xslt elementsXslt elements
Xslt elements
 
Xslt attributes
Xslt attributesXslt attributes
Xslt attributes
 

Similar to Structure Array Display

Structure in c language
Structure in c languageStructure in c language
Structure in c languagesangrampatil81
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structuresKrishna Nanda
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And UnionsDhrumil Patel
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and UnionsDhrumil Patel
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languagetanmaymodi4
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languageTanmay Modi
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structureshaibal sharif
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++Jeff TUYISHIME
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxGIRISHKUMARBC1
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++Prof Ansari
 
Definition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptxDefinition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptxAnithaTAssistantProf
 
9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMS9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMSkoolkampus
 

Similar to Structure Array Display (20)

CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
 
Programming in C
Programming in CProgramming in C
Programming in C
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
C structure and union
C structure and unionC structure and union
C structure and union
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
 
1. structure
1. structure1. structure
1. structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Definition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptxDefinition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptx
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMS9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMS
 
Structure & union
Structure & unionStructure & union
Structure & union
 

Structure Array Display

  • 1. STRUCTURE School of Informatics, Hawassa University 11/28/2011 1
  • 2.  At the end of this lecture you will able to  Realize how to define a structure of your own data type using a keyword <struct >  Understand how Store large data of different type with in a single structure name  Identify different ways of structure instance creation and initializing structure instance with data members  Access individual structure members using dot( . ) access operator  Use arrays of structure and able to store more records  Use structure as a function argument School of Informatics, Hawassa University 11/28/2011 2
  • 3. Only one value will store at a time It holds one type no data type diversity No possibility to use as a new data type  Class Activity Why regular Variable? Why Array? discus by Their: It stores large amount of data at a time All data are of the same type It is not possible to use as a new data type  Storage capacity(amount of data store at a time)  Diversity of data type  Capacity to be new user defined data type Why structure? (5 Min) It stores Large amount of data at a time It is a collection of multi- diversified data types We can have data type of our own It is an instance of full record about one object School of Informatics, Hawassa University 11/28/2011 3
  • 4.  Arrays allow you to define variables that combine several data items of the same type  But structure is another user defined data type which allows you to combine a group of data items(members) of different data type and length with in a single structure name.  Structures are used to represent a record, Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book: Title Author ISBN Book_No School of Informatics, Hawassa University 11/28/2011 4
  • 5.  To declare structure of your own data type with different data member we use keyword <truct>  General syntax struct Struct_Name { type2 DataMember 1; type2 DataMember 2; type1 DataMember 3; School of Informatics, Hawassa University 11/28/2011 5 }; New user Defined Data type Keyword Struct Data members with different Data type
  • 6.  Struct_Name :- we can use as a user defined data type we can create a new object of struct_name as following: Syntax :- We Use as our new data type struct_name Obj_Name;  We can also create an object of user defined structure during structure declaration like: struct Struct_Name { type2 DataMember 1; type2 DataMember 2; type1 DataMember 3; }stru1,stru2,stru3; School of Informatics, Hawassa University 11/28/2011 6
  • 7.  Create structure called Book which has attributes Title, Author, ISBN, Book_No Struct Book { char Title[50]; char Author[20]; char ISBN[15]; int Book_No; School of Informatics, Hawassa University 11/28/2011 7 }; Struct Book { char Title[50]; char Author[20]; char ISBN[15]; int Book_No; } bookObj;
  • 8.  As with arrays and variables, structure members can also be initialized. This is performed by enclosing the values to be initialized inside the braces { and } after the structure variable name while it is defined. Struct Book { char Title[50]; char Author[20]; char ISBN[15]; int Book_No; } Book1 = {“C++”, ” John”, “12-34-034-234”,12}; School of Informatics, Hawassa University 11/28/2011 8
  • 9.  The Second way to initialize structure Struct Book { char Title[50]; char Author[20]; char ISBN[15]; int Book_No; } ; int main() { Book Book1 = {“C++”, ” John”, “12-34-034-234”,12}; School of Informatics, Hawassa University 11/28/2011 9 }
  • 10.  Structure members can’t access directly as a normal local or global variables …  To access any member of a structure, we use the member access operator either called dot (.) operator for non pointer structure variable or arrow operator () for pointer structure variables. The member access operator is coded as a dot between the structure variable name and the structure member that we wish to access. School of Informatics, Hawassa University 11/28/2011 10
  • 11.  Syntax struct_var.Member1=//some operation;  Example Struct Book { char Title[50]; char Author[20]; char ISBN[15]; int Book_No; School of Informatics, Hawassa University 11/28/2011 11 }Book1; Assign Individual Member Book1 .Title= “C++”; Book1.Author= ”John”; Book1.ISBN= “12-34-034-234” Book1.Book_No=12;
  • 12. ? Example :1 Create structure called Books which have four data members Title, Author, ISBN, Book_No that used to store three different Books and display all Book you initialize from the initialization when you instantiate an instance of structure Books School of Informatics, Hawassa University 11/28/2011 12
  • 13. #include<iostream> using namespace std; struct Books { char Title[50]; char Author[25]; char ISBN[15]; int Book_No; }; int main() { Books Book1 = {"Java","Johe","123-3243-2312",1}; Books Book2 = {"C++","Johe","123-3243-2313",3}; Books Book3 = {"C#","Johe","123-3243-2314",2}; cout<<"tTitle tAuthor tISBN ttBook Number"<<endl; cout<<"t................................................................"<<endl; cout<<"t"<<Book1.Title<<"t"<<Book1.Author<<"t“ Initializing 3 Books With 4 data members for each book <<Book1.ISBN<<"t"<<Book1.Book_No<<endl; cout<<"t"<<Book2.Title<<"t"<<Book2.Author<<"t“ <<Book2.ISBN<<"t"<<Book2.Book_No<<endl; cout<<"t"<<Book3.Title<<"t"<<Book3.Author<<"t“ <<Book3.ISBN<<"t"<<Book3.Book_No<<endl; system("pause"); return 0; } School of Informatics, Hawassa University 11/28/2011 13 •Printing 3 books data by using dot operator access operator Structure declaration and member definition
  • 14. Why Arrays of Structure?  Arrays of a structure is a collection of structure  A single structure instance will represent a single record at a time. If we need to store large amount of record in a single structure we need to create many instances of a give structure and this is inefficient to manage large record  It is possible to work by creating individual structure variable for small number of records which is less than 10 record but …  In order to manage large record of same category we need to develop an arrays of structure which is an array stores different types of structure member variables School of Informatics, Hawassa University 11/28/2011 14
  • 15.  Image you have structure Books with attribute Title, Author, ISBN, Book_No and you need to store 1000 Books with their corresponding attribute Is it easy to create 1000 Books Variable?  It is not easy! so what is the solution? … 1. We can solve this using arrays of structure … 2. Lets compare and contrast individual structure variable creation and array usage School of Informatics, Hawassa University 11/28/2011 15
  • 16. Struct Books { char Title[50]; char Author[20]; char ISBN[15]; int Book_No; }Book1, Book2, Book3, Book4 …Book1000; Individual structure variable Using arrays of structure Struct Books { char Title[50]; char Author[20]; char ISBN[15]; int Book_No; } BookObj[1000]; School of Informatics, Hawassa University 11/28/2011 16
  • 17.  To access individual elements of arrays of structure we need to specify the index number with in a square bracket of the structure instance like simple array operation  Assume that you have structure as follow Struct Books { char Title[50]; char Author[20]; char ISBN[15]; int Book_No; } BookObj[10]; How to access the first three books from your book storage structure? School of Informatics, Hawassa University 11/28/2011 17
  • 18. Book one information Book Two information Book Three information BookObj[0].Title BookObj[0].Author BookObj[0].ISBN BookObj[0].Book_No BookObj[1].Title BookObj[1].Author BookObj[1].ISBN BookObj[1].Book_No BookObj[2].Title BookObj[2].Author BookObj[2].ISBN BookObj[2].Book_No Title Author ISBN Book_No BookObj[0] Title Author ISBN Book_No BookObj[1] Title Author ISBN Book_No BookObj[2] School of Informatics, Hawassa University 11/28/2011 18
  • 19.  Structure can pass into function as an argument rather than passing individual elements it is better to pass the whole of structure  We can pass structure either by reference or by value but it is recommended to pass structure by their reference because to minimize the cost of copying the value  Its default value of passing structure is by nature uses by reference School of Informatics, Hawassa University 11/28/2011 19
  • 20. #include<iostream> using namespace std; struct Time { int h, m, s; }; int ToSecond(Time now){ return (now.h*60*60 + now.m*60 + now.s); School of Informatics, Hawassa University 11/28/2011 20 }i nt main(){ Time T; cout<<"Enter Time In Hour, Min and Second:"; while( cin>>T.h>>T.m>>T.s) { cout<<"Total Second:"<<ToSecond(T); } system("pause"); return 0; } Example Passing structure time T
  • 21. ? Class activity  Create a structure called Books to hold records of 100 Books with Book detail of Title, Author, ISBN and Book_No. Your program will accept an input from the user and display all Book information in table format  Assume you will have a basic about  Display():- Function to give output  Input():- Function to accept input from keyboard  Main():- To call an input() function  Structure definition:-To declare all members School of Informatics, Hawassa University 11/28/2011 21
  • 22. #include<iostream> using namespace std; struct Books { char Title[50]; char Author[20]; char ISBN[15]; int Book_No; }; const int size = 100; void display(Books Book[size], int n) { cout<<"Title"<<"t"<<"Author"<<"t“ <<"ISBN"<<"t"<<"Book No"<<endl; for(int i = 0; i < n ; i++) { cout<<Book[i].Title<<"t"<<Book[i].Author<<"t" <<Book[i].ISBN<<"t"<<Book[i].Book_No<<"t" <<endl; } } School of Informatics, Hawassa University 11/28/2011 22 void input() { Books Book[size]; cout<<"nEnter No of Books:"; int n; cin>>n; for(int i = 0;i < n ; i++) { cout<<"nEnter Title:“; cin>>Book[i].Title; cout<<"nEnter Author:“; cin>>Book[i].Author; cout<<"nEnter ISBN:“; cin>>Book[i].ISBN; cout<<"nEnter Book No:"; cin>>Book[i].Book_No; } display(Book,n); } int main() { input(); system("pause");; return 0; } Input function (initialize struct) Output function Structure declaration Main function
  • 23.  Structures can be implemented as the member of other structure.  This is termed as structure within structure we call it nested structure  Why we use nested structure?  To organize multi valid attribute as a single value  To eliminate data redundancy in different structures We collect similar data members of two or more structure with in a single structure name  To minimize coding time School of Informatics, Hawassa University 11/28/2011 23
  • 24. struct date{ int day; int month; int year; }; struct Employee { char name[40]; long int Emp_ID; char sex[5]; date dob; Example: Using nested structure to manipulate multi valid attribute Example: Date of Birth has 3 integer values to manipulate it we use Date as nested struct in Employee struct School of Informatics, Hawassa University 11/28/2011 24 }; Using date structure as Member of Employee structure
  • 25.  Assume you have struct Employee and Customer with their data members as following struct Employee{ string Name; string EID; int age; String Contact_Name; string Kebele; string Woreda; string PhoneMobil; string PhonHome; School of Informatics, Hawassa University 11/28/2011 25 }; struct Customer{ string Name; string CustID; int age; string Kebele; string Woreda; string PhoneMobil; string PhonHome; };
  • 26.  From the above structure definition address information is redundant both for Employee and customer structure  So to eliminate this data redundancy we separate address information in different structure name called address and both customer and employee will inherit from address structure School of Informatics, Hawassa University 11/28/2011 26
  • 27. struct Employee { string Name; string EID; int Empage; Address EmpAddress; School of Informatics, Hawassa University 11/28/2011 27 }; struct Customer { string Name; string CustID; int Custage; Address CustAddress; }; Struct Address { String Contact_Name; string Kebele; string Woreda; string PhoneMobil; string PhonHome; }
  • 28.  We can initialize a nested structure either by putting all elements in order of one structure or as block of data members Example:- Initialize all member of the above nested structure definition with Data : Name =Dagim, ID =EMP009, Sex = M, Date of Birth = 10-2-1989 Option 1:- Employee Emp ={“Dagim”, “EMP009”,’M’,10,2,1989}; Option 2:- Employee Emp ={“Dagim”, “EMP009”,’M’,{10,20,1989}}; Option 3:- Emp.name=“Dagim”; Emp.EMP_ID=“EMP009”; Emp.sex=‘M’; Emp.dob.day=10; Emp.dob.month=2; Emp.dob.dat=1989; School of Informatics, Hawassa University 11/28/2011 28
  • 29.  To access a nested structure data members we will use to dot access operator  Example:- To access a particular month of Employee birth date we can access as follow Employee Emp; Emp.dob.month ; School of Informatics, Hawassa University 11/28/2011 29
  • 30. #include<iostream> using namespace std; struct date { int day; int month; int year; }; struct Employee { char First_Name[23]; char Last_Name[24]; date dob; } Emp= {"DEMEKE","BIRHANU",{16 , 2, 2012}}; int main() { cout<<"ntFirst NametLast NametDate of Birth"<<endl; cout<<"nt"<<Emp.First_Name<<"tt"<<Emp.Last_Name<<"tt" <<Emp.dob.day<<"-"<<Emp.dob.month<<"-"<<Emp.dob.year<<endl; system("pause"); return 0; } School of Informatics, Hawassa University 11/28/2011 30
  • 31. Thanks ! School of Informatics, Hawassa University 11/28/2011 31