Inheritance in C++
NOOR AFSHAN
What is Inheritance?
The capability of a class to derive properties and characteristics from another
class is called Inheritance.
Inheritance is one of the most important feature of Object Oriented
Programming.
Sub Class:
The class that inherits properties from another class is called Sub class or Derived
Class.
Super Class:
The class whose properties are inherited by sub class is called Base Class or Super
class.
Why and when to use Inheritance?
Capability to express the inheritance relationship.
Idea of reusability
Transitive nature of inheritance
Advantages of Inheritance
Software development time is less.
Software take less memory.
Code execution time is less.
Code performance is enhance (improved).
Redundancy (repetition) of the code is reduced or minimized so that we get
consistence results and less storage cost.
Real life example of inheritance
Example
Example
Modes of Inheritance
Public mode: If we derive a sub class 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. Private
members of the base class will never get inherited in sub class.
Protected mode: If we derive a sub class from a Protected base class. Then both
public member and protected members of the base class will become protected in
derived class. Private members of the base class will never get inherited in sub class.
Private mode: If we derive a sub class from a Private base class. Then both public
member and protected members of the base class will become Private in derived
class. Private members of the base class will never get inherited in sub class.
Modes of Inheritance
Types of Inheritance
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multilevel inheritance
Hybrid inheritance
Single Inheritance
In single inheritance, a class is allowed to inherit
from only one class. i.e. one sub class is inherited
by one base class only.
There exists single base class and single derived
class.
Syntax
class subclass_name : access_mode base_class
{
//body of subclass
};
Multiple Inheritance