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

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 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 11 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 11 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 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS1C – Advanced Programming in C++structs to classesConcepts of OOPSaddleback College Fall 2010 – J TateyamaSW Chapter 3CS1C – 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.CS1C – 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–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 a formal parameter list– Using 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();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  To do so, place the public member functions at the top and then specify that the data members are priviatestruct DataRec{void GetData();void OutputData();private: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 functionsCS1C – Saddleback CollegeConcepts of Object-Oriented Programming Procedural Programming Languages– Procedural languages are used to code algorithms using functions and procedures (void functions in C++)– Early languages such as FORTRAN, BASIC, and Pascal were designed to use standard algebraic formulas and this method of programming worked very well Object-Oriented Programming (OOP) Languages– For a language to be considered a "pure" object-oriented language it must contain three specific features: classes, inheritance, and polymorphism– The language must• always use classes• have all data types be classes (no primitive types)• have all data values as objects• allow all operators to be overloaded• ensure that every data operation can only be executed using a class member functionCS1C – Saddleback CollegeHybrid Programming Languages Languages such as C++ rely heavily on ___________ programming It is possible to write entire programs using only these features of the language.  To be a hybrid language, the language must have the ability to define classes. It is not necessary for hybrid languages to provide inheritance and proceduralIt is not necessary for hybrid languages to provide inheritance and polymorphism features.  But If the language provides classes but does not support inheritance and polymorphism, it is said to be an ______________ language rather than ________________ .object- basedobject orientedCS1C – Saddleback CollegeObject Oriented Programming The purpose of object-oriented programming is to model real world objects with _______________________ Our world is made up of objects and their associated behaviors and statesstate behaviora child name cryingsoftware counterpartsgenderagesleepingeatinga car # of doors# of gears4 wheelsacceleratingchanging gears In C++, the mechanism that allows you to combine data and the operations on that data in a single unit is called a classCS1C – Saddleback CollegeInstance In programming, everything the object knows (state) and everything it can do (behaviors) is expressed by the variables and methods (member functions) within the object Once a class is defined, you can declare variables of that type resulting in an instance of the class or class instance The variables and methods are referred to as– instance variables– instance methods An example of an instance and method you may have already used for file i/o is ifstreamifstream infile; //instance of class ifstreaminfile.open(); //instance methodCS1C – Saddleback CollegeObject and Information Hiding Object – A software collection of variables and related methods.  A class object can also be called a class instance.  The purpose of an object is to bundle the variables and methods together in one software entity. This is called ________________ and it has two main benefits for software developers: information hiding, and modularity.encapsulation Information Hiding– An object has a public interface that clients can use to communicate with it. Additionally, the object can contain private information and methods not accessible to the client– The private information can be changed without affecting other objects that depend on it– For example, you do not need to understand how a transmission works to drive a carCS1C – Saddleback CollegeModularity Modularity– The code for various objects can be written and maintained independently from that of other objects– An object can be easily used in many different environments by many different clients and is not bound to any one program In the real world, there are many objects of the same kind–My daughter has the same basic qualities as your child, your niece, –My daughter has the same basic qualities as your child, your niece, nephew etc– The prototype for a child could be defined in a class


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?