Logistics • Project C++: Inheritance III – Part 2 (water) due Sunday, Oct 16 th • Questions? Advanced Topics Logistics Plan for this week • Final exam • Today: Intro to Memory Management – Good news…bad news • Tomorrow: Memory Problems – Good news • Thursday: Standard Libraries • Last day of finals, November 18 th – Advanced Inheritance – Bad news • 8am-10am – Room • 01-3338 Before we begin Standard Libraries • Standard “C” libraries • Any questions? – http://www.acm.uiuc.edu/webmonkeys/book/c_ guide/ • Standard C++ libraries – Standard C libraries – iostream – STL – http://www.cplusplus.com/ref/ 1
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 } 2
Inheritance Inheritance Musician basePay • Let’s add a Drummer data • What the memory for 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. 3
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: A(); – What happens after statement ~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 { public: 3. A::A() executes (calls A’s func1()) AA(); 4. AA:AA() sets vptr to AA VTABLE ~AA() 5. AA:AA() executes and returns. void func1(); void func3(); } 4
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 Musician (“Ringo”)); basePay – 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. 5
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? 6
Recommend
More recommend