INHERITANCE
Muhammad Farooq
MAIN TOPICS
 What is Inheritance?
 Advantages
 Types of Inheritance/Access modifiers
 Categories of Inheritance
 Function over-riding
INHERITANCE
 Inheritance is the Key feature of OOP
 The process of creating new classes from the existing classes is called
 inheritance
 Existing classes: are called
 ⚫   Parent or
 ⚫   Base or
 ⚫   Super classes
 New classes: are called
 ⚫   Child or
 ⚫   Derived or
 ⚫   Sub classes
  Need: Reusability
 The derived class inherits some of the features from the base class
 and can have additional features of its own.
private
             Base Class
protected
 public
              Derived     Class
 protected
   public
             private
Base Class   protected
              public
                private
               protected   Derived class members
 Derived        public
  Class
              protected    Derived from Base
                           class
                public
 •Student
  •Rollo
  •Name
  •subjects    Base Class
•Student
 •Discipline
 •Rollo        Derived
 •Name
 •subjects     Class
 •Animal
 •Four legs      Base Class
 •Hair on body
                 Derived
•Vertebrates
•Have Backbone   Class
   SYNTAX
class Subclass_name : access_mode Superclass_name
Example
Class A : public class B
Output
Main function
Derived function
5
                             EXAMPLE
class Animal
{
   public:
   int legs = 4;
};
// Dog class inheriting Animal class
class Dog : public Animal
{
   public:
   int tail = 1;
};
int main()
{
  Dog d;
  cout << d.legs;
  cout << d.tail;
ACCESS MODIFIERS
VISIBILITY OF CLASS MEMBERS
  Depending on Access modifier used while inheritance,
  the availability of class members of Super class in the
  sub class changes.
  It can either be
  ⚫   private
  ⚫   protected
  ⚫   public
1) PUBLIC INHERITANCE
 This is the most used inheritance mode. In this
 the protected member of super class becomes
 protected members of sub class and public
 becomes public.
 class Subclass : public Superclass
2) PRIVATE INHERITANCE
 In private mode, the protected and public
 members of super class become private members
 of derived class.
 class Subclass : Superclass // By default its
 private inheritance
3) PROTECTED INHERITANCE
 In protected mode, the public and protected
 members of Super class becomes protected
 members of Sub class.
 class subclass : protected Superclass
TABLE SHOWING ALL THE VISIBILITY MODES
                Private mode
Base class                       Derived class
                                                 Used by
Protecte                                         functions
   d                              Private
   +
 Public
                Protected mode
                                     Derived class
   Base class
                                                       Used by
                                                       functions
    Protecte
                                        Protecte
       d
                                           d
       +
     Public
                Public mode
Base class                      Derived class
                                                Used by
Protecte                          Protecte      functions
   d                                 d
 Public
                                                 Used by
                              Derived class
   Base class                                    object
  Public                           Public
SUMMARY
 Private members of base class can not be used by
 derived class
 Protected members of base class can be used by
 derived class
 Public members of base class can be used by
 derived class using object
C++ FUNCTION OVERRIDING
 If both base class and derived class have a
 member function with same name and
 arguments (number and type of arguments).
 If we create an object of the derived class and call
 the member function which exists in both classes
 (base and derived)
  the member function of the derived class is
 invoked and the function of the base class is
 ignored.
 The Base class function is overridden by derived
 class function
 This feature in C++ is known as function
 overriding.
HOW TO ACCESS THE OVERRIDDEN FUNCTION IN
THE BASE CLASS FROM THE DERIVED CLASS?
 To access the overridden function of the base
 class from the derived class/main function, scope
 resolution operator :: is used.
 int main()
 {
   B obj;
   obj.A::show();
   obj.show();
 }
     DIFFERENCE BETWEEN
FUNCTION OVERLOADING & FUNCTION OVERRIDING
    S.No          Overloading                     overriding
1          No need Inheritance         Need Inheritance
2          Different return type and   Same return type and list of
           list of parameters          parameters
3          In same scope               In different scope
4          Example of compile-time     Example of run-time
           polymorphism                polymorphism
FUNCTION OVERLOADING VS FUNCTION OVERRIDING
 Inheritance: Overriding of functions occurs when one class is
 inherited from another class. Overloading can occur without
 inheritance.
 Function Signature: Overloaded functions must differ in
 function signature ie either number of parameters or type of
 parameters should differ. In overriding, function signatures
 must be same.
 Scope of functions: Overridden functions are in different
 scopes; whereas overloaded functions are in same scope.
 Behavior of functions: Overriding is needed when derived
 class function has to do some added or different job than the
 base class function. Overloading is used to have same name
 functions which behave differently depending upon parameters
 passed to them.
CATEGORIES OF INHERITANCE
     There are 5 different types of inheritance
1.     Single Inheritance
2.     Multiple Inheritance
3.     Multilevel Inheritance
4.     Hierarchical Inheritance
5.     Hybrid (Virtual) Inheritance
SINGLE INHERITANCE
 A derived class with only one base class is
 called single inheritance.
SINGLE INHERITANCE
 Single inheritance represents a form of
 inheritance when there is only one base class and
 one derived class.
  For example, a class describes a Person:
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
MULTIPLE INHERITANCE.
 A derived class with multiple base class is
 called multiple inheritance.
MULTIPLE INHERITANCE
 a class can be derived from more than one
 parents.
  For example: A class Bat is derived from base
 classes Mammal and WingedAnimal.
  It makes sense because bat is a mammal as well
 as a winged animal.
class A
{
... .. ...
};
class B
{
... .. ...
};
class C: public A, public B
{
... .. ...
};
MULTILEVEL INHERITANCE
A derived class with one base class and that base
class is a derived class of another is
called multilevel inheritance.
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
class C: public B
{
... .. ...
};
HIERARCHICAL INHERITANCE
Multiple derived classes with same base class is
called hierarchical inheritance.
Opposite of Multiple Inheritance
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
class C: public A
{
... .. ...
};
HYBRID/VIRTUAL INHERITANCE
Combination of multiple and hierarchical
inheritance is called hybrid inheritance.
class A
{
... .. ...
};
class B
{
... .. ...
};
class C: public A, public B
{
... .. ...
};
class D: public C
{
... .. ...
};
class E: public C
{
... .. ...
};