DOC PREVIEW
Saddleback CS 1C - structs to classes Concepts of OOP

This preview shows page 1 out of 3 pages.

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

Unformatted text preview:

CS1C – Advanced Programming in C++Saddleback College Spring 2009 – J. Tateyamastructs to classesConcepts of OOPChapter 3 – part 1CS1C – Saddleback CollegeStructs to Classes In CS1B we emphasized procedural programming– We declared variables (objects to act upon) and a set of separate and independent operations (functions)– The functions could act upon any set of variables– The objects were passive - they did not have any particular set of operations defined for them– see SW 3-2 In CS 1C we are going to look at a type of programming called object-oriented programming (OOP). – One of the key concepts in OOP is that of encapsulation. – The objects (variables) in OOP are active. – They have not only data but also a set of actions that may be performed on the data. Let’s try to put the functions that operate on the data into the structitself – see SW 3-3CS1C – Saddleback CollegeStructs to Classes Let’s try to put the functions that operate on the data into the structitself – see SW 3-3 What’s wrong with this?– Violates the separation of specification from implementation (black box) What will happened when we put the definitions outside of the structand place the prototype inside the struct? see SW 3-4– Compiler error – function has no prototype Need to tell the compiler which structure the definitions are associated with– Format is struct name :: function name where the :: is call the scope resolution operator– Since the functions “belong” to variables of a specific type then there is no need for formal parameter list– Use dot notation with the function calls and a call to a member function is call “message passing” – see SW3-5CS1C – Saddleback CollegeMember Access What if we modified the main as follows:int main(void){DataRec theData;theData.GetData( );cout << "\nEnter name: ";cin.ignore();getline(cin,theData.name);theData.OutputData( );} Anyone declaring a variable of type DataRec could do anything they wished to the name and age because by default all members of a struct are public.CS1C – Saddleback CollegeMember Access We want to restrict access to member functions only  We place the public member functions at the top and then specify that the data members are priviatestruct DataRec{void GetData();void OutputData();private:string name;int age;};  Then the statementgetline(cin,theData.name); would produce an error message (illegal access to protected/private member) The only way the user of the struct may gain access to the private data is through the member


View Full Document
Download structs to classes Concepts of OOP
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 structs to classes Concepts of OOP 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 structs to classes Concepts of OOP 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?