Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1
Haresh Jaiswal
Rising Technologies, Jalna.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2
Introduction
 Inheritance allows us to extend the definition of a class without
making any physical changes to the existing class.
 Inheritance lets you create new classes reusing some features
from existing class.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3
Introduction
 Any new class that you create from an existing class is
called derived class & existing class is called as base class.
A
B
Base Class
Derived
Class
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4
Introduction
 The inheritance enables a derived class to inherit features from
its base class. Furthermore, the derived class can add new
features of its own.
A
B
void show()
{
}
void show()
{
}
void print()
{
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5
Introduction
 Therefore, rather than to create a completely new class from
scratch, you can take advantage of inheritance and reduce
software complexity.
A
B
void show()
{
}
void show()
{
}
void print()
{
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6
Inheritance
Class Bird
Non Flying BirdFlying Bird
Robin Swallow Penguin Kiwi
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7
Inheritance
 The concept of inheritance provides the idea of reusability. This
means that we can add additional features to an existing class
without modifying it.
A
B
void show()
{
}
void show()
{
}
void print()
{
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8
Inheritance
 Inheritance is the process by which objects of one class acquire
properties of another class. It supports the concepts of
hierarchical classification.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9
Forms/Types of Inheritance
 Single Inheritance
 Multiple Inheritance
 Multi-Level Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10
Single Inheritance
 It is the inheritance hierarchy wherein a derived class inherits
from only one base class.
Class A
Class B
Base Class
Derived Class
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11
Multiple Inheritance
 It is the inheritance hierarchy wherein a derived class inherits
from multiple base classes.
Class BClass A
Class C
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12
Multi Level Inheritance
 It is the inheritance hierarchy wherein subclass acts as a base
class for other classes.
Class B
Class A
Class C
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13
Hierarchical Inheritance
 It is the inheritance hierarchy wherein multiple subclasses inherit
from one base class.
Class B
Class A
Class C
Class D Class E
Class D
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14
Hybrid Inheritance
 The inheritance hierarchy that reflects any legal combination of
other four types of inheritance.
Class B
Class A
Class C
Class D
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15
Defining Derived Class
 Derived class can be defined by specifying its relationship with
the base class in addition to its own details.
 In order to derive a class from another, we use a colon (:) in the
declaration of the derived class using the following format.
class Derived_Class : VisiblityModifier Base_Class
{
...
...
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16
Defining Derived Class
 Where Derived_Class is the name of the derived class and
Base_Class is the name of the class on which it is based.
class Derived_Class : VisiblityModifier Base_Class
{
...
...
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17
Defining Derived Class
 The VisiblityModifier is optional, and if present it may be
 public
 protected
 private.
 It describes the access level for the members that are inherited from
the base class.
class Derived_Class : VisiblityModifier Base_Class
{
...
...
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18
Defining Derived Class
 If VisiblityModifier is not specified then default visibility is
private.
class Derived_Class : Base_Class
{
...
...
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19
Defining Derived Class
 A class can have members under following 3 visibilities
 Public
 Protected
 Private
 In inheritance, public & protected members of base class gets
inherited into derived class.
 But private members of base class are inaccessible to derived
class.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20
Public Derivation
class Abc
{
// members of ABC
};
class Xyz : public Abc
{
// members of XYZ
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21
Private Derivation
class Abc
{
// members of ABC
};
class Xyz : private Abc
{
// members of XYZ
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22
Protected Derivation
class Abc
{
// members of ABC
};
class Xyz : protected Abc
{
// members of XYZ
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 23
Default Private Derivation
class Abc
{
// members of ABC
};
class Xyz : Abc
{
// members of XYZ
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 24
Private Visibility Mode
 When a base class is privately inherited by a derived class,
 All public & protected members of base class becomes private
members of derived class.
 No private member of base class will be accessible by derived class.
 In this case the public as well as protected members of base class
can be accessed by the member functions of derived class.
 But no member of base class can be accessed by object of
derived class.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 25
Public Visibility Mode
 When a base class is publicly inherited by a derived class,
 all public members of base class becomes public members of
derived class.
 all protected members of base class will become protected
members of derived class.
 No private member of base class will be accessible by derived class.
 In this case the public members of base class can be accessed via
object of derived class.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 26
Protected Visibility Mode
 C++ Provides a third visibility modifier ‘protected’, which serve a
limited purpose in inheritance.
 A member declared as protected is accessible by the member
functions within its class and any class immediately derived from
it.
 It cannot be accessed by the functions outside these two classes.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 27
Protected Visibility Mode
 When a base class is protectedly inherited by a derived class,
 all public & protected members of base class becomes protected
members of derived class.
 No private member of base class will be accessible by derived class.
 In this case no members of base class can be accessed via object
of derived class.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 28
Visibility Modes
class ABC
{
private: //optional
//accessible to member functions within this class only.
protected:
//accessible to member functions within this class and
derived class only.
public:
//accessible to all functions within the program.
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 29
Private
Protected
Public
Private
Protected
Public
Private
Protected
Public
Private
Protected
Public
Not InheritableNot Inheritable
Class B : Public A
Class A
Class C : Private A
Class X : Protected B

05. inheritance

  • 1.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 1 Haresh Jaiswal Rising Technologies, Jalna.
  • 2.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 2 Introduction  Inheritance allows us to extend the definition of a class without making any physical changes to the existing class.  Inheritance lets you create new classes reusing some features from existing class.
  • 3.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 3 Introduction  Any new class that you create from an existing class is called derived class & existing class is called as base class. A B Base Class Derived Class
  • 4.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 4 Introduction  The inheritance enables a derived class to inherit features from its base class. Furthermore, the derived class can add new features of its own. A B void show() { } void show() { } void print() { }
  • 5.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 5 Introduction  Therefore, rather than to create a completely new class from scratch, you can take advantage of inheritance and reduce software complexity. A B void show() { } void show() { } void print() { }
  • 6.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 6 Inheritance Class Bird Non Flying BirdFlying Bird Robin Swallow Penguin Kiwi
  • 7.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 7 Inheritance  The concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. A B void show() { } void show() { } void print() { }
  • 8.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 8 Inheritance  Inheritance is the process by which objects of one class acquire properties of another class. It supports the concepts of hierarchical classification.
  • 9.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 9 Forms/Types of Inheritance  Single Inheritance  Multiple Inheritance  Multi-Level Inheritance  Hierarchical Inheritance  Hybrid Inheritance
  • 10.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 10 Single Inheritance  It is the inheritance hierarchy wherein a derived class inherits from only one base class. Class A Class B Base Class Derived Class
  • 11.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 11 Multiple Inheritance  It is the inheritance hierarchy wherein a derived class inherits from multiple base classes. Class BClass A Class C
  • 12.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 12 Multi Level Inheritance  It is the inheritance hierarchy wherein subclass acts as a base class for other classes. Class B Class A Class C
  • 13.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 13 Hierarchical Inheritance  It is the inheritance hierarchy wherein multiple subclasses inherit from one base class. Class B Class A Class C Class D Class E Class D
  • 14.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 14 Hybrid Inheritance  The inheritance hierarchy that reflects any legal combination of other four types of inheritance. Class B Class A Class C Class D
  • 15.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 15 Defining Derived Class  Derived class can be defined by specifying its relationship with the base class in addition to its own details.  In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format. class Derived_Class : VisiblityModifier Base_Class { ... ... };
  • 16.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 16 Defining Derived Class  Where Derived_Class is the name of the derived class and Base_Class is the name of the class on which it is based. class Derived_Class : VisiblityModifier Base_Class { ... ... };
  • 17.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 17 Defining Derived Class  The VisiblityModifier is optional, and if present it may be  public  protected  private.  It describes the access level for the members that are inherited from the base class. class Derived_Class : VisiblityModifier Base_Class { ... ... };
  • 18.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 18 Defining Derived Class  If VisiblityModifier is not specified then default visibility is private. class Derived_Class : Base_Class { ... ... };
  • 19.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 19 Defining Derived Class  A class can have members under following 3 visibilities  Public  Protected  Private  In inheritance, public & protected members of base class gets inherited into derived class.  But private members of base class are inaccessible to derived class.
  • 20.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 20 Public Derivation class Abc { // members of ABC }; class Xyz : public Abc { // members of XYZ };
  • 21.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 21 Private Derivation class Abc { // members of ABC }; class Xyz : private Abc { // members of XYZ };
  • 22.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 22 Protected Derivation class Abc { // members of ABC }; class Xyz : protected Abc { // members of XYZ };
  • 23.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 23 Default Private Derivation class Abc { // members of ABC }; class Xyz : Abc { // members of XYZ };
  • 24.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 24 Private Visibility Mode  When a base class is privately inherited by a derived class,  All public & protected members of base class becomes private members of derived class.  No private member of base class will be accessible by derived class.  In this case the public as well as protected members of base class can be accessed by the member functions of derived class.  But no member of base class can be accessed by object of derived class.
  • 25.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 25 Public Visibility Mode  When a base class is publicly inherited by a derived class,  all public members of base class becomes public members of derived class.  all protected members of base class will become protected members of derived class.  No private member of base class will be accessible by derived class.  In this case the public members of base class can be accessed via object of derived class.
  • 26.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 26 Protected Visibility Mode  C++ Provides a third visibility modifier ‘protected’, which serve a limited purpose in inheritance.  A member declared as protected is accessible by the member functions within its class and any class immediately derived from it.  It cannot be accessed by the functions outside these two classes.
  • 27.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 27 Protected Visibility Mode  When a base class is protectedly inherited by a derived class,  all public & protected members of base class becomes protected members of derived class.  No private member of base class will be accessible by derived class.  In this case no members of base class can be accessed via object of derived class.
  • 28.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 28 Visibility Modes class ABC { private: //optional //accessible to member functions within this class only. protected: //accessible to member functions within this class and derived class only. public: //accessible to all functions within the program. };
  • 29.
    Rising Technologies, Jalna(MH). + 91 9423156065, http://www.RisingTechnologies.in 29 Private Protected Public Private Protected Public Private Protected Public Private Protected Public Not InheritableNot Inheritable Class B : Public A Class A Class C : Private A Class X : Protected B