Introduction to Inheritance in C++
What is Inheritance?
Inheritance is one of the four fundamental principles of Object-Oriented Programming (OOP). It allows one class (the derived or child class) to inherit attributes and methods from another class (the base or parent class). This promotes code reusability and establishes a natural hierarchy between classes. Inheritance allows the derived class to inherit and override methods of the base class, which can be useful for extending functionality.
Why Use Inheritance?
Inheritance provides several benefits:
- Code Reusability: You can use existing code without rewriting it.
- Establishing a Natural Hierarchy: It creates a relationship between base and derived classes.
- Extending Functionality: Derived classes can add new features or override existing ones.
- Maintenance: Easier to manage and maintain code.
Basic Syntax of Inheritance
In C++, the syntax for inheritance involves the use of a colon (:) followed by the access specifier and the base class name.
class BaseClass { // Base class code }; class DerivedClass : public BaseClass { // Derived class code };
Types of Inheritance
There are different types of inheritance in C++:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Example of Single Inheritance
In single inheritance, a class inherits from one base class. Here's an example:
#include <iostream> using namespace std; class Base { public: void show() { cout << "Base class method" << endl; } }; class Derived : public Base { public: void display() { cout << "Derived class method" << endl; } }; int main() { Derived obj; obj.show(); obj.display(); return 0; }
Base class method Derived class method
Example of Multilevel Inheritance
In multilevel inheritance, a class is derived from another derived class. Here's an example:
#include <iostream> using namespace std; class Base { public: void show() { cout << "Base class method" << endl; } }; class Intermediate : public Base { public: void display() { cout << "Intermediate class method" << endl; } }; class Derived : public Intermediate { public: void print() { cout << "Derived class method" << endl; } }; int main() { Derived obj; obj.show(); obj.display(); obj.print(); return 0; }
Base class method Intermediate class method Derived class method
Access Specifiers in Inheritance
Access specifiers play an important role in inheritance. They define the accessibility of the base class members in the derived class:
- public: Public members of the base class become public members of the derived class.
- protected: Public and protected members of the base class become protected members of the derived class.
- private: Public and protected members of the base class become private members of the derived class.
Conclusion
Inheritance is a powerful feature of C++ that allows for the creation of a new class by reusing the properties and behaviors of an existing class. It not only promotes code reusability but also helps in creating a natural hierarchical relationship between classes. With different types of inheritance, C++ provides a flexible way to extend and enhance the functionality of programs.