CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design
CSC 143 O 2 Public Inheritance means "isa" If class D publicly inherits from class B, every object of type D is also an object of type B (not vice-versa) class Person{…}; class Student: public Person {…}; void dance(const Person& p); // anyone can dance void study(const Student& s); // only students study Person p; Student s; // p is a Person, s a Student dance(p); // fine, p is a Person dance(s); // fine, s is a Student, therefore isa Person study(s); // OK study(p); // error! p is not a Student
CSC 143 O 3 IsA Design (1) Trouble: penguins can fly! C++ is stricter than the English language. In C++, "Birds can fly" means "All birds can fly" Solution? class Bird{ public: virtual void fly() // birds can fly … }; class Penguin: public Bird{ // penguins are birds … };
CSC 143 O 4 IsA Design (2) Refine the hierarchy NonFlyingBird Penguin Bird (no fly function) FlyingBird (fly function declared here)
CSC 143 O 5 IsA Design (3) Could also do: Penguins can fly, but it is an error for them to try to do so. How? class Bird {… //no fly function}; class FlyingBird : public Bird{ virtual void fly(); … }; class NonFlyingBird{ … //no fly function}; class Penguin: public NonFlyingBird{ … // no fly function};
CSC 143 O 6 Interface Implementation Interface = function declaration Implementation = code for the function Member function interfaces are always inherited. If a function applies to a class, it must also apply to its subclasses. If you want the subclasses to inherit the interface only, make the member function pure virtual (virtual … = 0) the interface and a default implementation, make the member function virtual Non virtual means inherit the interface as well as a mandatory implementation
CSC 143 O 7 Example: A Shape Class class Shape { public: virtual void draw() const = 0; // Abstract class virtual void error(const string& msg); int ObjectID() const; … }; class Rectangle: public Shape{…}; class Ellipse: public Shape{…}; Rectangle and Ellipse must provide a draw function Every class must support an error function (but you don't have to write your own if you don't want to) Every object has an ID. That ID is determined by the definition of Shape::objectID. Don't change it!
CSC 143 O 8 Virtual Destructor Make sure that base classes have a virtual destructor If D is derived from B, what happens in the following? B* pB = new D; … delete pB; If you try to delete a derived class object through a base pointer and the base class doesn't have a virtual destructor, the result is undefined.