Project • Othello – submitted. • Next submission: C++: Inheritance III – Team Evaluations – Nov 10 th – Please don’t forget – If solo – give yourself a good evaluation! Advanced Topics – Indicate if okay to share feedback with partner • Default: NO. • That’s today! Project Plan • Project • This week…in the home stretch – Grading done during exam week – Inheritance – The “Ethics” lecture – Will send grades via e-mail. • Open Source / Free Software / GNU – Final Review – Grades will be available on mycourses after quarter is over. – Questions? The final exam Before we begin • Other finals review • Any questions? – CSH Review • November 17 th – 7pm – 10pm • Gracies – Recall • Final: November 18 th • 12:30pm – 2:30pm • 70-3690
Plan for today Subclassing and Inheritance • Inheritance • When you define a class as a subclass: – What goes on behind the scenes – The subclass inherits all of the data members and methods of the superclass. – In addition, a subclass can have data/methods – What’s up with the syntax: that are it’s own. – Inheritance is transitive: virtual void foo () = 0; • I.e. If B is a subclass of A and C is a subclass of B, then C inherits the data/methods from both B and A. Inheritance Subclassing • Define a more general class “Performer”. • Behind the scenes • Both Actors and Musicians are specializations of – The memory allocated for an object of a Performer derived class consists of: • An area for the base class’s data superclass Performer • An area for the derived class’s data isA isA Actor Musician subclasses Inheritance Inheritance class Performer class Musician : public • What the memory for Musician looks like Performer { { private: private: float basePay; Instrument *axe; char *name; Performer bool isRecorded; basePay char *talent; … data … public: name public: Musician (char *name); talent Performer (char *name, char virtual float * talent); calculatePay(); Musician virtual void virtual float axe setInstrument (Intrument calculatePay(); data *I); } isRecorded }
Inheritance Inheritance Musician basePay • Let’s add a Drummer • What the memory for data name (includes class Drummer : public Musician drummer looks like talent Performer { data) private: axe Drum kit[]; isRecorded public: Drummer (char *name); Drummer virtual void setInstrument kit data (Instrument *I); } Virtual Functions Virtual Functions • What about virtual functions? – C/C++ allows one to define pointers to functions. – For all classes with virtual functions (defined or redefined), the compiler will create an array of pointers to virtual functions (VTABLE) – Objects of such classes have a hidden data member (vpt) which will point to the correct VTABLE array. • Note: suggested, not required implementation – Your mileage may vary. Virtual Functions Virtual Functions • vtable created at compile time – Initialized with values of Base class unless overriden by derived class. • vptr set at run time. – Each object (derived or otherwise) will have a SINGLE vptr. – During construction – Based on actual derived class.
Virtual Functions Pure virtual functions • Calling virtual functions • What happens if you have a pure virtual function? – In the original C++ interpretter: – Virtual functions are nothing but entries in an array of function pointers. Instrument *I = new Instrument(); – Pointers are pointers I->play(); • I.e. All pointers can be set to NULL. – Thus, the syntax: Was converted to virtual void foo () = 0; ((I->__vptr)[0]) (I); Pure virtual functions Virtual Functions and Constructors • If a Base class defines a pure virtual • Why you should not call virtual functions in function not overridden by the derived a constructor class: – Recall: – That entry in the derived class’s VTABLE will • When a member of a derived class is constructed, the constructor of it’s base class is called first be 0. – This fills in the memory area containing members of the – Of course, the compiler will catch this. base class – This includes the setting of the vptr – The base constructor will conclude before the derived constructor begins. Virtual Functions and Constructors Virtual Functions and Constructors class A { • Let’s assume that A’s constructor calls func1(). public: – What happens after statement A(); ~A(); • AA *myAA = new AA(); virtual void func1(); virtual void func2(); virtual void func3() = 0; 1. AA::AA() calls A::A() } 2. A::A() sets vptr to A’s VTABLE class AA : public A { 3. A::A() executes (calls A’s func1()) public: AA(); 4. AA:AA() sets vptr to AA VTABLE ~AA() 5. AA:AA() executes and returns. void func1(); void func3(); }
Slicing Virtual Functions and Constructors • It’s worse if A’s constructor calls func3(). • Recall that polymorphism can only be achieved using pointers rather than objects – What happens after statement themselves. • AA *myAA = new AA(); • Attempts to copy a base class object with a 1. AA::AA() calls A::A() derived class object will cause slicing. 2. A::A() sets vptr to A’s VTABLE – Only the base class section of the derived object 3. A::A() executes (calls A’s func3()=0) will be copied. 4. core dumped and compiler will NOT catch this. Slicing Virtual Functions and Slicing • During copy construction, Musician M (“Ringo”); basePay basePay – The vptr will get set to the VTABLE for the class of object being copied name name Performer P (M); Musician M (“Ringo”); talent talent Performer P (M); Space allocated for P axe P – Performer’s copy constructor called isRecorded Copy constructor called • P is a Performer • vptr points to Performer’s VTABLE • virtual functions are Performer’s. M Correct Polymorphism Virtual Functions and Destructors • Why define destructors to be virtual Musician *M (new M basePay Musician (“Ringo”)); – The destructor of a derived class will always name P call the destructor of it’s Base class. talent Performer *P (M); axe – However, even with correct polymorphism, M allocated on heap there’s no guarantee that the derived class’s isRecorded Pointer copy. destructor will be called. heap M still has Musician’s vptr since no copy constructor was called.
Virtual Functions and Destructors Virtual Functions and Destructors • If Base’s destructor was declared as virtual. Base *B = new Derived(); ... delete B; // Base::~Base() called class Base { // Derived part of B never gets cleaned public: up Base(); virtual ~Base(); } • A pointer to the destructor will be placed in the VTABLE. – This pointer will get overridden by derived class’s destructor Remainder of course Virtual Functions and Destructors • Tomorrow: Open Source / GNU Base *B = new Derived(); ... • Thursday: Final Exam Review. delete B; // Destructor of B is found in the VTABLE pointed to // by B’s vptr (namely ~Derived) • Questions? // Derived destructor is called // which in turn will call Base’s destructor. Questions?
Recommend
More recommend