1 / 15

Chapter 11

Chapter 11. More about classes and OOP. Relationships Between Classes. Possible relationships Access ("uses-a") Ownership/Composition ("has-a") Inheritance ("is-a"). Friend Function Declarations. Friend function may be a stand-alone function: class aClass { private:

sachi
Download Presentation

Chapter 11

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. Chapter 11 More about classes and OOP

  2. Relationships Between Classes Possible relationships • Access ("uses-a") • Ownership/Composition ("has-a") • Inheritance ("is-a")

  3. Friend Function Declarations • Friend function may be a stand-alone function: class aClass { private: int x; friend void fSet(aClass &c, int a); }; void fSet(aClass &c, int a) { c.x = a; }

  4. Friend Function Declarations 2) Friend function may be a member of another class: class aClass { private: int x; friend void OtherClass::fSet (aClass &c, int a); }; class OtherClass { public: void fSet(aClass &c, int a) { c.x = a; } };

  5. Friend Class Declaration • An entire class can be declared a friend of a class: class aClass {private: int x; friend class frClass; }; class frClass {public: void fSet(aClass &c,int a){c.x = a;} int fGet(aClass c){return c.x;} };

  6. Friends • Why use it? • Really a good idea?

  7. Object Composition class StudentInfo { private: string firstName, LastName; string address, city, state, zip; ... }; class Student { private: StudentInfo personalData; ... };

  8. Aggregation vs. Composition • Composition is special form of Aggregation. class Person { string name; Date dob; Country *pCountry; … } • If life-time of objects exactly match, it is composition. Aggregation may use pointers.

  9. Composition vs. Inheritance • Person has a name • Car has 4 wheels • Student is a person • Car is a vehicle • Terms: • Super class, base class, parent class • Sub class, derived class, child class

  10. Base Class Derived Class Inheritance Syntax and Notation // Existing class class Base { }; // Derived class class Derived : public Base { }; Inheritance Class Diagram

  11. What about these? Class Student : private Person { … Class Student : protected Person { …

  12. Effect of Base Access How base class members appear in derived class Base class members private: x protected: y public: z private base class xinaccessible private: y private: z protected base class private: x protected: y public: z x inaccessible protected: y protected: z public base class private: x protected: y public: z xinaccessible protected: y public: z

  13. Order of Execution • When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor • When an object of a derived class is destroyed, its destructor is called first, then that of the base class

  14. Passing Arguments to Base Class Constructor class Parent { int x, y; public: Parent(int,int); }; class Child : public Parent { int z; public: Child(int a): Parent(a,a*a) {z = a;} };

  15. Examples • Static variables: 11-02, 11-03 • Friend: 11-04 • Copy constructor: 11-05, 11-08 • Composition: 11-17 • Inheritance • using Person, Student & Advisor: 11-19, 11-21 • constructors: 11-20

More Related