Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

C++ Inheritance

Inheritance builds a new class on top of an existing one. The new derived class gets all the members of the base class and can add its own. It models an "is a" relationship: a dog is an animal, a savings account is an account.

A base and a derived class

#include <iostream>
#include <string>

class Animal {
protected:                      // visible in derived classes
    std::string name;
public:
    explicit Animal(const std::string& n) : name(n) {}
    void eat() const { std::cout << name << " is eating\n"; }
};

class Dog : public Animal {
public:
    explicit Dog(const std::string& n) : Animal(n) {}   // call the base constructor
    void bark() const { std::cout << name << " says Woof\n"; }
};

int main() {
    Dog d("Bruno");
    d.eat();      // inherited from Animal
    d.bark();     // Dog's own
    return 0;
}
Output
Bruno is eating
Bruno says Woof
  • class Dog : public Animal makes Dog derive from Animal. With public inheritance, the public members of Animal stay public in Dog.
  • The base constructor is called in the derived constructor's initializer list: Animal(n).
  • protected members are hidden from the outside but usable by derived classes; private members of the base are not reachable in Dog at all.

Adding and changing behaviour

A derived class can add members and also define a method with the same name as the base's, which hides the base version for Dog objects. To make such a replacement work through a pointer or reference to the base class, the base method must be virtual, which is the topic of Polymorphism.

Multilevel inheritance

A derived class can itself be a base:

#include <iostream>

class Account {
protected:
    double balance = 0;
public:
    void deposit(double a) { if (a > 0) balance += a; }
    double getBalance() const { return balance; }
};

class SavingsAccount : public Account {
    double rate;
public:
    explicit SavingsAccount(double r) : rate(r) {}
    void addInterest() { balance += balance * rate; }
};

class StudentSavings : public SavingsAccount {
public:
    StudentSavings() : SavingsAccount(0.05) {}
    void deposit(double a) { SavingsAccount::deposit(a + 10); }   // a small bonus on every deposit
};

int main() {
    StudentSavings s;
    s.deposit(1000);
    s.addInterest();
    std::cout << s.getBalance() << "\n";
    return 0;
}
Output
1060.5

SavingsAccount::deposit(...) calls the base version explicitly.

Inheritance or a member?

Use inheritance only for a real "is a" relationship. When a class only uses another (a car has an engine), make it a member instead; this is called composition, and it is usually the simpler and more flexible choice.

Practice exercises

  1. Add a Cat class with a meow method and use both inherited and own methods.
  2. Give Animal an age member and a constructor that sets it; pass it from Dog's constructor.
  3. Design classes for Vehicle, Car and Bike with shared and own members.