Inheritance in C++ Classes

Introduction

Hello again! In this part of our C++ Class Basics Revision, we delve into inheritance in object-oriented programming (OOP) with C++. Inheritance allows us to share code across classes, thus improving readability and efficiency.

In this lesson, we'll clarify attribute and method inheritance in C++ using practical examples. Our lesson's blueprint includes defining inheritance, examining attribute inheritance, exploring method inheritance, and decoding the base class constructor's usage in C++. Ready? Let's get started!

Defining Inheritance

Inheritance involves creating a derived class that inherits details from a base class. In C++, we often find scenarios where classes share common attributes or methods, which makes inheritance highly useful.

Here's an example featuring a base class named Vehicle and a derived class named Car:

#include <iostream>
#include <string>

// Define the base class 'Vehicle'
class Vehicle {
public:
    // Constructor to initialize 'color' and 'brand' attributes
    Vehicle(std::string color, std::string brand) : color(color), brand(brand) {}

protected:
    std::string color; // color of the vehicle
    std::string brand; // brand of the vehicle
};

// Define the derived class 'Car'
class Car : public Vehicle { // Car inherits from Vehicle
public:
    // Constructor to initialize 'color', 'brand', and 'doors'
    Car(std::string color, std::string brand, int doors) : Vehicle(color, brand), doors(doors) {}

private:
    int doors; // number of doors specific to 'Car'
};

Inheritance types, such as Single, Multiple, Multilevel, and Hierarchical, in C++, cater to different needs. However, our focus in this lesson is primarily on single inheritance, where one base class feeds one derived class.

Attribute Inheritance

Attribute inheritance allows a derived class to inherit the attributes of a base class.

Consider this example featuring a base class named Artist, and a derived class named Musician:

#include <iostream>
#include <string>

// Define the base class 'Artist'
class Artist {
public:
    // Constructor to initialize 'name' attribute
    Artist(std::string name) : name(name) {}

protected:
    std::string name; // name of the artist
};

// Define the derived class 'Musician'
class Musician : public Artist { // Musician inherits from Artist
public:
    // Constructor to initialize 'name' and 'instrument' attributes
    Musician(std::string name, std::string instrument) : Artist(name), instrument(instrument) {}

    // Method to display the musician's details
    void display() {
        std::cout << "Name: " << name << "\nInstrument: " << instrument << std::endl;
    }

private:
    std::string instrument; // instrument played by the musician
};

int main() {
    // Create an instance of 'Musician' and display its details
    Musician john("John Lennon", "Guitar");
    john.display();
    return 0;
}

The Musician class inherits the name attribute from the Artist class and also has its own unique attribute, instrument.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal