1 / 16

C++ Data Types

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

gwylan
Download Presentation

C++ Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  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. struct type Declaration SYNTAX struct TypeName // does not allocate memory { MemberList } ; MemberList SYNTAX DataType MemberName ; DataType MemberName ; . . .

  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. More aboutstruct 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. Accessingstruct 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. Valid operations on a struct member depend only on its type onePerson.id = 7581; cin >> onePerson.hour; anotherPerson.name = “Adel”;

  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. 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. Passing a struct Type by Reference void ChangeHour( /* inout */ Record& onePerson, float today_hour ) { onePerson.hour += today_hour; }

  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. struct DateType { int month ; // Assume 1 . . 12 int day ;// Assume 1 . . 31 int year ; // Assume 1900 . . 2050 }; struct StatisticsType { float failRate ; DateType lastServiced ; // DateType is a struct type int downDays ; } ; struct MachineRec { int idNumber ; string description ; StatisticsType history ; // StatisticsType is a struct type DateType purchaseDate ; float cost ; } ; MachineRec machine ; 15

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

More Related