Download presentation
Presentation is loading. Please wait.
Published byEzra Craig Modified over 8 years ago
1
Chapter 11 More about classes and OOP
2
Relationships Between Classes Possible relationships – Access ("uses-a") – Ownership/Composition ("has-a") – Inheritance ("is-a") 7-2
3
Friend Function Declarations 1)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; } 11-3
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; } }; 11-4
5
Friend Class Declaration 3)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;} }; 11-5
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;... }; 11-7
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
Inheritance Syntax and Notation // Existing class class Base { }; // Derived class class Derived : public Base { }; Inheritance Class Diagram 11-10 Base Class Derived Class
11
What about these? Class Student : private Person { … Class Student : protected Person { …
12
Effect of Base Access 11-12 private: x protected: y public: z private: x protected: y public: z private: x protected: y public: z Base class members x inaccessible private: y private: z x inaccessible protected: y protected: z x inaccessible protected: y public: z How base class members appear in derived class private base class protected base class public base class
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 11-13
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;} }; 11-14
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.