This post will introduce inheritance and polymorphism in C++.

Inheritance General Idea

  • Inheritance allows us to create new classes from already defined classes
  • This creates a Parent->Child relationship with the child gaining everything that is defined in the parent
  • This is known as an "IS A" relationship

    • Chair "IS A" Furniture

Standard Inheritance

  • No keywords for inheritance in C++
  • We simply use the : operator

    • Note: this is a single-colon, not the double colon that makes up the scope-operator.
class student : public person

Inheritance Modifier

  • public
  • protected
  • private (rarely use)

Inheritance Modifier encapsulated the visibility of the things in the parent within the child.

Public Inheritance

  • Public members in parent class are inherited as public in the new class
  • Protected members in parent class are inherited as protected in the new class
  • Private members in parent class remain private and cannot be directly accessed but can be accessed through public and protected members (interface) of the base-class.

When to use?

This is the most common way to do inheritance.

Protected Inheritance

  • Public members in parent class are inherited as protected
  • Protected members in parent class are inherited as protected
  • Private members in parent class remain private.

Note: Since the public interface is now protected, you can not access the interface making it seem like class isn't "is-a".

When to use?

If the child wants to have their own public interface, and it wants other programmers exactly act on this child interface. Not the interface from parent classes.

Private Inheritance

  • Every (Public/Protected/Private) members in parent class are inherited as private in the new class.

No longer "IS A" relationship. It now described as "IMPLEMENTED IN TERMS OF" relationship

Private Inheritance

When to use?

  1. I want to stop others from inheriting my class. Like a stopping point. You can still do inheritance, and it exists in your child class. However, they are inaccessible directly.
  2. I use some of the interfaces from parent class, but users don't have to use it.

    "If you make a class D privately inherit from a class B, you do so because you are interested in taking advantage of some of the features available in c lass B, not because there is any conceptual relationship between objects of types B and D."


Inheritance Type Summary


class Rectangle: public Polygon
    Rectangle (int a, int b):Polygon(a,b){}
    int area ()
      { return width*height; }

Rectangle (child) invokes Polygon (parent) constructor. Be careful about single-colon :.

Use Super (Parent) Class Methods

void child::print(int x)

We use the scope-operator ::. ==annoying==

More Inheritance

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hieratical Inheritance
  • Hybrid Inheritance

Single Inheritance

Single Inheritance

Multiple Inheritance

  • A class inherits from multiple different parents.

Multiple Inheritance

Multilevel Inheritance

  • "IS A" relationship through multiple levels of inheritance

Multilevel Inheritance

Hieratical Inheritance

  • One parent class inherits multiple child classes.
  • This is usually where Polymorphism is expressed the most.

Hieratical Inheritance

Hybrid Inheritance

  • This combines Multiple, Multilevel and Hierarchal inheritance to create a hybrid child class

Hybrid Inheritance


Polymorphism is change at the same level of inheritance

  • Automobile -> Car -> Type of Car

Few primary mechanisms for polymorphism:

  • "IS A" relationship

    • one class inherits from the other class
    • Objects that "IS A" another objects can be stored in references to the parent object
  • Overriding **(NOT Overloading)**

    • Overriding is when we redefine a method that exists in the parent class with a specialized version in child class

Polymorphism Overriding

In Java, we can use the tag @Override to override a method. But it basically happens automatically.

In C++...

  • Virtual Keyword

    • It allows us to mark a method as overridable


What is Virtual keyword?

  • Without the virtual keyword, the execution will default to the parent method’s definition

    • This is known as static resolution, static linkage or early binding
  • The virtual keyword tells the compiler to examine the nature of the object and call the appropriate method instead

    • This is known as dynamic resolution or dynamic linkage or late binding

Code Example:

Early binding - without virtual keyword:

poly demo code early binding

poly demo code early binding run

Without virtual keyword, storing the child object in the parent pointer results in an unsuccessful override.

With virtual keyword:

poly demo code virtual

poly demo code virtual run

Pure Virtual

  • We can create a method in the parent class to be default behavior (Some method definition).
  • We can also create a method with no method definition, also called Abstract Base or Pure Virtual Method

    • Pure Virtual method has no definition explicitly.
    • It exists only for the child to implement.
    • It is still defined in the parent so every child must reliably have this method.
  • When we declare them, we actually use a special syntax:

    • virtual int myMethod(<parameters>) = 0;
    • The = 0 portion of the declaration tells the compiler that the method has no body explicitly.

Consequences to using Pure Virtual Method:

  • Any class that contains a declaration for a pure virtual method is labeled an Abstract Base Class.
  • Abstract Base Classes CANNOT be instantiated.

    • However, you can instantiate their child class (NOT a abstract base class), and store it into their parent (virtual) class pointer.
// Assume A is an abstract base class
// B is A's child class, and NOT an abstract base class

A* aTest = new A(); // Will cause error
B* bTest = new B(); // OK
A* parent = new B(); //OK
  • Any class that inherits from the Abstract Base Class must provide a definition for the Pure Virtual methods.
  • If they do not … they are also considered an Abstract Base Class and suffer consequence one

Pure Virtual … with a body

  • We can mark a method as Pure Virtual (= 0) and provide a body!

    • Java interfaces cannot have a body in their methods.
    • C++ lets us define a body for default behavior
virtual const char* speak() = 0 
// The = 0 means this function is pure virtual
    return “hello"; // even though it has a body
  • You cannot explicitly call this method, because you cannot instantiate an abstract base classes.
  • But the child class can call the parent class.
virtual const char* speak() 
// this class is no longer abstract because we defined this function
    return Animal::speak(); // use Animal's default implementation

Pure Virtual - Interface

  • In C++ we can create interfaces through the use of Pure Virtual Methods and multiple inheritance.
  • No keyword in C++, unlike Java use Interface Keyword.