C++ Inheritance
Inheritance is one of the key features of Object-oriented programming in C++. It allows us to create a new
class (derived class) from an existing class (base class).
The derived class inherits the features from the base class and can have additional features of its own. For
example,
class Animal {
// eat() function
// sleep() function
};
class Dog : public Animal {
// bark() function
};
Inheritance in C++
• Inheritance is one of the key features of
Object-oriented programming in C++. It allows us to
create a new class (derived class) from an existing
class (base class).
• Inheritance is used when two classes in a program
share the same domain, and the properties of the
class and its superclass should remain the same.
Inheritance is a technique used in C++ to reuse code
from pre-existing classes. C++ actively supports the
concept of reusability.
• The derived class inherits the features from the
base class and can have additional features of its
Example
Continue….
• When we say derived class inherits the base class, it means, the derived
class inherits all the properties of the base class, without changing the
properties of base class and may add new features to its own. These
new features in the derived class will not affect the base class. The
derived class is the specialized class for the base class.
• Sub Class: The class that inherits properties from another class is called
Subclass or Derived Class.
• Super Class: The class whose properties are inherited by a subclass is
called Base Class or Superclass.
Why and when to use inheritance?
Consider a group of vehicles. You need to create classes for Bus, Car,
and Truck. The methods fuelAmount(), capacity(), applyBrakes() will
be the same for all three classes. If we create these classes avoiding
inheritance, then we have to write all of these functions in each of the
three classes as shown below figure:
• You can clearly see that the above process results in duplication of the
same code 3 times. This increases the chances of error and data
redundancy. To avoid this type of situation, inheritance is used. If we
create a class Vehicle and write these three functions in it and inherit the
rest of the classes from the vehicle class, then we can simply avoid the
duplication of data and increase re-usability. Look at the below diagram in
which the three classes are inherited from vehicle class:
Continue..
• Using inheritance, we have to write the functions only one time
instead of three times as we have inherited the rest of the three
classes from the base class (Vehicle).
• Implementing inheritance in C++: For creating a sub-class that is
inherited from the base class we have to follow the below syntax
Types Of Inheritance:-
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
Continue….
1. Single Inheritance: In single inheritance, a class is allowed to inherit
from only one class. i.e. one subclass is inherited by one base class only.
Example
• #include <iostream>
• using namespace std;
• class vehicle{
• public:
• string brand="ford";
• int price=12000;
• void honk(){
• cout<<"toot toot \n";
• }
• };
• class car:public vehicle{
• public:
• string model="mustang";
• };
• int main()
• {
• car mycar;
• mycar.honk();
• cout << mycar.brand + " " + mycar.model<<endl ;
• cout<<mycar.price;
• }
Multilevel Inheritance
Multilevel inheritance in C++ is a type of inheritance where a class is
derived from another class, which is also derived from another class.
This creates a chain of classes, where each class inherits from its
predecessor.
In the following example, MyGrandChild is derived from class MyChild
(which is derived from MyClass).
2.Multiple Inheritance in C++
Multiple inheritance is a feature in C++ that allows a class (derived class)
to inherit characteristics and behaviors (methods and properties) from
more than one base class. This enables the derived class to combine the
functionalities of multiple classes, thus promoting code reuse
Eg:
• (i) A CHILD class is derived from FATHER and MOTHER class
(ii) A PETROL class is derived from LIQUID and FUEL class.
Example
nclude <iostream>
#i
using namespace std;
class grandparent{
public:
void showgrandparent(){
cout<<"i am the grand parent"<<endl;
}
};
class parent:public grandparent{
public:
void showparent(){
cout<<"iam the parent"<<endl;
}
};
class child:public parent{
public:
void showchild(){
cout<<"iam the child"<<endl;
}
};
int main()
{
child child1;
child1.showchild();
child1.showgrandparent();
child1.showparent();
2. C++ Multilevel
Inheritance
• Multilevel Inheritance in C++ is the process of
deriving a class from another derived class. When one
class inherits another class, it is further inherited by
another class. It is known as multi-level inheritance.
• For example, if we take Grandfather as a base class
then Father is the derived class that has features of
Grandfather and then Child is the also derived class that
is derived from the sub-class Father which inherits all
the features of Father.
Real-life analogy of multilevel
inheritance
• When to use multilevel inheritance
• 1.Hierarchy of classes:It is used when you need to
create a complex class structure and when you want to
create a logical hierarchy for your classes.
• 2. Code reuse: Multilevel inheritance is a powerful tool
for rapid development of complex applications. It allows
the programmer to create a complex class structure and
reduces the amount of code needed to create a new
class. However, it can also create complexity and make
it difficult to maintain the codebase. As such, it should
be used with caution.
Advantages of Multilevel
Inheritance
• Advantages of Multilevel Inheritance
1.Multilevel inheritance is a powerful tool in object-oriented programming,
allowing developers to build upon existing classes and add greater levels of
complexity. It enables developers to avoid redundancy and share common
characteristics across classes.
2.It reduces the amount of code that must be written. When objects inherit from
multiple parent classes, developers do not have to write code for each class;
instead, they can use the existing code from the parent classes. This helps to
save time and makes it easier to maintain the code.
3.Finally, multilevel inheritance allows developers to create a hierarchy of classes
where each class is responsible for a specific task. This makes the code easier to
understand and debug, as developers can easily pinpoint the source of any
errors.
4.Multilevel inheritance is an essential tool for object-oriented programming,
allowing developers to create complex and efficient models quickly and easily.
Disadvantages of Multilevel
Inheritance
1.Multilevel inheritance can quickly become complex and
difficult to manage, as it can easily cause a large
amount of code to be written for one class. Also, it can
lead to a less clear program structure since the class
hierarchy can get hard to understand. Additionally, it
may lead to the reuse of code which is only sometimes
desirable, as different classes may require different
implementations of the same methods. Finally, it may
be not easy to debug since the code from multiple
classes may have to be checked.
Conclusion
• Multiple inheritance is a powerful feature of object-
oriented programming that allows a child class to inherit
properties from multiple parent classes. It can be used
to create complex and efficient software applications;
however, it can also lead to complex code and many
potential software bugs. When using multiple
inheritance, it is important to consider its
consequences, such as the complexity of code, the risk
of unexpected behavior, and the potential for conflicts
between parent classes. It is advisable to use multiple
inheritance judiciously, understanding the associated
risks and potential benefits.