Review of Inheritance 2 Inheritance Hierarchy Base Class
Review of Inheritance
2 Inheritance Hierarchy Base Class B Private Protected Public Derived class D : public B Private Protected Public
3 Inheritance: Access Types Base Class B Private Protected Public Derived class D : public B Private Protected Public Inherited but cannot access private members of B within D
4 Inherited Member Access Base Class B Private Protected Public Derived class D : public B Private Protected Can access Public protected members of B within D Private Protected Public
5 Inherited Member Access Base Class B B. Private B. Protected B. Public Derived class D : public B Private Protected Can access Public protected members of B within D, cannot access these in main() Private Protected D obj; Public Obj. protected_member;
6 Several Levels of Inheritance Base Class B Derived class D 1
7 Constructors Base Class B B b; Derived class D D d; Derived class D 1 d 1; // constructors for //B, D, D 1 called
8 Which Constructor ? § The default constructor. § If you want another one – use initializer lists D : : D(int m, int n) : B(int n){ …}
9 Inheriting and Overriding Functions Base Class B public: void print(){cout <<“B”} Derived class D void print() {cout << “D”} overriden Derived class D 1 obj; obj. print();
10 Inheriting and Overriding Functions Base Class B void print(){cout <<“B”} Derived class D void print() {cout << “D”} overriden Derived class D 1 obj; obj. print(); -- “D” the one closest in the hierarchy
11 Inheriting and Overriding Functions Base Class B void print(){cout <<“B”} Derived class D not overriden Derived class D 1 which one is it? D 1 obj; obj. print();
12 Inheriting and Overriding Functions Base Class B void print(){cout <<“B”} Derived class D not overriden Derived class D 1 which one is it? D 1 obj; obj. print(); “B” the one closest in the hierarchy
13 Type Conversions Base Class B B b; //is not of type D or D 1 Derived class D D d; //is also of type B Derived class D 1 d 1; //is also of type D, B
14 Assignment b = d Base Class B Derived class D D d; //is also of type B Derived class D 1 d 1; //is also of type D, B
15 Assignment d = b Base Class B B b; //is not of type D Derived class D D d; //is also of type B Derived class D 1 d 1; //is also of type D, B
16 Function calls Base Class B void print() {cout << “B”} Derived class D void print() {Base: : print(); } Derived class D 1 D d; //is also of type B
17 Function calls Base Class B void print() {D: : print(); } B b; //is not of type D Derived class D void print() {Base: : print(); } D d; //is also of type B Derived class D 1 d 1; //is also of type D, B
18 Use of Virtual (Dynamic Binding) § To enable programming with pointers to different object types § The compiler generates code to: inspect the type of the object the pointer points to at runtime and then call the appropriate function D* D 1
19 Use of Virtual (Dynamic Binding) D *ptr = new D 1(); ptr f(); //dynamic binding – calls f from D 1 For dynamic binding to occur for function f: - must use pointers or references - f must exist in D - f must be declared virtual in D
20 Use of Virtual (Dynamic Binding) D d; d. f(); //static binding – calls f from D For dynamic binding to occur for function f: - must use pointers or references - f must exist in D - f must be declared virtual in D
21 Use of Virtual (Dynamic Binding) D *ptr = new D 1(); ptr print(); //dynamic binding – calls print from D 1 For dynamic binding to occur for function print: - must use pointers or references - print must exist in D (could be implemented or pure virtual) - print must be declared virtual in D
22 Use of Virtual (Dynamic Binding) D *ptr = new D 1(); ptr print(); //dynamic binding – calls print from D 1 Make sure you go through the steps: 1. Find type of ptr (D* here) 2. Look in that class (D here) for the function (print) 3. If print - is not there (neither declared in D nor inherited from B) compiler error ! - is there and is virtual (either declared virtual explicitly or inherited from B) dynamic binding (print from D 1) - is there and not virtual static binding (print from D)
23 Inheritance with Virtual Functions Base Class B virtual void print(){cout <<“B”} Derived class D not overriden Derived class D 1 void print(){cout <<“D 1”; } which print() is called? D *ptr = new D 1; ptr->print(); “D 1” Dynamic binding
24 Inheritance with Virtual Functions Base Class B virtual void print(){cout <<“B”} Derived class D not overriden Derived class D 1 not overriden Dynamic binding occurs but print is not overriden in D 1, it is inherited as is from B which one is it then ? D *ptr = new D 1; ptr->print(); “B” the one closest in the hierarchy
25 Inheritance with Virtual Functions Base Class B void print(){cout <<“B”} Derived class D void print() {cout << “D”} overridden Derived class D 1 Dynamic binding occurs but print is not overridden in D 1 which one is it? D *ptr = new D 1; ptr->print(); -- “D” the one closest in the hierarchy
26 Virtual Destructors ! Base Class B ~B(); Derived class D ~D(); Derived class D 1 ~D 1(); which destructor is called ? D *ptr = new D 1; Delete ptr;
27 Virtual Destructors ! Base Class B ~B(); Derived class D ~D(); Derived class D 1 Static binding occurs, because ~D is not virtual ~D 1(); which destructor is called ? D *ptr = new D 1; Delete ptr; //~D and ~B called
28 Virtual Destructors ! Base Class B ~B(); Derived class D virtual ~D(); Derived class D 1 Dynamic binding occurs, because ~D is virtual ~D 1(); which destructor is called ? D *ptr = new D 1; Delete ptr; //~D 1, ~D and ~B
29 Virtual Destructors ! Base Class B virtual ~B(); Derived class D ~D(); Derived class D 1 ~D 1(); which destructor is called ? D *ptr = new D 1; Delete ptr; //~D 1, ~D and ~B
- Slides: 29