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, in addition to its own. This allows it to extend or modify the behavior of the base class.
Implementation of 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
{
// Base class members
};
class DerivedClass : public BaseClass
{
// Additional members of the derived class
};
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;
}
In this example,
- The class
Dog
inherits fromAnimal
- This means that
Dog
has access to theEat
method ofAnimal
- In addition to its own
Bark
method.
Accessing base class members
You can access the members of the base class directly using the name of the base class 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;
}
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 keyword virtual
, and the method in the derived class must use the keyword override
.
#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;
}
In this example,
Dog
overrides theMakeSound
method ofAnimal
.- When we call
MakeSound
through a pointer toAnimal
, the overridden version inDog
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 constructor of the base class should be called using the initializer list of the derived class.
#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;
}
In this example, the constructor of Dog
calls the constructor of Animal
to initialize the Name
field.