Understanding Inheritance in C++

Inheritance is when one class inherits the behaviors of a parent class. That is, a class can inherit the data members and methods of a parent class.

For example, the snippet below shows a class of type Dog with two methods: bark and run.

class Dog{

private:

public:
    Dog();    //1. Constructor
    ~Dog();  //2. Desctuctor

    void bark(); //3. Bark method
    void run(); //4. Run method

};

Dog::Dog(){

    std::cout<<"Dog creat"<<std::endl;
}

Dog::~Dog(){}

void Dog::bark(){

    std::cout<<"I bark"<<std::endl; //5. Print "I bark"
}

void Dog::run(){

    std::cout<<"I run"<<std::endl; //6. Print "I run"

}

These methods have a particular behavior. The bark method prints "I bark" (line 5). The run method prints "I run" (line 6).

Just like a child inherits the traits of his father, a child class can inherit the traits of a parent class. In C++, a subclass refers to a child class. A super class refers to a parent class.

Inheritance in C++ requires the following syntax:

class subclass_type::public superclass_type{};

For instance, the snippet below declares the Beagle class a subclass of the Dog class:

class Beagle:public Dog{

private:

public:
    Beagle(); //1. Constructor
    ~Beagle(); //2. Destructor

};

Beagle::Beagle(){}

Beagle::~Beagle(){}

Notice a crucial point. The Beagle class does not need to declare nor define the bark and run methods. However, it can use them.

For example, the snippet below creates an instance of a Beagle class and access the bark and run methods. See lines 2 & 3.

int main(){

    Beagle beagle; //1. Create an instance of the class

    beagle.bark(); //2. Access the bark method
    beagle.run(); //3. Access the run method

    return 0;
}

When you execute the snippet above, the program will print "I bark" and "I run."

Overwriting a Method

Not every dog barks nor runs the same. These behaviors among dogs are different. This fact implies that we need to modify the behaviors of the Beagle class. C++ allows a subclass to change its behaviors by overwriting a method.

To overwrite a method, you need to declare and define the method you want to modify. In the example below, the bark and run methods have been modified to print: "I bark like a Beagle" and "I run like a Beagle." See lines 5 and 6.

class Beagle:public Dog{

private:

public:
    Beagle(); //1. Constructor
    ~Beagle(); //2. Destructor

    void bark(); //3. Overwrite the bark method

    void run(); //4. Overwrite the run method

};

Beagle::Beagle(){

    std::cout<<"Beagle creat"<<std::endl;

}

Beagle::~Beagle(){}

void Beagle::bark(){

    std::cout<<"I bark like a beagle"<<std::endl; //5. Print "I bark like a beagle"

}

void Beagle::run(){

    std::cout<<"I run like a beagle"<<std::endl; //6. Print "I run like a beagle"

}

When you execute the snippet below, the bark and run methods will print: "I bark like a beagle" and "I run like a beagle."

int main(){

    Beagle beagle; //1. Create an instance of the class

    beagle.bark(); //2. Access the bark method- It prints "I bark like a beagle"
    beagle.run(); //3. Access the run method- It print "I run like a beagle"

    return 0;
}

Before I conclude this lesson, I want to state a crucial point of inheritance in C++. When you create an instance of a class, C++ calls the class constructor. However, when you create an instance of a subclass, C++ calls the superclass constructor and the subclass constructor. C++ calls the superclass constructor before calling the subclass constructor.

Hope this helps

Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.