Inheritance is a fundamental concept in object-oriented programming (OOP) that allows classes to inherit properties and behaviors from other classes. In C++, inheritance enables the creation of new classes based on existing classes, promoting code reusability and providing a hierarchical structure to organize and manage related classes.
There are different types of inheritance in C++, including single inheritance, multiple inheritance, and multilevel inheritance. In this article, we will focus on single inheritance, which is the most commonly used type.
Single Inheritance:
Single inheritance occurs when a derived class inherits properties and behaviors from a single base class. The derived class, also known as the child class, extends the functionality of the base class, also known as the parent class. Let’s understand this concept with an example:
“`cpp
#include
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << “The animal is eating.” << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << “The dog is barking.” << endl;
}
};
int main() {
// Create an object of the derived class
Dog dog;
// Accessing the inherited method from the base class
dog.eat();
// Accessing the method specific to the derived class
dog.bark();
return 0;
}
“`
In the example above, we have a base class called “Animal” and a derived class called “Dog.” The derived class inherits the “eat()” method from the base class. Additionally, the derived class introduces a new method called “bark()”.
By creating an object of the derived class, we can access both the inherited method “eat()” and the method specific to the derived class “bark()”. This demonstrates how inheritance allows us to reuse code from the base class while adding new functionality in the derived class.
Inheritance also enables polymorphism, which means that objects of the derived class can be treated as objects of the base class. This allows for more flexibility and modularity in code design.
“`cpp
// Base class
class Shape {
public:
virtual void draw() {
cout << “Drawing a shape.” << endl;
}
};
// Derived class
class Circle : public Shape {
public:
void draw() {
cout << “Drawing a circle.” << endl;
}
};
// Function that takes a Shape object as a parameter
void drawShape(Shape* shape) {
shape->draw();
}
int main() {
// Create objects of the derived class
Shape shape;
Circle circle;
// Pass the derived class objects to the function
drawShape(&shape); // Output: Drawing a shape.
drawShape(&circle); // Output: Drawing a circle.
return 0;
}
“`
In this example, we have a base class called “Shape” and a derived class called “Circle.” The derived class overrides the “draw()” method inherited from the base class to provide its own implementation.
The function “drawShape()” takes a pointer to a Shape object as a parameter. By passing objects of both the base class and the derived class to this function, we can see how the appropriate implementation of the “draw()” method is called based on the actual type of the object.
In conclusion, C++ inheritance is a powerful mechanism that allows classes to inherit properties and behaviors from other classes. It promotes code reusability, modularity, and flexibility in object-oriented programming. By understanding and utilizing inheritance effectively, you can write cleaner and more maintainable code in C++.