OSC++.017: Inheritance Basics

Black and neon-green OSC++.017 Inheritance Basics cover showing Vehicle as the base class and Car as the derived class

Inheritance lets one C++ class build on another class.

The easiest way to think about it is this:

  • Vehicle is a general class.
  • Car is a more specific kind of Vehicle.
  • So Car can inherit useful parts of Vehicle.

In C++, the general class is usually called the base class. The class that inherits from it is called the derived class.

Your first inheritance example

#include <iostream>
using namespace std;

class Vehicle {
public:
    void start() {
        cout << "Vehicle started\n";
    }
};

class Car : public Vehicle {
};

int main() {
    Car myCar;
    myCar.start();
}

The output is:

Vehicle started

Notice something important: we did not write start() inside Car. Car received access to that public function through inheritance.

Read this line slowly

class Car : public Vehicle

For this beginner lesson, read it as:

Car inherits from Vehicle.

Vehicle is the base class. Car is the derived class.

Video 1: Create a derived class with inheritance

This focused C++ lab shows how inheritance is used to create a derived class from a base class.

The child can add its own feature

class Vehicle {
public:
    void start() {
        cout << "Vehicle started\n";
    }
};

class Car : public Vehicle {
public:
    void honk() {
        cout << "Beep!\n";
    }
};

Now a Car object can use both:

Car myCar;

myCar.start();  // inherited from Vehicle
myCar.honk();   // defined in Car

That is the main benefit: put shared behavior in the general class, then put special behavior in the more specific class.

A simple gaming example

class Player {
public:
    void move() {
        cout << "Player moves\n";
    }
};

class Runner : public Player {
public:
    void sprint() {
        cout << "Runner sprints\n";
    }
};

A Runner can use the normal move() behavior from Player and add sprint() for itself.

Constructors still matter

The previous lesson, OSC++.016: Constructors and Destructors Basics, introduced automatic setup and cleanup.

With inheritance, the base part of an object is set up before the derived part.

class Vehicle {
public:
    Vehicle() {
        cout << "Vehicle constructor\n";
    }
};

class Car : public Vehicle {
public:
    Car() {
        cout << "Car constructor\n";
    }
};

int main() {
    Car myCar;
}

The output is:

Vehicle constructor
Car constructor

Why? A Car contains its Vehicle base part, so C++ sets up that base part first.

Video 2: C++ inheritance and class relationships

This focused C++ lesson reinforces how a derived class builds on a base class. It also introduces protected members as an additional inheritance concept.

A derived class can provide its own version

class Vehicle {
public:
    void sound() {
        cout << "Vehicle sound\n";
    }
};

class Car : public Vehicle {
public:
    void sound() {
        cout << "Beep!\n";
    }
};

int main() {
    Car myCar;
    myCar.sound();
}

For this Car object, the result is Beep!. The derived class has its own function with the same name.

Later lessons can go deeper into virtual functions and true runtime polymorphism. For now, the important idea is simply that a derived class can add behavior and can also define its own version of a function name.

Video 3: Redefining a base-class function

This tutorial shows a derived C++ class defining its own version of a base-class function.

When inheritance makes sense

A good beginner test is the phrase “is a kind of.”

  • A Car is a kind of Vehicle.
  • A Runner is a kind of Player.
  • A Dog is a kind of Animal.

If that sentence sounds wrong, inheritance may not be the right relationship.

Common beginner mistakes

  • Mixing up the base class and derived class.
  • Thinking inheritance copies and pastes the source code.
  • Using inheritance between things that are not actually related.
  • Jumping into advanced inheritance features before the basic relationship is clear.

Quick practice

  1. Create a base class named Animal.
  2. Add a public function named eat().
  3. Create Dog as a public derived class of Animal.
  4. Add bark() to Dog.
  5. Create one Dog object and call both eat() and bark().

Key takeaway

Inheritance lets a more specific class build on a more general class. In class Car : public Vehicle, Vehicle is the base class and Car is the derived class. Car can use accessible behavior from Vehicle and add behavior of its own.

Leave a comment