Inheritance is a fundamental concept in object-oriented programming (OOP) that allows classes to inherit properties and behaviors from other classes. In C++, inheritance is a powerful mechanism that facilitates code reuse and promotes modularity, allowing developers to write more efficient and scalable programs. In this article, we will explore what inheritance is, how it works in C++, and how to use it effectively.
What is Inheritance in C++?
Inheritance is a feature of OOP that allows a new class, called the derived class, to inherit properties and methods from an existing class, called the base class. In other words, the derived class is a specialized version of the base class that inherits all its attributes and functionality. The base class is also known as the parent class, while the derived class is the child class.
How does Inheritance Work in C++?
In C++, inheritance is achieved by using the colon (:) operator followed by the name of the base class in the class definition of the derived class. For example:
class BaseClass {
public:
int x;
void display() {
cout << "This is the base class" << endl;
}
};
class DerivedClass : public BaseClass {
public:
void show() {
cout << "This is the derived class" << endl;
}
};
In this example, the DerivedClass
is derived from the BaseClass
using the public
access specifier. This means that all public members of the BaseClass
are inherited by the DerivedClass
. The DerivedClass
also has its own member function called show()
.
Now, if we create an object of the DerivedClass
and call its member functions, it will have access to both the inherited member variables and functions, as well as its own member functions:
DerivedClass obj;
obj.x = 5; // access the public member variable of the base class
obj.display(); // call the member function of the base class
obj.show(); // call the member function of the derived class
Output:
This is the base class
This is the derived class
Inheritance also allows us to override base class functions in the derived class. For example, if we want to modify the behavior of the display()
function in the DerivedClass
, we can simply redefine it in the derived class:
class DerivedClass : public BaseClass {
public:
void display() {
cout << "This is the derived class" << endl;
}
void show() {
cout << "This is the derived class" << endl;
}
};
Now, when we call the display()
function on an object of the DerivedClass
, it will print the message from the derived class, not the base class.
Benefits of Inheritance in C++
Inheritance has several benefits in C++:
- Code reuse: Inheritance allows developers to reuse existing code, saving time and effort in the development process.
- Modularity: By separating functionality into separate classes, it makes the code more modular and easier to maintain.
- Polymorphism: Inheritance enables polymorphism, which allows objects to take on multiple forms and behave differently depending on the context.
- Scalability: Inheritance promotes scalability, allowing developers to easily add new features and functionality to existing code.
Follow us on Twitter: Hacktube5