Use of Inheritance in C++
What is inheritance in c++?
Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the properties and behaviors of an existing class (called a base class). This enables the programmer to reuse existing code and avoid duplication, which can save time and improve the reliability of their programs.
In C++, inheritance is implemented using the:
operator, followed by the access specifier (such as public
) and the name of the base class.
Here is an example of how inheritance might be used in C++:
#include <iostream>
// Base class (Animal)
class Animal
{
public:
void move()
{
std::cout << "The animal is moving" << std::endl;
}
};
// Derived class (Dog)
class Dog: public Animal
{
public:
void bark()
{
std::cout << "Woof!" << std::endl;
}
};
int main()
{
// Create a dog object
Dog dog;
// Use the move() method from the Animal class
dog.move();
// Use the bark() method from the Dog class
dog.bark();
return 0;
}
Output:

Explanation:
The above code is used to demonstrate inheritance. In this program, the "Dog" class is derived from the "Animal" class and has access to its move()
method. In the main()
function, a "Dog" object is created, and the move()
and bark()
methods are called.
One of the key benefits of inheritance is code reuse. By creating a hierarchy of classes, a programmer can define common properties and behaviors in a base class and then reuse them in derived classes. This can save time and improve the program's reliability because the programmer does not have to rewrite the same code multiple times.
Here is a simple C++ program that demonstrates how inheritance can be used for code reuse:
#include <iostream>
// Base class (Animal)
class Animal
{
public:
void move()
{
std::cout << "The animal is moving" << std::endl;
}
};
// Derived class (Dog)
class Dog: public Animal
{
public:
void bark()
{
std::cout << "Woof!" << std::endl;
}
};
// Derived class (Cat)
class Cat: public Animal
{
public:
void meow()
{
std::cout << "Meow!" << std::endl;
}
};
int main()
{
// Create a dog object
Dog dog;
// Use the move() method from the Animal class
dog.move();
// Use the bark() method from the Dog class
dog.bark();
// Create a cat object
Cat cat;
// Use the move() method from the Animal class
cat.move();
// Use the meow() method from the Cat class
cat.meow();
return 0;
}
Output:

Explanation:
In this program, the "Animal" class defines a move()
method, and the "Dog" and "Cat" classes are derived from the "Animal" class. This means that the "Dog" and "Cat" classes can use the move()
method without having to provide their implementation.
In the main() function, "Dog" and "Cat" objects are created, and the move() and bark()/meow() methods are called.
This demonstrates how inheritance allows derived classes to reuse the functionality of their base class and provide their unique behavior. It also shows how a base class can provide a common interface for derived classes so that code can be written that can be used with objects of different types without knowing the specific type at compile time.
Another key benefit of inheritance is polymorphism, which allows derived classes to provide their own implementations of base class methods. This is achieved through the use of virtual methods and the override keyword.
Virtual methods are declared using the virtual keyword in the base class and the override keyword in the derived class. This tells the compiler that the derived class method is intended to override the base class method. Here is a simple C++ program that demonstrates the use of virtual methods and the override keyword:
#include <iostream>
// Base class (Shape)
class Shape
{
public:
// Virtual method for calculating the area
virtual double getArea() = 0;
};
// Derived class (Circle)
class Circle: public Shape
{
public:
Circle(double radius)
{
this->radius = radius;
}
double getArea() override
{
return 3.14159 * radius * radius;
}
private:
double radius;
};
int main()
{
// Create a circle object with radius 5
Circle circle(5);
// Use the getArea() method to calculate the circle's area
std::cout << "Area: " << circle.getArea() << std::endl;
return 0;
}
Output:

Explanation:
In this program, the "Shape" class defines a virtual method called getArea() that calculates the area of a shape. The "Circle" class is derived from the "Shape" class and provides its implementation of the getArea() method, which calculates the area of a circle based on its radius.
The getArea() method in the "Shape" class is declared as virtual and the getArea() method in the "Circle" class is declared with the `over