Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures Chapter 4.

Similar presentations


Presentation on theme: "Structures Chapter 4."— Presentation transcript:

1 Structures Chapter 4

2 Structures Variables of simple data types such as float , char, and int represent one item of information A structure is collection of simple variables. Variables can be of different types Data items in structure called members of the structure A structure is collection of data while a class is collection of both data and functions Members of structure are public by default while in class members are private by default

3 Syntax of Structure Syntax Example Struct structure-name {
member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } struct-variable; Struct part { int modelnumber; int partnumber; float cost; } part1;

4 Example ////////////////////////////////////// struct part { int modelnumber; int partnumber; float cost; }; int main() part part1; part1.modelnumber = 6244; part1.partnumber = 373; part1.cost = ; cout<<“Model: ”<< part1.modelnumber; cout<<“, Part: ”<< part1.partnumber; cout<<“, cost $: ”<< part1.cost; return 0; }

5 structures The structure definition is only blue print for creation of variables Defining a structure variable reserves space in memory part part1; part part2; is similar when we are defining a variable e.g. int var1 Members of structures are accessed by its variable following dot operator (member access operator) part1.modelnumber = 6244

6 Cont’d Another way to initialize structure members are
part part1 = {6244,373,217.55}; One structure variable can be assigned to another part2 = part1; The value of each member of part1 is assigned to corresponding member of part2 One structure variable can assigned to another only when they are of the same structure type

7 Structure within structures
Structures can be nested within other structures Example struct Distance { int feet; float inches; }; ////////////////////////////////// struct Room Distance length; Distance Width; } int main() { Room dining; dinning.length.feet = 13; dinning.length.inches =6.5; dinning.width.feet = 10; dinning.width.inches =0.0; Float l = dinning.length.feet + dinning.length.inches/12; Float w= dinning.width.feet + dinning.width.inches/12; Cout<<“Dining room area is ”<< l*w; return 0; }

8 Enumeration Another way to define own data type is enumeration
Enumerated types work when you know in advance a finite list of values that a data type can take on e.g. enum days_of_weeks { Sun, Mon, Tue, Wed, Thu, Fri, Sat}; An enum defines a set of all names that will be permissible values of type. These permissible values are called enumeration Also enumeration is list of values and each value has its specific name

9 Cont’d Variable can be defined like in structure
days_of_week day1, day2; These variables can be given any of the values listed in the enum declaration. E.g. day1 = Mon; , day2 = Thu; day1 = halloween; // illegal Enumerations are treated internally as integers First name in list is given value 0, next name is 1 and so on.

10 Example enum days_of_week{Sun, Mon, Tue, Wed, Thu, Fri, Sat}; int main() { days_of_week day1, day2; day1 = Mon; day2 = Thu; int diff = day2 – day1; // can do integer arithmetic cout<<“days between = ”;<<diff<<endl; if(day1<day2) cout<<“day1 comes before day2 ”; return 0; }


Download ppt "Structures Chapter 4."

Similar presentations


Ads by Google