Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.

Similar presentations


Presentation on theme: "1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long."— Presentation transcript:

1 1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double

2 2 Structured Data Type A structured data type is a type in which each value is a collection of component items. l the entire collection has a single name l each component can be accessed individually

3 3 onePerson 5000.id 2037581.name “John Smith”.dept ‘B’.hours 23.6

4 4 AnotherPerson 6000.id 5281003.name “Mary Jones”.dept ‘A’.hour 35

5 5 struct Record struct Record// declares a struct data type {// does not allocate memory long id ; string name ; char dept ; // struct members float hour; } ; Record onePerson; // declare variables of Record Record anotherPerson ; 5

6 6 struct type Declaration SYNTAX struct TypeName // does not allocate memory { MemberList } ; MemberList SYNTAX DataType MemberName ;.

7 7 struct type Declaration The struct declaration names a type and names the members of the struct. It does not allocate memory for any variables of that type! You still need to declare your struct variables.

8 8 More about struct type declarations If the struct type declaration precedes all functions it will be visible throughout the rest of the file. If it is placed within a function, only that function can use it. It is common to place struct type declarations with TypeNames in a (.h) header file and #include that file. It is possible for members of different struct types to have the same identifiers. Also a non-struct variable may have the same identifier as a structure member.

9 9 Accessing struct Members Dot ( period ) is the member selection operator. After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot. EXAMPLES onePerson.hour anotherPerson.name

10 10 Valid operations on a struct member depend only on its type onePerson.id = 7581; cin >> onePerson.hour; anotherPerson.name = “Adel”;

11 11 Examples of aggregate struct operations anotherPerson = onePerson ; // assignment printReocrd(onePerson); // value parameter ChangeHours(onePerson); // reference parameter anotherPerson = GetRecord( ); // return value of function NOW WE’LL WRITE THE 3 FUNCTIONS USED HERE...

12 12 void printRecord( /* in */ Record onePerson) // Prints out values of all members { cout <<“ID # “ << onePerson.id<<endl <<“Name:”<<onePerson.name<<endl <<“Dept:”<<onePerson.dept<< endl <<“Hours:”<<onePerson.hour<<endl; } 12

13 13 void ChangeHour( /* inout */ Record& onePerson, float today_hour ) { onePerson.hour += today_hour; } Passing a struct Type by Reference

14 14 Record GetRecord ( ) // Obtains all information from keyboard { Record thisPerson ; cout<<“ Enter ID, Name, dept, hour\n”; cin>>thisPerson.id; cin>>thisPerson.name; cin>>thisPerson.dept; cin>>thisPerson.hour; return thisPerson ; } 14

15 15 struct DateType {int month ; // Assume 1.. 12 int day ;// Assume 1.. 31 int year ; // Assume 1900.. 2050 }; struct StatisticsType {floatfailRate ; DateTypelastServiced ; // DateType is a struct type intdownDays ; } ; struct MachineRec {int idNumber ; string description ; StatisticsType history ; // StatisticsType is a struct type DateType purchaseDate ; float cost ; } ; MachineRec machine ; 15

16 16 Abstraction l is the separation of the essential qualities of an object from the details of how it works or is composed l focuses on what, not how l is necessary for managing large, complex software projects


Download ppt "1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long."

Similar presentations


Ads by Google