Implement a inheritance hierarchy(cont.) Derived class calling int main( cout <<"Create an Audio Book object! < endl Audio Book ab(" Man Without Quarlities”,“ Robert musil”,“ Kenneth Meyer") global print(ab)
Implement a inheritance hierarchy (cont.) ◼ Derived class calling int main() { cout << “Create an AudioBook object!”<< endl; AudioBook ab(“Man Without Quarlities”, “Robert Musil”, “Kenneth Meyer”); global_print(ab); };
Implement a inheritance hierarchy(cont.) Result. Create an Audio Book object LibMat Default Constructor!//constructing Book(Man Without Quarlities, Robert Musil) Constructor AudioBook(Man Without Quarlities, Robert Musil, Kenneth Meyer) Constructor! This is an Audio Book object! /print Title: Man Without Quarlities Author: robert musil Narrator: Kenneth Meyer Audio Book Destructor! /destructing Book Destructor LibMat Destructor
Implement a inheritance hierarchy (cont.) Result: Create an AudioBook object! LibMat Default Constructor!//constructing Book(Man Without Quarlities, Robert Musil) Constructor! AudioBook(Man Without Quarlities, Robert Musil, Kenneth Meyer) Constructor! This is an AudioBook object! //print Title: Man Without Quarlities Author: Robert Musil Narrator: Kenneth Meyer AudioBook Destructor! //destructing Book Destructor! LibMat Destructor!
Implement a inheritance hierarchy(cont.) When constructing a derived class object, every constructor is executed in the inheritance order u When destructing, in reverse order For a derived object, no difference between its own members and inherited members int main( Audio Book ab(“ Mason and dixon",“ Thomas Pynchon”,“ Edwin leonard") cout <<ab title( < endl; cout << ab author(<< endl cout < ab narrator( < end;
Implement a inheritance hierarchy (cont.) ◼ When constructing a derived class object, every constructor is executed in the inheritance order ◼ When destructing, in reverse order ◼ For a derived object, no difference between its own members and inherited members int main() { AudioBook ab(“Mason and Dixon”, “Thomas Pynchon”, “Edwin Leonard”); cout << ab.title() << endl; cout << ab.author() << endl; cout << ab.narrator() << endl; };
Story of inheritance polymorphism What's beneath the virtual function mechanism and overriding?- Layout of objects Virtual table(vtbl) and virtual table pointer(vptr) Every class maintains its vtbl Every object contains a vptr pointing to the vtbl of this class B's vtbl class B Object B Bs type info object [public Data members Impl of B: B B(; for the object Impl of B: mfl virtual -BO virtual void mf10 Objects vptr Impl. of B: mf2 Impl. of B: mf3 virtual int mf2 (char c)const, Object B virtual void mf3(string& s); void mf40 Data members for the object Object's vptr
Story of inheritance & polymorphism ◼ What’s beneath the virtual function mechanism and overriding? – Layout of objects ◼ Virtual table (vtbl) and virtual table pointer (vptr) ◼ Every class maintains its vtbl ◼ Every object contains a vptr pointing to the vtbl of this class class B {public: B(); virtual ~B(); virtual void mf1(); virtual int mf2(char c) const; virtual void mf3(string& s); void mf4(); } B’s type_info object Impl. of B::~B Impl. of B::mf1 Impl. of B::mf2 Impl. of B::mf3 B’s vtbl Data members for the object Object’s vptr Object B Data members for the object Object’s vptr Object B
Story of inheritance polymorphism (cont u How the vtbl and vptr take effects in inheritance class D: public B D Object [public Data members D's vtbl DO for the object virtual -DO D's type info object virtual void mf10 Objects vptr Impl. of D: D Impl. of d: mfl virtual void mf5(char* str); D Object Impl. of B: mf2 Data members Impl of B: mf3 B object for the object Impl of d: mt5 Data members B's vtbl Objects vptr for the object B's type info object Object s vptr Impl of B: B B Obiect Impl. of B: mfI Impl. of B: mf2 Data members Impl. of B: mf3 for the object Object's vptr
Story of inheritance & polymorphism (cont.) ◼ How the vtbl and vptr take effects in inheritance? class D : public B {public: D(); virtual ~D(); virtual void mf1(); virtual void mf5(char* str); } B’s type_info object Impl. of B::~B Impl. of B::mf1 Impl. of B::mf2 Impl. of B::mf3 B’s vtbl Data members for the object Object’s vptr B Object Data members for the object Object’s vptr B Object D’s type_info object Impl. of D::~D Impl. of D::mf1 Impl. of B::mf2 Impl. of B::mf3 Data members D’s vtbl for the object Object’s vptr D Object Data members for the object Object’s vptr D Object Impl. of D::mf5