150 likes | 294 Views
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:
E N D
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: int x; friend void fSet(aClass &c, int a); }; void fSet(aClass &c, int a) { c.x = a; }
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; } };
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;} };
Friends • Why use it? • Really a good idea?
Object Composition class StudentInfo { private: string firstName, LastName; string address, city, state, zip; ... }; class Student { private: StudentInfo personalData; ... };
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.
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
Base Class Derived Class Inheritance Syntax and Notation // Existing class class Base { }; // Derived class class Derived : public Base { }; Inheritance Class Diagram
What about these? Class Student : private Person { … Class Student : protected Person { …
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
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
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;} };
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