Chapter 13. Inheritance

In C++ a class can derive from zero or more base classes.

Unlike Other languages (ObjC, Java), there is no common base class. A class that is not derived is really not derived.

derived class (subclass)

a class with at least one base class

direct base classes (direct superclasses)

are the classes are the classes that are immediate base classes

indirect base classes (indirect superclasses)

are the classes that are superclasses of the direct base classes (and their superclasses, etc.)

Remember: A base class inherits all the functions and variables of all of its superclasses!

Inheritance in C++:

class Max { ... };
class SomeClass { ... };
class LittleMax : public Max { ... };
class MultiDerived : public Max, public SomeClass { ... };
class LinearDerived : public LittleMax { ... };

To declare inheritance:

between the class name and the opening brace, insert a colon, the keyword public, and the name of the base class.

There are also other types of inheritance (other than "public") which we will talk about later. Other OO languages (ObjC, Java) only have public inheritance

If you have multiple direct base classes, join them with a comma.

Practice:

Define these two classes with inheritance. You may omit all contents of the actual class (the attribtues and methods), I am only interested in the definition line (as in the example).

Virtual

Remember when we talked about polymorhpism and function overriding? Unfortunately, C++ is a grown languages, and therefore does not allow function overriding by default. We need to do two things:

  • Use dynamic object references. If we use static references, classes get "sliced". This is a very strange concept. If your interested, read page 156 of C++ in a Nutshell. Just remember: To use function overriding, and to make proper use of inheritance, always use dynamic references.

  • Functions to be overridden or that are overridden are declared with the "virtual" specifier (only in the declaration, not the implementation).

In other languages (ObjC, Java), virtual is the default.

Therefore, for this class:

  • Specify ALL functions (including destructor, excluding constructor) as virtual in all classes that are subclasses or superclasses!

Example 13.1. Example: Implementation of polygon

Here are the class definitions:

class Polygon {
private:
  vector<Location *> points;
public:
  Polygon(vector<Location *> points);
  virtual void draw();
  virtual float getArea();
};
class Triangle : public Polygon {
public:
  Triangle(Location *p1, Location *p2, Location *p3);
  virtual float getArea();
};
class Rectangle : public Polygon {
public:
  Rectangle(Location *topLeft, Location *bottomRight);
  virtual float getArea();
};
class Hexagon : public Polygon {
public:
  Hexagon(Location *center, float radius);
};

And some code that uses these classes:

// assume v1 is a vector<Location *> with some useful values.
// assume p1,p2, .. are Location* with useful values;

Polygon *pg1 = new Polygon(v1);
Triange *t1 = new Triangle(p1,p2,p3);
Rectangle *r1 = new Rectangle(p4,p5);

// Using oo:
Polygon *pg2 = new Triangle(p6,p7,p8);
Polygon *pg3 = new Rectangle(p9,p10);
Polygon *pg4 = new Hexagon(p11,1.0);

cout << pg1->getArea() << endl; // calls getArea() from Polygon
cout << pg2->getArea() << endl; // calls getArea() from Triangle
cout << pg3->getArea() << endl; // calls getArea() from Rectangle
cout << pg4->getArea() << endl; // calls getArea() from Polygon

Practice:

Define these three classes. Show the class definitions and the definitions for the methods given here (all other methods / attributes / constructors / etc. may be ommited)

class Aircraft { 
public: 
  virtual void startEngines(); 
};

class Boing747 : public Aircraft {
};

class Glider : public Aircraft { 
public: 
  virtual void startEngines();
};