Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP) and is supported in C++. It allows you to create new classes based on existing classes, inheriting their attributes and behaviors. This makes it easier to create and manage complex class hierarchies and promote code reuse.
The base class is referred to as the parent class, while the derived class is referred to as the child class. The child class inherits all the properties and methods of the parent class and can also add new properties and methods of its own. This means that you can reuse the code in the parent class and extend it or override it as needed.
Key Concepts of Inheritance
- Base Class (Parent Class) : The class whose properties and behaviors are inherited by another class is called the base class. It provides a common interface and serves as a blueprint for derived classes.
class Animal { public: void eat() { cout << "Animal is eating." << endl; } };
- Derived Class (Child Class) : The class that inherits properties and behaviors from another class is called the derived class. It extends or specializes the functionality provided by the base class.
class Dog : public Animal { public: void bark() { cout << "Dog is barking." << endl; } };
- Access Specifiers : In C++, the access specifiers (public, private, protected) determine the visibility of the base class members in the derived class. The most common specifier is public, which means public members of the base class remain public in the derived class.
class Base { public: int publicMember; private: int privateMember; protected: int protectedMember; }; class Derived : public Base { // publicMember remains public // privateMember is not accessible // protectedMember remains protected };
Syntax and Declaration of Inheritance in C++
- Syntax of Inheritance : In C++, the class keyword is used to declare a class, and the public keyword is used to specify the access specifier for inheritance.
class DerivedClass : accessSpecifier BaseClass { // Class members and functions };
The accessSpecifier can be public, private, or protected. The choice of access specifier determines the visibility of the base class members in the derived class. - Declaration of Inheritance : When declaring a derived class, the base class name is mentioned after a colon (:), followed by the access specifier.
class Animal { public: void eat() { cout << "Animal is eating." << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog is barking." << endl; } };
In this example, Dog is a derived class that publicly inherits from the Animal base class.
For example, you can create a Person class that contains basic information about a person, such as their name and date of birth. Then, you can create a Student class that inherits from the Person class and adds additional information, such as the student's ID number and list of courses. The Student class can then reuse the properties and methods of the Person class, saving you time and effort, and making it easier to manage the data for each student.
Here's an example to demonstrate inheritance in C++.
#include <iostream> using namespace std; // Base class class Shape { public: void setWidth(int width) { this->width = width; } void setHeight(int height) { this->height = height; } protected: int width; int height; }; // Derived class class Rectangle: public Shape { public: int getArea() { return (width * height); } }; int main(void) { Rectangle rectangle; rectangle.setWidth(5); rectangle.setHeight(7); cout << "Total area: " << rectangle.getArea() << endl; return 0; }Output
Total area: 35
In this example, the Shape class is the base class and the Rectangle class is the derived class. The Rectangle class inherits the setWidth and setHeight methods from the Shape class and adds a new method getArea to calculate the area of a rectangle.
Is-A Relationship Between Two Classes
In C++, inheritance creates an "is-a" relationship between two classes. This means that a derived class is a type of its base class, and can be used interchangeably with the base class in certain situations. For example, if you have a Rectangle class that is derived from a Shape class, you can say that a Rectangle is a Shape, and use a Rectangle object wherever a Shape object is expected.
This relationship is established through inheritance, which allows the derived class to inherit the properties and methods of the base class. This makes it easier to reuse code and create more flexible and manageable class hierarchies. The derived class can also override or extend the methods and properties of the base class, allowing it to provide a more specialized behavior.
- Car is a Vehicle.
- Dog is a Mammal.
- Earth is a Planet.
Here is an example C++ program that demonstrates the "is-a" relationship in inheritance with a Planet class as the base class and an Earth class as the derived class.
#include <iostream> #include <string> using namespace std; class Planet { public: Planet(string name) { this->name = name; } void rotate() { cout << name << " is rotating." << endl; } string name; }; class Earth : public Planet { public: Earth() : Planet("Earth") { } void supportLife() { cout << name << " supports life." << endl; } }; int main() { Planet planet("Planet"); planet.rotate(); Earth earth; earth.rotate(); earth.supportLife(); // Demonstrating the "is-a" relationship in inheritance Planet *pPlanet = &earth; pPlanet->rotate(); return 0; }Output
Planet is rotating. Earth is rotating. Earth supports life. Earth is rotating.
Multiple Inheritance
C++ supports multiple inheritance, multilevel inheritance and hierarchical inheritance, which are the different forms of multiple inheritance in C++ programming language.
- Multiple Inheritance: In multiple inheritance, a class is derived from more than one base class. This allows a class to inherit the properties and methods of multiple classes.
- Multilevel Inheritance: In multilevel inheritance, a class is derived from a class that is derived from another class. It allows you to create a hierarchy of classes where each class is a subclass of another class.
- Hierarchical Inheritance: In hierarchical inheritance, a class is derived from multiple classes, but each class is not derived from the same class. Instead, each class is derived from a different base class.
Multiple Inheritance
Multiple inheritance is a feature of object-oriented programming language in which an object or class can inherit characteristics and features from more than one parent class. In other words, it is a situation where a single class is derived from more than one base class. The class which is derived from multiple base classes is known as the derived class or the sub class.
Example:
Consider the following class structures to demonstrate multiple inheritance.
#include <iostream> using namespace std; class Base1 { public: int publicData1; }; class Base2 { public: int publicData2; }; class Derived: public Base1, public Base2 { }; int main() { Derived d; d.publicData1 = 10; d.publicData2 = 20; cout << "Public Data1: " << d.publicData1 << endl; cout << "Public Data2: " << d.publicData2 << endl; return 0; }Output
Public Data1: 10 Public Data2: 20
Multilevel Inheritance
In multilevel inheritance, a derived class becomes the base class for another derived class. This allows for a hierarchical inheritance structure. The syntax is similar to single and multiple inheritance.
Here is an example of multilevel inheritance in C++:
#include <iostream> using namespace std; class Base { public: int baseVariable; }; class Derived1 : public Base { public: int derived1Variable; }; class Derived2 : public Derived1 { public: int derived2Variable; }; int main() { Derived2 derived2Object; derived2Object.baseVariable = 10; derived2Object.derived1Variable = 20; derived2Object.derived2Variable = 30; cout << "Base Variable: " << derived2Object.baseVariable << endl; cout << "Derived1 Variable: " << derived2Object.derived1Variable << endl; cout << "Derived2 Variable: " << derived2Object.derived2Variable << endl; return 0; }Output
Base Variable: 10 Derived1 Variable: 20 Derived2 Variable: 30
Hierarchical Inheritance
In hierarchical inheritance, multiple derived classes inherit from a single base class. This allows for a clean and organized inheritance structure. The syntax is similar to single and multiple inheritance.
#include <iostream> using namespace std; class Base { public: int baseValue; Base() {} Base(int baseValue) { this->baseValue = baseValue; } }; class Derived1: public Base { public: int derived1Value; Derived1() {} Derived1(int baseValue, int derived1Value): Base(baseValue) { this->derived1Value = derived1Value; } }; class Derived2: public Base { public: int derived2Value; Derived2() {} Derived2(int baseValue, int derived2Value): Base(baseValue) { this->derived2Value = derived2Value; } }; int main() { Derived1 d1(10, 20); Derived2 d2(30, 40); cout << "Derived1's baseValue: " << d1.baseValue << endl; cout << "Derived1's derived1Value: " << d1.derived1Value << endl; cout << "Derived2's baseValue: " << d2.baseValue << endl; cout << "Derived2's derived2Value: " << d2.derived2Value << endl; return 0; }Output
Derived1's baseValue: 10 Derived1's derived1Value: 20 Derived2's baseValue: 30 Derived2's derived2Value: 40
In this example, the class Base is the base class, Derived1 and Derived2 are the derived classes and both the derived classes inherit the baseValue from the base class Base.