logistics
play

Logistics Project C++: Inheritance II Part 2 (water) due Sunday, - PDF document

Logistics Project C++: Inheritance II Part 2 (water) due Sunday, Oct 16 th Questions? Plan for this week Subclassing Define a more general class Performer. This is abstraction week Both Actors and


  1. Logistics • Project C++: Inheritance II – Part 2 (water) due Sunday, Oct 16 th • Questions? Plan for this week Subclassing • Define a more general class “Performer”. • This is “abstraction week” • Both Actors and Musicians are specializations of – Monday: Inheritance in C++ Performer – Tuesday: Exam – Today: Inheritance in C++ (cont’d) superclass Performer isA isA Actor Musician subclasses Class Heirarchies Subclassing and Inheritance • Class heirarchies can be as deep as needed: • When you define a class as a subclass: – The subclass inherits all of the data members Performer and methods of the superclass. isA isA – In addition, a subclass can have data/methods that are it’s own. Actor Musician – Inheritance is transitive: • I.e. If B is a subclass of A and C is a subclass of B, Guitarist Pianist Drummer then C inherits the data/methods from both B and A. 1

  2. Inheritance Inheritance class Musician : public class Performer • Behind the scenes Performer { { private: – The memory allocated for an object of a private: float basePay; derived class consists of: Instrument *axe; char *name; bool isRecorded; char *talent; • An area for the base class’s data … … public: • An area for the derived class’s data public: Musician (char *name); Performer (char *name, char virtual float calculatePay(); * talent); virtual void virtual float setInstrument (Intrument calculatePay(); *I); } } Inheritance Inheritance • Let’s add a Drummer • What the memory for Musician looks like class Drummer : public Musician { Performer private: basePay data Drum kit[]; name public: talent Drummer (char *name); Musician virtual void setInstrument axe data (Instrument *I); isRecorded } Inheritance Inheritance and Construction Musician basePay data • What the memory for • When a member of a derived class is name (includes drummer looks like constructed, the constructor of it’s base talent Performer class is called first data) axe – This fills in the memory area containing isRecorded members of the base class. Drummer kit data 2

  3. Inheritance and Construction Inheritance and Construction basePay name Musician *M = new Musician(“Ringo”); Drummer *D = new Drummer(“Ringo”); basePay talent Drummer’s constructor is called name Musician’s constructor is called axe talent Musician’s constructor is called isRecorded Performer’s constructor is called Performer’s constructor is called axe Musician’s constructor continues Musician’s constructor continues isRecorded kit Drummer’s constructor continues Slicing Constructing Derived Class Objects Musician::Musician (char *name) : • Recall that polymorphism can only be Performer (name, “music”), axe (0), achieved using pointers rather than objects isRecorded(false),... { themselves. } • Attempts to copy a base class object with a • There is no super function in C++ derived class object will cause slicing. • Call to base class constructor required unless base – Only the base class section of the derived object class has a default constructor. will be copied. • Questions? Slicing Correct Polymorphism Musician *M (new Musician M (“Ringo”); M basePay basePay basePay Musician (“Ringo”)); name name name Performer P (M); P talent talent talent Performer *P (M); Space allocated for P axe axe P M allocated on heap isRecorded isRecorded Copy constructor called Pointer copy. M heap 3

  4. Slicing Multiple Inheritance • Questions? • In C++, a class can be derived from more than 1 base class. • Recall that C++ has no notion of an interface. Multiple Inheritance Multiple Inheritance class MusicBuyer { private: Performer MusicBuyer float cashOnHand; isA isA … Actor Musician Radio Station public: MusicBuyer (float cash); … } Multiple Inheritance Multiple Inheritance class Musician : public Performer, public • Objects of classes that have multiple base MusicBuyer classes will have a data section for each { public: Base class. Musician (char *name); – The constructor for each base class will need to … } be called in the constructor for the derived class. Musician::Musician (char *name) : Performer (name, “music”), MusicBuyer(100.0), ... { } 4

  5. Multiple Inheritance Multiple Inheritance basePay • Why some think Multiple Inheritance is evil Musician *M = new Musician(“Ringo”); name – Data member ambiguity talent – Can possibly derive from a base class twice – Extra work for compiler. Musician’s constructor is called cashOnHand – Most multiple inheritance heirarchies can be done using Performer’s constructor is called single inheritance MusicBuyer’s constructor is called • Java chose to disallow axe Musician’s constructor continues – Created interfaces instead – I suggest you do the same! isRecorded Multiple Inheritance Summary • Questions • Inheritance – What really goes on… – Slicing • Multiple Inheritance • Questions 5

Recommend


More recommend