Inheritance

Inheritance allows a class (derived) to inherit the members and methods of another (base).

class Animal {          // Base class
	public:
	    std::string species;
	    void eat() { std::cout << "eating..."; }
};
 
class Dog : public Animal { // Dog inherits from Animal
	public:
	    void bark() { std::cout << "Woof!"; }
};
Dog d;
d.species = "canine";  // inherited member
d.eat();               // inherited method
d.bark();              // own method

Inheritance types

The keyword between : and the base name controls the visibility of inherited members:

Inheritance typeEffect on public members of the base
publicRemain public
protectedBecome protected (only for derived classes)
privateBecome private (not accessible)

Note

public is the most common inheritance (“is-a”). protected allows access only from subclasses. private is rarely used (composition).

protected

A protected member is accessible from the class itself and its derived classes, but not from outside:

class Base {
protected:
    int data = 5;
};
class Derived : public Base {
public:
    void f() { std::cout << data; } // ok: derived class can access it
};

Polymorphism with virtual functions

A virtual function allows the derived class version to be called through a base pointer/reference:

class Animal {
	public:
	    virtual void sound() { std::cout << "generic"; } // virtual
};
 
class Dog : public Animal {
	public:
	    void sound() override { std::cout << "Woof!"; } // override indicates it replaces
};
 
class Cat : public Animal {
	public:
	    void sound() override { std::cout << "Meow!"; }
};
Animal* ptr = new Dog();
ptr->sound(); // "Woof!" (calls Dog's version)

Warning

Without virtual, ptr->sound() would call Animal’s version even though the actual object is a Dog. virtual is what enables polymorphism.

Abstract classes

A class with a pure virtual function (= 0) is abstract: it cannot be instantiated, it only serves as a base.

class Shape {
	public:
	    virtual double area() const = 0; // pure virtual
};
 
class Circle : public Shape {
	public:
	    double area() const override { return 3.14 * r * r; }
	private:
	    double r = 1;
};

Note

An abstract class is C++‘s way of defining an interface that multiple classes implement differently.

Next: Move Semantics