cpp-herencia

What is and how to use inheritance in C++

  • 4 min

In C++, inheritance is used to establish a relationship between a base class (or parent class) and a derived class (or child class).

The derived class has all the public and protected characteristics of the base class, plus its own. This allows it to extend or modify the behavior of the base class.

Implementing Inheritance in C++

Inheritance in C++ is defined using the colon syntax (:) followed by the type of inheritance (public, protected, or private) and the name of the base class.

class BaseClass
{
    // Members of the base class
};

class DerivedClass : public BaseClass
{
    // Additional members of the derived class
};
Copied!

Let’s create a simple example where a class Animal is the base class and a class Dog is the derived class:

#include <iostream>

class Animal
{
public:
    void Eat()
    {
        std::cout << "The animal is eating." << std::endl;
    }
};

class Dog : public Animal
{
public:
    void Bark()
    {
        std::cout << "The dog is barking." << std::endl;
    }
};

int main()
{
    Dog myDog;
    myDog.Eat();  // Inherited from Animal
    myDog.Bark(); // Method defined in Dog

    return 0;
}
Copied!

In this example,

  • The Dog class inherits from Animal
  • This means Dog has access to the Eat method from Animal
  • In addition to its own Bark method.

Accessing Parent Class Members

You can access members of the base class directly by using the base class name followed by the scope resolution operator (::).

#include <iostream>

class Animal
{
public:
    std::string Name;

    Animal(const std::string& name) : Name(name) {}

    virtual void MakeSound()
    {
        std::cout << "The animal makes a sound." << std::endl;
    }
};

class Dog : public Animal
{
public:
    Dog(const std::string& name) : Animal(name) {}

    void MakeSound() override
    {
        Animal::MakeSound(); // Calls the base class method
        std::cout << "The dog barks." << std::endl;
    }
};

int main()
{
    Dog myDog("Rex");
    myDog.MakeSound(); // Output: The animal makes a sound. The dog barks.

    return 0;
}
Copied!

Method Overriding

Derived classes can override methods from the base class to provide a specific implementation.

To do this, the method in the base class must be declared with the virtual keyword, and the method in the derived class should use the override keyword.

#include <iostream>

class Animal
{
public:
    virtual void MakeSound()
    {
        std::cout << "The animal makes a sound." << std::endl;
    }
};

class Dog : public Animal
{
public:
    void MakeSound() override
    {
        std::cout << "The dog barks." << std::endl;
    }
};

int main()
{
    Animal* myAnimal = new Dog();
    myAnimal->MakeSound(); // Calls the overridden method in Dog

    delete myAnimal;
    return 0;
}
Copied!

In this example,

  • Dog overrides the MakeSound method from Animal.
  • When we call MakeSound through a pointer to Animal, the overridden version in Dog is executed.

Constructors in Derived Classes

When an instance of a derived class is created, the constructor of the base class is automatically called before the constructor of the derived class.

You can specify which base class constructor should be called using the derived class’s initializer list.

#include <iostream>

class Animal
{
public:
    std::string Name;

    Animal(const std::string& name) : Name(name) {}

    void ShowName()
    {
        std::cout << "Name: " << Name << std::endl;
    }
};

class Dog : public Animal
{
public:
    std::string Breed;

    Dog(const std::string& name, const std::string& breed)
        : Animal(name), Breed(breed) {}

    void ShowInfo()
    {
        ShowName();
        std::cout << "Breed: " << Breed << std::endl;
    }
};

int main()
{
    Dog myDog("Rex", "Labrador");
    myDog.ShowInfo(); // Output: Name: Rex Breed: Labrador

    return 0;
}
Copied!

In this example, the Dog constructor calls the Animal constructor to initialize the Name field.