DOC PREVIEW
CUNY CS 503 - C++ Data Types

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PowerPoint PresentationStructured Data TypeonePersonAnotherPersonstruct Recordstruct type DeclarationSlide 7More about struct type declarationsAccessing struct MembersValid operations on a struct member depend only on its typeExamples of aggregate struct operationsSlide 12Passing a struct Type by ReferenceSlide 14Slide 15Abstraction1C++ Data TypesC++ Data Typesstructuredarray struct union class addresspointer referencesimple integral enumchar short int long boolfloatingfloat double long double2Structured 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 individually3onePerson 5000 .id 2037581.name “John Smith” .dept ‘B’.hours 23.64AnotherPerson 6000 .id 5281003 .name “Mary Jones”.dept ‘A’.hour 355struct Recordstruct Record // declares a struct data type{ // does not allocate memorylong id ;string name ;char dept ; // struct members float hour; } ;Record onePerson; // declare variables of RecordRecord anotherPerson ; 56struct type DeclarationSYNTAX struct TypeName // does not allocate memory { MemberList } ;MemberList SYNTAX DataType MemberName ; DataType MemberName ; . . .7struct type DeclarationThe 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.8More about struct type declarationsIf 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.9Accessing 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. EXAMPLESonePerson.houranotherPerson.name10Valid operations on a struct member depend only on its typeonePerson.id = 7581;cin >> onePerson.hour;anotherPerson.name = “Adel”;11Examples of aggregate struct operationsanotherPerson = onePerson ; // assignmentprintReocrd(onePerson); // value parameterChangeHours(onePerson); // reference parameter anotherPerson = GetRecord( ); // return value of function NOW WE’LL WRITE THE 3 FUNCTIONS USED HERE . . .12void 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;}1213void ChangeHour( /* inout */ Record& onePerson, float today_hour ){onePerson.hour += today_hour;} Passing a struct Type by Reference14Record 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 ;}1415struct DateType{ int month ; // Assume 1 . . 12int day ; // Assume 1 . . 31 int year ; // Assume 1900 . . 2050};struct StatisticsType{ float failRate ;DateType lastServiced ; // DateType is a struct typeint downDays ;} ;struct MachineRec{ int idNumber ;string description ;StatisticsType history ; // StatisticsType is a struct typeDateType purchaseDate ;float cost ;} ;MachineRec machine ; 1516Abstraction 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


View Full Document

CUNY CS 503 - C++ Data Types

Documents in this Course
Load more
Download C++ Data Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C++ Data Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C++ Data Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?