Structures
Introduction • 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 • A structure is a collection of variable types grouped together or • A structure is a collection of heterogeneous data types grouped together to form a single data type • You can refer to a structure as a single variable, and to its parts as members of that variable • Members are accessed by using the dot (.) operator following the structure variable
Cont’d… • The power of structures lies in the fact that once defined, the structure name becomes a user-defined data type and may be used the same way as other built-in data types, such as int, double, char, … • The format for defining a structure is: struct Tag { Members }; • Where Tag is the name of the entire type of structure • Members are the variables or data fields within the struct
Structure Tag or Structure Name • Examples… struct employee { intid_number; int age; float salary; } emp; struct employee { intid_number; int age; float salary; } emp1, emp2, emp3; Members/Fields of the structure Structure variable Three different Structure variables
struct employee { intid_number; int age; float salary; }; employee emp1, emp2, emp3; struct date{ int day; int month; int year; }; Three different Structure variables
Arrays vs Structures • The difference between arrays and structures is that: • The elements of an array are of the same type(homogeneous) while the elements of a structure which are know as members or fields are of different types(heterogeneous) • Each element of an array is referred by its position(index) which each element of a structure referred by its name(has a unique name)
Accessing array elements vs structure fields #include<iostream.h> #include<conio.h> struct number { int x; float y; char ch; }num = {15, 3.5, 'A'}; void main() { cout<<"x = "<<num.x<<", y = "<<num.y<<", ch = "<<num.ch<<endl; int num[3] = { 4, -6,10}; cout<<"num[0]= "<<num[0]<<", num[1]= "<<num[1]<<", num[2]= "<<num[2]<<endl; getch(); } Output x = 15, y = 3.5, ch = A num[0]= 4, num[1]= -6, num[2]= 10 Structure initialization
Structure initialization // A structure to record and display date #include<iostream.h> #include<conio.h> struct date{ int day; int month; int year; }; date today; void main() { today.day = 28; today.month = 8; today.year = 2013; cout<<"\nToday's date is: "<<today.day<<'/'<<today.month<<'/'<<today.year; getch(); } Output Today's date is: 28/8/2013
Reading the data fields Output Enter the day: 15 Enter the month: 7 Enter the year: 2012 Today's date is: 15/7/2012 //Reading data field values from the keyboard #include<iostream.h> #include<conio.h> struct date{ int day; int month; int year; }; date today; void main() { cout<<"\n Enter the day: "; cin>>today.day; cout<<"\n Enter the month: "; cin>>today.month; cout<<"\n Enter the year: "; cin>>today.year; cout<<"\nToday's date is: "<<today.day<<'/'<<today.month<<'/'<<today.year; getch(); }
Structures and pointers • If you wish to have a pointer to a structure, to actually access the information stored inside the structure that is pointed to, you use the -> operator in place of the dot ( . ) operator #include<iostream.h> #include<conio.h> struct example { int x; }; void main() { example *ptr = new example; ptr->x = 12; cout<<" x = "<<ptr->x<<endl; cout<<" x = "<<(*ptr).x<<endl; getch(); } Output x = 12 x = 12
Cont’d… #include<iostream.h> #include<conio.h> struct bal { char name[30]; float balance; }person; int main() { bal *p= new bal; cout<<"Customer name: "; cin>>p->name; cout<<"Customer balance: "; cin>>p->balance; cout<<"\n Customer Info\n"; cout<<"Customer name\t Balance\n"; cout<<p->name<<"\t\t"<<p->balance<<endl; getch(); return 0; } Sample Run Customer name: Abebe Customer balance: 1500 Customer Info Customer name Balance Abebe 1500
Examples (pointers and Structure …) // A simple program involving structure of items #include<iostream.h> #include<conio.h> struct item { intitemCode; char itemName[20]; int qty; float unitPrice; }; item *p;
Cont’d… void main() { p = new item; cout<<"\nItem code: "; cin>>p->itemCode; //cin>>(*p).itemCode cout<<"\nItem Name: "; cin>>p->itemName; //cin>>(*p).itemName cout<<"\nItem Quantity: "; cin>>p->qty; //cin>>(*p).qty cout<<"\nItem unit price: "; cin>>p->unitPrice; //cin>>(*p).unitPrice cout<<"\nItem Code\t Item Name \t Quantity \t Unit Price\t Total Price\n"; cout<<p->itemCode<<"\t\t"<<p->itemName<<"\t\t"<<p->qty<<"\t\t"<< p->unitPrice<<"\t\t"<<p->qty*p->unitPrice<<endl; getch(); }
Cont’d… Output Item code: 101 Item Name: Sugar Item Quantity: 200 Item unit price: 15 Item Code Item Name Quantity Unit Price Total Price 101 Sugar 200 15 3000
The same example with different style of pointer void main() { p = new item; cout<<"\nItem code: "; cin>>(*p).itemCode; //p->itemCode; cout<<"\nItem Name: "; cin>>(*p).itemName; //cin>>p->itemName; cout<<"\nItem Quantity: "; cin>>(*p).qty; //cin>>p->qty; cout<<"\nItem unit price: "; cin>>(*p).unitPrice; //cin>>p->unitPrice; cout<<"\nItem Code\t Item Name \t Quantity \t Unit Price\n"; cout<<(*p).itemCode<<"\t\t"<<(*p).itemName<<"\t\t"<<(*p).qty<<"\t\t"<<(*p).unitPrice<<endl; getch(); }
Structures and Functions • We can use functions to write a modular program and access structure variables #include<iostream.h> #include<conio.h> struct student { char name[20]; int age; char gender; }; student stud1,stud2; void getStudent(); void printStudent(); void main() { getStudent(); printStudent(); getch(); } Function prototypes/Function declarations
Cont’d… void getStudent() { cout<<"\n Enter name of the first student: "; cin>>stud1.name; cout<<"\n Enter age of the first student: "; cin>>stud1.age; cout<<"\n Enter gender of the first student: "; cin>>stud1.gender; cout<<"\n Enter name of the second student: "; cin>>stud2.name; cout<<"\n Enter age of the second student: "; cin>>stud2.age; cout<<"\n Enter gender of the second student: "; cin>>stud2.gender; } void printStudent() { cout<<"\nStudent Information\n"; cout<<"Name\tAge\tGender\n"; cout<<stud1.name<<'\t'<<stud1.age<<'\t'<<stud1.gender<<endl; cout<<stud2.name<<'\t'<<stud2.age<<'\t'<<stud2.gender<<endl; }
Cont’d… Sample Run of the previous program Enter name of the first student: Abebe Enter age of the first student: 23 Enter gender of the first student: M Enter name of the second student: Hirut Enter age of the second student: 21 Enter gender of the second student: F Student Information Name Age Gender Abebe 23 M Hirut 21 F
Arrays of Structures • Structure corresponds to records of a database • One can specify the number of records of a structure in an array form: struct student { char name[20]; int age; char gender; }; student st1,st2,st3,st4,st5; student stud[5]; Five structure variables Array of structure variable of size 5
… Arrays of Structures #include<iostream.h> #include<conio.h> const int max=100; struct employee{ char empID[10]; char empName[20]; float salary; //unit price }; employee emp[max]; int n; void getEmployee(); void printReport(); float netSalary(float); void main() { getEmployee(); printReport(); getch();
…Cont’d void getEmployee() { char ans; n=0; do{ cout<<"\n Employee ID: "; cin>>emp[n].empID; cout<<"\n Employee Name: "; cin>>emp[n].empName; cout<<"\n Salary: "; cin>>emp[n].salary; n++; cout<<"\n Any more employees? y/n: "; cin>>ans; }while(ans=='y' || ans=='Y'); }
Cont’d… float netSalary(float bs) { float tax,pension; pension = bs*0.06; if(bs<=150) tax = 0; else if(bs<=650) tax = (bs-150)*0.1; else if(bs<=1400) tax = 50+(bs-650)*0.15; else if(bs<=2350) tax = 162.5+(bs-1400)*0.2; else if(bs<=3500) tax = 352.5+(bs-2350)*0.25; else if(bs<=5000) tax = 640+(bs-3500)*0.3; else if(bs>5000) tax = 1090+(bs-5000)*0.35; return bs-(tax + pension); } void printReport() { cout<<"\n Employee Info\n\n"; cout<<"\n ID\t\t Name \t\t Basic Salary \t Net Salary\n"; for(inti=0; i<n; i++) cout<<emp[i].empID<<"\t\t"<<emp[i].empName<<"\t\t"<<emp[i].salary<<"\t\t"<<netSalary(emp[i].salary)<<endl; }
Cont’d… Employee ID: emp01 Employee Name: Dawit Salary: 2500 Any more employees? y/n: y Employee ID: emp02 Employee Name: Aster Salary: 3000 Any more employees? y/n: y Employee ID: emp04 Employee Name: Rahel Salary: 1500 Any more employees? y/n: n Employee Info ID Name Basic Salary Net Salary emp01 Dawit 2500 1960 emp02 Aster 3000 2305 emp04 Rahel 1500 1227.5 • Sample Run of the program
…Example: Array of Structures #include<iostream.h> #include<conio.h> const int max=20; struct student { int id; char name[20]; int age; }; student stud[max]; int n; void getStudent(); void printStudent(); void main() { getStudent(); printStudent(); getch(); }
… Cont’d void getStudent() { n=0; char ans; do{ cout<<"\n Enter id of the student: "; cin>>stud[n].id; cout<<"\n Enter name of the student: "; cin>>stud[n].name; cout<<"\n Enter age of the student: "; cin>>stud[n].age; n++; cout<<"\n Any more student?y/n: "; cin>>ans; }while(ans=='y' || ans=='Y'); } void printStudent() { cout<<"\n Student Info\n\n"; cout<<"\n ID\t Name \t Age \n"; for(inti=0; i<n; i++) cout<<stud[i].id<<"\t"<<stud[i].name<<"\t"<<stud[i].age<<endl; }
Cont’d… • Sample run of the program Enter id of the student: 11 Enter name of the student: Bekele Enter age of the student: 21 Any more student?y/n: y Enter id of the student: 33 Enter name of the student: Aster Enter age of the student: 23 Any more student?y/n: n Student Info ID Name Age 11 Bekele 21 33 Aster 23
Nested Structures struct date { int day; int month; int year; }; struct employee { char empID[5]; char name[20]; date dob; //date of birth float salary; }; • Structures within another structures are called nested structures structaddressInfo { charsubcity[20]; charworeda[20]; charkebele[2]; }; struct employee { charempID[5]; char name[20]; addressInfoaddress; float salary; }; Nested structure Nested Structure
Nested Structure #include<iostream.h> #include<conio.h> const int max=20; struct date { int day; int month; int year; }; struct employee { char empID[10]; char name[20]; date dob; //date of birth float salary; }; employee emp[max]; int n;
Cont’d… void getEmployee() { char ans; n=0; do{ cout<<"\n Enter id of the employee: "; cin>>emp[n].empID; cout<<"\n Enter name of the employee: "; cin>>emp[n].name; cout<<"\n Enter birth date of the employee"; cout<<"\n Day: "; cin>>emp[n].dob.day; cout<<"\n Month: "; cin>>emp[n].dob.month; cout<<"\n Year: "; cin>>emp[n].dob.year; cout<<"\n Enter salary of the employee: "; cin>>emp[n].salary; n++; cout<<"\n Any more employee?y/n: "; cin>>ans; }while(ans=='y' || ans=='Y'); }
Cont’d… void printEmployee() { cout<<"\n Employee Info\n\n"; cout<<"\n ID\t Name\t Date of birth\t Salary \n"; for(inti=0; i<n; i++) cout<<emp[i].empID<<"\t"<<emp[i].name<<"\t"<<emp[i].dob.day<<'/'<<emp[i].dob.month<<'/'<<emp[i].dob.year<<"\t"<<emp[i].salary<<endl; } void main() { getEmployee(); printEmployee(); getch(); }
Cont’d… (Sample Run of the program) Enter id of the employee: emp01 Enter name of the employee: Abdul Enter birth date of the employee Day: 16 Month: 5 Year: 1998 Enter salary of the employee: 2500 Any more employee?y/n: y Enter id of the employee: emp02 Enter name of the employee: Dawit Enter birth date of the employee Day: 8 Month: 8 Year: 1994 Enter salary of the employee: 1500 Any more employee?y/n: n Employee Info ID Name Date of birth Salary emp01 Abdul 16/5/1998 2500 emp02 Dawit 8/8/1994 1500
Demonstration for Assignment • In groups of 3 or 4 members write a program satisfying the following requirements:
Assignment (Final Project part one) • Write a menu driven program using structures of inventory items that consists of a minimum of the following functionalities: • getItem… to input inventory data • searchItem… to search inventory item • displayItem…. To display inventory items • The displayItem function should display item code, item name, item quantity and total price of a given item • The inventory structure should have the following minimum fields: itemCode (string), itemName (string), quantity (int), unitPrice(float)
Next topic • Data File Operations