Session_9 - Lesson 11 Inheritance A dog inherits that is it...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: Lesson 11 - Inheritance A dog inherits, that is, it gets all the features of a mammal. Because it is a mammal, we know that it moves and it breathes air. All mammals, by definition, move and breathe air. The concept of a dog adds the idea of barking, wagging its tail, etc. Dogs can be divided into working dogs, sporting dogs, etc and we can further divide sporting dogs into retrievers, spaniels, etc. C++ attempts to represent these relationships by enabling you to define classes that derive from one another. Derivation is a way of expressing the is-a relationship. You derive a new class, Dog, from the class Mammal. You don’t have to state explicitly that dogs move because they inherit that from Mammal. A class that adds new functionality to an existing class is said to derive from that original class. The original class is said to be the new class’s base class. If the Dog class derives from the Mammal class, then Mammal is a base class of Dog. Derived classes are supersets of their base classes. Just as dog adds certain features to the idea of mammal, the Dog class will add certain methods or data to the Mammal class. Typically, a base class will have more than one derived class. Because dogs, cats, and horses are all types of mammals, their classes would all derive from the Mammal class. Page 1 of 23 Animal Mammal Reptile Horse Dog Working Sporting Terriers Retrievers Spaniels Labradors Goldens When you declare a class, you can indicate what class it derives from by writing a colon after the class name, the type of derivation (public or otherwise, discussed later), and the class from which it derives. An example is shown below, just including the declarations: #include <iostream> using namespace std; enum BREED { GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB}; class Mammal { public : // constructors Mammal() {}; ~Mammal() {}; // accessors int GetAge() const ; void SetAge ( int ); int GetWeight() const ; void SetWeight(); // other methods void Speak() const ; void Sleep() const ; protected : int itsAge; int itsWeight; }; class Dog : public Mammal { public : // Constructors Dog() {}; ~Dog() {}; // Accessors BREED GetBreed() const ; void SetBreed(BREED); // other methods void WagTail(); void BegForFood(); protected : BREED itsBreed; }; int main( void ) { return 0; } Note that Mammal does not derive from any other class; the hierarchy begins with Mammal. The Dog class inherits from Mammal; every Dog object will have three member variables—itsAge, itsWeight, and itsBreed. Note that the class declaration does not include the member variables itsAge and itsWeight. Dog objects inherit these variables from the Mammal class, along with all Mammal’s methods except the copy operator and the constructors and destructor ....
View Full Document

This note was uploaded on 09/12/2011 for the course CS 175 taught by Professor Mike during the Spring '06 term at Golden West College.

Page1 / 23

Session_9 - Lesson 11 Inheritance A dog inherits that is it...

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

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