360 likes | 373 Views
Learn about structures in C++, including their definition, declaration, and accessing members using the dot operator. Understand how structures hold related data items and can be declared with different member types.
E N D
Programming Structures
Structures • A Structure is a collection of related data items, possibly of different types. • A structure type in C++ is called struct. • A struct is heterogeneous in that it can be composed of data of different types. • In contrast, array is homogeneous since it can contain only data of the same type.
Defination • A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. Data structures can be declared in C++ using the following syntax: struct type_name {member_type1 member_name1;member_type2 member_name2;member_type3 member_name3;..} object_names;
Structures • Structures hold data that belong together. • Examples: • Student record: student id, name, major, gender, start year, … • Bank account: account number, name, currency, balance, … • Address book: name, address, telephone number, … • In database applications, structures are called records.
Structures • Individual components of a struct type are called members (or fields). • Members can be of different • A struct is named as a whole while individual members are named using field identifiers. • Complex data structures can be formed by defining arrays of structs.
struct basics • Definition of a structure: struct <struct-type>{ <type> <identifier_list>; <type> <identifier_list>; ... } ; • Example: struct Date { int day; int month; int year; } ; Each identifierdefines a memberof the structure. The “Date” structure has 3 members, day, month & year.
struct examples • Example: struct StudentInfo{ int Id; int age; char Gender; double CGA; }; • Example: struct StudentGrade{ char Name[15]; char Course[9]; int Lab[5]; int Homework[3]; int Exam[2]; }; The “StudentInfo” structure has 4 members of different types. The “StudentGrade” structure has 5 members of different array types.
struct examples • Example: struct BankAccount{ char Name[15]; int AcountNo[10]; double balance; Date Birthday; }; • Example: struct StudentRecord{ char Name[15]; int Id; char Dept[5]; char Gender; }; The “BankAcount” structure has simple, array and structure types as members. The “StudentRecord” structure has 4 members.
Name Id Gender Dept Name Id Gender Dept struct basics • Declaration of a variable of struct type: <struct-type> <identifier_list>; • Example: StudentRecord Student1, Student2; Student1andStudent2are variables ofStudentRecordtype. Student1 Student2
struct basics struct product { int weight; double price; } ; product apple; product banana, melon; struct product { int weight; double price; } apple, banana, melon;
Structure E.g struct part { int modelnumber; int partnumber; float cost; }; int main () { part part1, part2; part1.modelnumber = 1111; part1.partnumber = 111; part1.cost = 111.11; Initializing data members
Structure E.g cout<<“Enter Model number = ”; cin>>part2.modelnumber ; .. … …..; cin>>part2.partnumber ; .. .. .. ..; cin>>part2.cost ; cout<<"\nModel of Part1 = "<<part1.modelnumber; cout<<"\nPart of part 1 = "<<part1.partnumber; cout<<"\nCost of part1 = "<<part1.cost<<endl; cout<<"\nModel of part2 = "<<part2.modelnumber; cout<<"\nPart of part2 = "<<part2.partnumber; cout<<"\nCost of part2 = "<<part2.cost<<endl; return 0; }
(Out Put) Model of Part1 = 1111 Part of part 1 = 111 Cost of part1 = 111.11 Model of part2 = 222 Part of part2 = 2222 Cost of part2 = 222.222 Press any key to continue
Structure definition Keyword struct part { int modelnumber; int partnumber; Structure Members float cost; } ; Semicolon terminate definition Braces Delimit Structure Members Structure name
Structure definition The structure definition serves only as a blueprint for the creation of variables of type part. It does not itself create any structure variables; that is, it does not set aside any space in memory or even name any variables. This is unlike the definition of a simple variable, which does set aside memory.
The first statement in main() part part1, part2; Structure Name Variable Names
Memory defines two variables of type structure part. The definition reserve space in memory for part1 & part2. How Much Space? Enough to hold all the members of part1 & part2. part1 part2 modelnumber = 4byte modelnumber = 4byte partnumber = 4byte partnumber = 4byte cos t= 4byte cos t= 4byte (Because int and float data type take 4 byte )
Accessing Structure members Members of part1 part1.modelnumber = 1111; part1.partnumber = 111; part1.cost = 111.11;
Accessing Structure members Members of part2 Entered by user part2.modelnumber = 222; part2.partnumber = 2222; part2.cost = 222.222;
Accessing Structure members • Can access using “dot operator” • Also know as member “access operator” • The variable name must be used to distinguish one variable from another, such as part1, part2.
Initializing Structures Members part part2 = {1980,321,22.22};
struct part{ int modelnumber; int partnumber; float cost;};int main (){ part part1, part3;; part part2 = {222,21232,22.22}; cin>>part1.modelnumber; cin>>part1.partnumber; cin>>part1.cost;
part3 = part1; cout<<"\nModel of Part1 = "<<part1.modelnumber; cout<<"\nPart of part 1 = "<<part1.partnumber; cout<<"\nCost $ of part1 = "<<part1.cost<<endl; cout<<"\nInitail Model = "<<part2.modelnumber; cout<<"\nInitial Part = "<<part2.partnumber; cout<<"\nInitial Cost = "<<part2.cost<<endl; cout<<"\nInitail Model = "<<part3.modelnumber; cout<<"\nInitial Part = "<<part3.partnumber; cout<<"\nInitial Cost = "<<part3.cost<<endl; return 0; }
Out Put Model of Part1 = 1111 Part of part 1 = 111 Cost $ of part1 = 111.11 Initail Model = 222 Initial Part = 21232 Initial Cost = 22.22 Initail Model = 1111 Initial Part = 111 Initial Cost = 111.11 Press any key to continue
Example Write a program in C++ that shows the area of 3 room's. Using Structure namely "distance". Take input of feet & inches from user for variable d1 (feet & inches), assign variable d2 = {10, 5.25} values. Now add feet and inches of d1 & d2 and store in d3. Display d1 (feet & inches) d2 (feet & inches) d3 (feet & inches) separately. Put Condition if d1 & d2 inches increase by 12 it become a foot.
Code struct Distance { int feet; float inches; }; void main() { Distance d1,d3; Distance d2= { 11, 6.25 }; cout<<“\n Enter feet:”; cin>>d1.feet; cout<< “Enter inches:”; cin>>d1.inches; d3.inches= d1.inches + d2.inches; d3.feet=d1.feet+ d2.feet; if(d3.inches>=12.0) { d3.inches=d3.inches – 12.0; d3.feet++; } cout<< d1.feet<<“---”<<d1.inches<<“ + “; cout<<d2.feet<<“---”<<d2.inches<<“ = “; cout<<d3.feet<<“---”<<d3.inches; }
Defining of objects struct product { int weight; double price; } apple, banana, melon;
cout << "Enter title: "; getline (cin,yours.title); cout << "Enter year: "; getline (cin,mystr); stringstream(mystr) >> yours.year; cout << "My favorite movie is:\n "; printmovie (mine); cout << "And yours is:\n "; printmovie (yours); return 0; } void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n"; } #include <iostream> #include <string> #include <sstream> using namespace std; struct movies_t { string title; int year; } mine, yours; void printmovie (movies_t movie); int main () { string mystr; mine.title = "2001 A Space Odyssey"; mine.year = 1968;
0 1 2 … 98 99 0 1 2 … 98 99 Arrays of structures • An ordinary array: One type of data • An array of structs: Multiple types of data in each array element.
Chan Tai Man 12345 M COMP . . . 0 1 2 … 98 99 Arrays of structures • We often use arrays of structures. • Example:StudentRecord Class[100];strcpy(Class[98].Name, "Chan Tai Man");Class[98].Id = 12345;strcpy(Class[98].Dept, "COMP");Class[98].gender = 'M';Class[0] = Class[98];
Arrays inside structures • We can use arrays inside structures. • Example:struct square{ point vertex[4];};square Sq; • Assign values to Sq using the given square (4, 3) (10, 3) (4, 1) (10, 1) x y x y x y x y
Example Create a structure called emp that contains three members, int id, char name[100], float sal. Ask the user to fill in data for three employees and then display information for each employee. Hint • Variable of struct emp will be array • Use while / for loop to control array
Que 3 (Code) #include<iostream.h> struct emp { int id; char name[100]; float sal; }; void main () { emp ob1[3]; int c=0; //Index Variable
Que 3 (Code) while(c<3) { cout<<"Enter ID of Employee "<<c+1<<" = "; cin>>ob1[c].id; cout<<"Enter name of Employee "<<c+1<<" = "; cin>>ob1[c].name; cout<<"Enter salary of Employee "<<c+1<<" = "; cin>>ob1[c].sal; c++; cout<<"\n\n"; }
Que 3 (Code) c = 0; while (c<3) { cout<<"\nId of emp "<<c+1<<" = "<<ob1[c].id; cout<<"\nName of emp "<<c+1<<" = "<<ob1[c].name; cout<<"\nSalary ofemp "<<c+1<<" = "<<ob1[c].sal; c++; cout<<"\n\n"; } }
Que 3 (Out Put) Enter ID of Employee 1 = 123 Enter name of Employee 1 = Yasir Enter salary of Employee 1 = 50000 Enter ID of Employee 2 = 124 Enter name of Employee 2 = Ali Enter salary of Employee 2 = 55000 Enter ID of Employee 3 = 125 Enter name of Employee 3 = Aamir Enter salary of Employee 3 = 65000 Id of emp 1 = 123 Name of emp 1 = Yasir Salary ofemp 1 = 50000 Id of emp 2 = 124 Name of emp 2 = Ali Salary ofemp 2 = 55000 Id of emp 3 = 125 Name of emp 3 = Aamir Salary ofemp 3 = 65000 Press any key to continue