C++ Inheritance

C++ inheritance lets one class reuse and extend another class. The existing class is called the base class, and the new class is called the derived class.

Inheritance is useful when one type is a more specific version of another type. For example, a SavingsAccount can be treated as a specialized kind of Account.

Basic Syntax

To inherit from a class, write the derived class name, a colon, an access label such as public, and the base class name. The general form is class Derived : public Base.

In beginner code, public inheritance is the most common form. It keeps the public members of the base class public in the derived class, which means outside code can still call those inherited public methods.

Example

This program defines a general Account class, then creates a SavingsAccount class that inherits from it. The derived class reuses deposit() and printBalance(), then adds its own addInterest() method.

#include <iostream>
#include <string>

class Account {
private:
    std::string owner;
    double balance;

public:
    Account(std::string accountOwner, double startingBalance) {
        owner = accountOwner;
        balance = startingBalance;
    }

    void deposit(double amount) {
        if (amount > 0.0) {
            balance += amount;
        }
    }

    double getBalance() const {
        return balance;
    }

    void printBalance() const {
        std::cout << owner << " has $" << balance << std::endl;
    }
};

class SavingsAccount : public Account {
private:
    double interestRate;

public:
    SavingsAccount(std::string accountOwner, double startingBalance, double rate)
        : Account(accountOwner, startingBalance) {
        interestRate = rate;
    }

    void addInterest() {
        double interest = getBalance() * interestRate;
        deposit(interest);
    }
};

int main(void) {
    SavingsAccount savings("Mina", 1000.0, 0.05);

    savings.printBalance();
    savings.deposit(200.0);
    savings.addInterest();
    savings.printBalance();

    return 0;
}

Output:

Mina has $1000
Mina has $1260

How The Example Works

Account stores the common data and behavior: an owner name, a balance, a deposit() method, a getBalance() method, and a printBalance() method.

SavingsAccount inherits from Account by writing : public Account. Because of that, a SavingsAccount object can call the public methods inherited from Account, such as deposit() and printBalance().

The constructor for SavingsAccount must also construct the Account part of the object. The line : Account(accountOwner, startingBalance) calls the base class constructor before the body of the derived class constructor runs.

Private Members Stay Private

Inheritance does not mean the derived class can directly use every member inside the base class. In the example, owner and balance are private in Account, so SavingsAccount cannot directly write balance += interest;.

Instead, the derived class uses the public interface of the base class: it calls getBalance() to read the balance and deposit() to change it. This keeps the encapsulation rules from the previous lesson intact.

When To Use Inheritance

Use inheritance when the derived class really is a kind of the base class. A SavingsAccount is an Account, so inheritance makes sense.

Do not use inheritance just to share a few lines of code. If one object merely has another object as a part, storing it as a member variable is often clearer than inheriting from it.

Common Mistakes

  • Forgetting public in class Derived : public Base, which changes how inherited members are exposed.
  • Trying to access private base class members directly from the derived class.
  • Using inheritance for a has-a relationship instead of an is-a relationship.
  • Forgetting to call a base class constructor when the base class needs arguments.

Takeaway: inheritance lets a C++ class build on another class while still respecting the base class’s public interface and private data.