36S-Section-Solution

36S-Section-Solution - CS106X Winter 2008 Handout 36S...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Handout 36S Winter 2008 March 5-7, 2008 Section Solution Problem 1: Inheritance Design The first thing we need to do is set up Animal to be subclass-able given our needs. This means that since each type of Animal moves differently, move should be a virtual function, allowing them to override it. Furthermore, since any generic Animal can’t really know how to move without knowing more about its construction, we will make move a pure virtual function with the = 0 syntax. This will prevent us from instantiating an the Animal class itself, but we will be able to make its subclasses once they have implemented move . Then, to accommodate the different numbers of legs, we will need to make numLegs a protected variable instead of a private one. Because all of our creatures have two eyes, breathe in the same basic way, and can report their color , those functions can remain as is. class Animal { public: Animal(string color); void breathe(); virtual void move() = 0; int getNumLegs(); int getNumEyes(); string getColor(); private: string color; protected: int numLegs; }; Animal::Animal(string color) { this->color = color; numLegs = 4; } void Animal::breathe() { //breathing code
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

36S-Section-Solution - CS106X Winter 2008 Handout 36S...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online