INHERITANCE
INTRODUCTION
The capability of a class to derive properties and characteristics from another class is
called Inheritance.
Two classes:
❑ Super Class : Also known as the Base class.
❑ Sub Class : Also known as the Derived class.
WHY AND WHEN TO USE INHERITANCE?
Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The
methods will be same for all of the 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 in below figure:
WHY AND WHEN TO USE INHERITANCE?
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.
SYNTAX
For creating a subclass which is inherited from the base class we have to
follow the below syntax.
class subclass_name : access_mode base_class_name
{
//body of subclass
};
EXAMPLE OF INHERITANCE
MODES OF INHERITANCE
► Public mode: If we derive a subclass from a public base class. Then
the public member of the base class will become public in the derived
class and protected members of the base class will become protected
in derived class.
► Protected mode: If we derive a subclass from a Protected base class.
Then both public member and protected members of the base class
will become protected in derived class.
► Private mode: If we derive a subclass from a Private base class.
Then both public member and protected members of the base
class will become Private in derived class. Therefore, the public
members of the base class are not accessible by the objects of the
derived class only by the member functions of the derived class.
THREE MODES OF INHERITANCE
TYPES OF INHERITANCE
SINGLE INHERITANCE
In single inheritance, a class is allowed to inherit from only one class.
SYNTAX:
class subclass_name : access_mode base_class
{
//body of subclass
};
MULTIPLE
INHERITANCE:
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes.
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
//body of subclass
};
MULTILEVEL
INHERITANCE:
In this type of inheritance, a derived class is created from another derived class.
HIERARCHICAL
INHERITANCE
In this type of inheritance, more than one sub class is inherited from a single base class.
HYBRID (VIRTUAL) INHERITANCE
Hybrid Inheritance is implemented by combining more than one type of inheritance..
CONCLUSION
❑ WHY DO WE USE INHERITANCE?
❑ MODES OF INHERITANCE
❑ TYPES OF INHERITANCE
THANK YOU

INHERITANCE

  • 1.
  • 2.
    INTRODUCTION The capability ofa class to derive properties and characteristics from another class is called Inheritance. Two classes: ❑ Super Class : Also known as the Base class. ❑ Sub Class : Also known as the Derived class.
  • 3.
    WHY AND WHENTO USE INHERITANCE? Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods will be same for all of the 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 in below figure:
  • 4.
    WHY AND WHENTO USE INHERITANCE? 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.
  • 5.
    SYNTAX For creating asubclass which is inherited from the base class we have to follow the below syntax. class subclass_name : access_mode base_class_name { //body of subclass };
  • 6.
  • 7.
    MODES OF INHERITANCE ►Public mode: If we derive a subclass from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class. ► Protected mode: If we derive a subclass from a Protected base class. Then both public member and protected members of the base class will become protected in derived class. ► Private mode: If we derive a subclass from a Private base class. Then both public member and protected members of the base class will become Private in derived class. Therefore, the public members of the base class are not accessible by the objects of the derived class only by the member functions of the derived class.
  • 8.
    THREE MODES OFINHERITANCE
  • 9.
  • 10.
    SINGLE INHERITANCE In singleinheritance, a class is allowed to inherit from only one class. SYNTAX: class subclass_name : access_mode base_class { //body of subclass };
  • 11.
    MULTIPLE INHERITANCE: Multiple Inheritance isa feature of C++ where a class can inherit from more than one classes. class subclass_name : access_mode base_class1, access_mode base_class2, .... { //body of subclass };
  • 12.
    MULTILEVEL INHERITANCE: In this typeof inheritance, a derived class is created from another derived class.
  • 13.
    HIERARCHICAL INHERITANCE In this typeof inheritance, more than one sub class is inherited from a single base class.
  • 14.
    HYBRID (VIRTUAL) INHERITANCE HybridInheritance is implemented by combining more than one type of inheritance..
  • 15.
    CONCLUSION ❑ WHY DOWE USE INHERITANCE? ❑ MODES OF INHERITANCE ❑ TYPES OF INHERITANCE
  • 16.