Inheritance in C++
- In C++, inheritance is a fundamental OOP concept that enables you to create new classes based on existing classes.
- Inheritance promotes code reuse and supports the "is-a" relationship between objects.
- Inheritance involves two types of classes:
- Base Class (Parent Class): This is the class that provides a blueprint or template for creating derived classes.
- Derived Class (Child Class): This is the class that inherits properties and behaviors from the base class. It can also add its own unique attributes and methods to its behavior.
Why and When to Use Inheritance in C++?
- Inheritance helps in organizing code into manageable and understandable modules.
- Inheritance supports polymorphism, which allows objects of derived classes to be treated as objects of their base class.
You should consider using inheritance when:
- For example, if you have multiple classes that share common attributes and behaviors, inheritance can be a suitable choice.
- You want to create a hierarchy of classes, where base classes define common characteristics, and derived classes provide specialization.
Modes of Inheritance in C++
- In C++, there are three modes of inheritance: public, private, and protected.
- Public Inheritance: With public inheritance, the public members of the base class become public members within the derived class
- and protected members become protected members.
- Members in the base class marked as "private" cannot be accessed in the derived class. let's understand with an example
1class Base {
2public:
3 int publicVar;
4 void publicFunction() { /* Code here */ }
5protected:
6 int protectedVar;
7 void protectedFunction() { /* Code here */ }
8private:
9 int privateVar;
10 void privateFunction() { /* Code here */ }
11};
12
13class Derived : public Base {
14 // publicVar and protectedVar are accessible here
15};
- Private Inheritance: With private inheritance, both the public and protected members of the base class transform into private members within the derived class.
1class Derived : private Base {
2 // publicVar and protectedVar are private members here
3};
- Protected Inheritance: In protected inheritance, public members of the base class become protected members in the derived class,
- and protected members of the base class also become protected members in the derived class.
1class Derived : protected Base {
2 // publicVar and protectedVar are protected members here
3};
Types of Inheritance in C++
- Single inheritance
- Multilevel inheritance
- Multiple inheritance
- Hierarchical inheritance
- Hybrid inheritance
C++ Single Inheritance
- Single inheritance is a form of inheritance in which a derived class inherits from just one base class.
- This type of inheritance is straightforward and commonly used to model "is-a" relationships. for example
- C++ Single Inheritance Syntax as follows:
1class DerivedClass : public BaseClass {
2 // Derived class members and methods
3};
C++ Single Inheritance Example
Let's grasp the concept of Single Inheritance with an example:
1#include <iostream>
2using namespace std;
3
4
5// Base class
6class Animal {
7public:
8 void eat() {
9 cout << "Animal is eating." << endl;
10 }
11};
12
13// Derived class inheriting from Animal
14class Dog : public Animal {
15public:
16 void bark() {
17 cout << "Dog is barking." << endl;
18 }
19};
20
21int main() {
22 Dog myDog;
23 myDog.eat(); // Accessing base class method
24 myDog.bark(); // Accessing derived class method
25 return 0;
26}
- In this example, we have a base class Animal with a method eat, and a derived class Dog that inherits from Animal.
- Dog can access and use the eat method from its base class.
C++ Multilevel Inheritance
Multilevel inheritance occurs when a derived class inherits from another derived class, creating a chain of inheritance.
C++ Multilevel Inheritance Syntax :
1class BaseClass {
2 // Base class members and methods
3};
4
5class IntermediateClass : public BaseClass {
6 // Intermediate class members and methods
7};
8
9
10class DerivedClass : public IntermediateClass {
11 // Derived class members and methods
12};
C++ Multilevel Inheritance Example
let's understand with an example :
1#include <iostream>
2using namespace std;
3
4
5// Base class
6class Animal {
7public:
8 void eat() {
9 cout << "Animal is eating." << endl;
10 }
11};
12
13// Intermediate class inheriting from Animal
14class Dog : public Animal {
15public:
16 void bark() {
17 cout << "Dog is barking." << endl;
18 }
19};
20
21// Derived class inheriting from Dog
22class GoldenRetriever : public Dog {
23public:
24 void fetch() {
25 cout << "Golden Retriever is fetching." << endl;
26 }
27};
28
29int main() {
30 GoldenRetriever myDog;
31 myDog.eat(); // Accessing base class method
32 myDog.bark(); // Accessing intermediate class method
33 myDog.fetch(); // Accessing derived class method
34 return 0;
35}
- In this example, we have a base class Animal, an intermediate class Dog that inherits from Animal, and a derived class GoldenRetriever that inherits from Dog.
- This creates a chain of inheritance where each class can access the methods of its parent class.
C++ Multiple Inheritance
- Multiple inheritance happens when a derived class inherits from two or more base classes where ambiguity may arise in the case of shared features.
- Example: A FlyingCar class inheriting from both Car and Aircraft classes, combining features of both types of vehicles.
- C++ multiple inheritance Syntax is as follows:
1class BaseClass1 {
2 // Base class 1 members and methods
3};
4
5class BaseClass2 {
6 // Base class 2 members and methods
7};
8
9
10class DerivedClass : public BaseClass1, public BaseClass2 {
11 // Derived class members and methods
12};
C++ Multiple Inheritance Example
1#include <iostream>
2using namespace std;
3
4// First base class
5class Animal {
6public:
7 void eat() {
8 cout << "Animal is eating." << endl;
9 }
10};
11
12// Second base class
13class Machine {
14public:
15 void start() {
16 cout << "Machine is starting." << endl;
17 }
18};
19
20// Derived class inheriting from both Animal and Machine
21class Robot : public Animal, public Machine {
22public:
23 void work() {
24 cout << "Robot is working." << endl;
25 }
26};
27
28
29int main() {
30 Robot myRobot;
31 myRobot.eat(); // Accessing method from first base class (Animal)
32 myRobot.start(); // Accessing method from second base class (Machine)
33 myRobot.work(); // Accessing method from derived class (Robot)
34 return 0;
35}
- We have two base classes, Animal and Machine, each with their respective methods.
- The Robot class is derived from both Animal and Machine using multiple inheritance.
Ambiguity resolution in C++ Inheritance
- In C++, ambiguity resolution refers to the process of resolving conflicts that arise when there is ambiguity in the usage of functions or variables due to multiple inheritance or overloaded functions.
- Ambiguity can occur when the compiler encounters a situation where it's unsure which function or variable to use, and it needs to determine the correct choice.
C++ Ambiguity resolution Syntax:
This is typically done using the scope resolution operator::
1oject_or_class_name::member_name
Resolving Ambiguity in Multiple Inheritance
1class Base1 {
2public:
3 void display() {
4 cout << "Display from Base1" << endl;
5 }
6};
7
8
9class Base2 {
10public:
11 void display() {
12 cout << "Display from Base2" << endl;
13 }
14};
15
16class Derived : public Base1, public Base2 {
17};
18
19int main() {
20 Derived d;
21 d.Base1::display(); // Resolving ambiguity by specifying Base1's display
22 d.Base2::display(); // Resolving ambiguity by specifying Base2's display
23 return 0;
24}
In this example, we resolve the ambiguity between Base1 and Base2's display functions by using the scope resolution operator to specify which one to call.
C++ Hierarchical Inheritance
- Hierarchical inheritance is a type of inheritance where several derived classes inherit from a common base class.
- Each derived class forms its own unique branch of the inheritance tree, sharing common characteristics from the base class.
- Example: A Bird class, Fish class, and Mammal class all inherit from an Animal class.
- Syntax of Hierarchical Inheritance as follows:
1class BaseClass {
2 // Base class members and methods
3};
4
5class DerivedClass1 : public BaseClass {
6 // Derived class 1 members and methods
7};
8
9class DerivedClass2 : public BaseClass {
10 // Derived class 2 members and methods
11
12};
13
C++ Hierarchical Inheritance Example
1#include <iostream>
2using namespace std;
3
4// Base class
5class Shape {
6public:
7 void draw() {
8 cout << "Shape is being drawn." << endl;
9 }
10};
11
12// Derived class 1
13class Circle : public Shape {
14public:
15 void drawCircle() {
16 cout << "Circle is being drawn." << endl;
17 }
18};
19
20// Derived class 2
21class Rectangle : public Shape {
22public:
23 void drawRectangle() {
24 cout << "Rectangle is being drawn." << endl;
25 }
26};
27
28
29int main() {
30 Circle myCircle;
31 Rectangle myRectangle;
32
33 myCircle.draw(); // Accessing method from base class
34 myCircle.drawCircle(); // Accessing method from derived class 1
35
36 myRectangle.draw(); // Accessing method from base class
37 myRectangle.drawRectangle(); // Accessing method from derived class 2
38
39 return 0;
40}
- In this example, we have a base class Shape, and two derived classes, Circle and Rectangle, both inheriting from Shape.
- This represents hierarchical inheritance, where multiple classes inherit from a single base class.
C++ Hybrid Inheritance
- Hybrid inheritance is a combination of two or more types of inheritance within a program.
- It often includes a mix of single, multiple, multilevel, or hierarchical inheritance.
- syntax of hybrid Inheritance as follows:
1.class BaseClass {
2 // Base class members and methods
3};
4
5
6class IntermediateClass1 : public BaseClass {
7 // Intermediate class 1 members and methods
8};
9
10class IntermediateClass2 {
11 // Intermediate class 2 members and methods
12};
13
14class DerivedClass : public IntermediateClass1, public IntermediateClass2 {
15 // Derived class members and methods
16};
C++ Hybrid Inheritance Example
1#include <iostream>
2using namespace std;
3
4// Base class 1
5class Animal {
6public:
7 void eat() {
8 cout << "Animal is eating." << endl;
9 }
10};
11
12// Base class 2
13class Machine {
14public:
15 void start() {
16 cout << "Machine is starting." << endl;
17 }
18};
19
20// Intermediate class inheriting from Animal
21class RobotDog : public Animal {
22public:
23 void bark() {
24 cout << "RobotDog is barking." << endl;
25 }
26};
27
28// Derived class inheriting from RobotDog and Machine
29class RoboCop : public RobotDog, public Machine {
30public:
31 void work() {
32 cout << "RoboCop is working." << endl;
33 }
34};
35
36int main() {
37 RoboCop myRoboCop;
38 myRoboCop.eat(); // Accessing method from Animal (base class 1)
39 myRoboCop.start(); // Accessing method from Machine (base class 2)
40 myRoboCop.bark(); // Accessing method from RobotDog (intermediate class)
41 myRoboCop.work(); // Accessing method from RoboCop (derived class)
42
43 return 0;
44}
In this example, we have a combination of multiple inheritance types. RoboCop inherits from both RobotDog and Machine, demonstrating hybrid inheritance.