Inheritance

• Inheritance is like a child inheriting the features of its parents.
• It is a technique of organizing information in a hierarchical (tree) form.
• Inheritance allows new classes to be created from older and less specialized classes instead of being written from the scratch.
• So classes are created by first inheriting all the variables and functions defined by some primitive class and then adding specialized variables and functions.
• Inheritance is a prime feature of OOP Languages

Arrange concepts into an inheritance hierarchy

• Concepts at higher levels are more general
• Concepts at lower levels are more specific (inherit properties of concepts at higher levels)

Advantages of inheritance

• When a class inherits from another class, there are three benefits:
• (1) You can reuse the methods and data of the existing class
(2) You can extend the existing class by adding new data and new methods
(3) You can modify the existing class by overloading its methods with your own implementations

Base and Derived Class

• In the process of inheritance, the existing classes that are used to derive new classes are called Base Classes and the new classes derived from existing classes are called Derived Classes.
• When a class is derived from a base class, the derived class inherits the characteristics of base class and can add new features as refinements and improvements.
• It improves the program reliability.
• A base class is also called ancestor, parent or super class and derived class is also called as descendent, child or subclass.
6

Define a Class Hierarchy

• Syntax:
class DerivedClassName : access-level BaseClassName
where
– access-level specifies the type of derivation
• private by default, or
• public
• Any class can serve as a base class
– Thus a derived class can also be a base class i.e. another
class can be derived from the derived class itself.

Base and Derived Class

Deriving class from parent classes

• The derived class inherits all the features of its parent class except the members declared as private in base class.
• In addition to inheriting the features of base class, the derived class adds its own new features.
• The syntax of declaration of derived class is
class derivedclass : baseclass
{
// member of derived class
};

Derivation Syntax

Class Derivation

Point is the base class of 3D_Point, while 3D_Point is the base class of Sphere

What to inherit?

• In principle, every member of a base class is inherited by a derived class.
• But the inheritance is controlled by
– just with different access permission
• So not all the members of base classes can be inherited to derived class.

Access Control Over the Members

• Two levels of access control over class members
– class definition
– inheritance type

Access Specifiers (private, protected, and public)

• Private: These members are not accessible outside class and even in the derived class of the class.
• Protected: These types of members of a class are like private in the class where they are defined but inherited to derived classes and accessible in derived classes
• Public: These types of members are fully accessible in base, derived as well as outside the class.

Accessibility in Inheritance

Example: Inheritance

Example: Analysis:

• A protected member (i.e. int count) is introduced in base class. A private member can not be accessed by objects of a derived class. The protected members can be accessed by the member functions of derived class.
• e.g. operator--( ) is defined in derived class two which can access the protected data ‘count’ of class one.
• The derived class Two inherits all the features of class members base class One.
• In main( ), object of derived class is created(i.e. Two i;) which can use both operator ++( ) defined in base class and operator --( ) in derived class.
• The derived class Two which is child class gets member data count and member function operator ++( ) as inherit attribute of its parent
• It is not possible to define an object of base class that use -- overloaded operator: since -- is member of derived class .
• The objects of base class can access the member of its own class only.

Types of Inheritance

• OOPs support the six different types of
inheritance as given below :
– Single inheritance
– Multiple inheritance
– Multi-level inheritance
– Multipath inheritance
– Hierarchical Inheritance
– Hybrid Inheritance

Single inheritance


• In this inheritance, a derived class is created from a single base class.
• In the given example, Class A is the parent class and Class B is the child class since Class B inherits the features and behavior of the parent class A.

Multiple inheritance

• In this inheritance, a derived class is created from more than one base class.
• In the given example, class C inherits the properties and behavior of class B and class A at same level. So, here A and Class B both are the parent classes for Class C.

Multi-level inheritance

• In this inheritance, a derived class is created from another derived class. • In the given example, class C inherits the properties and behavior of class B and class B inherits the properties and behavior of class A.
• So, here A is the parent class of B and class B is the parent class of C.
• So, here class C implicitly inherits the properties and behavior of class A along with Class B i.e there is a multilevel of inheritance.
• There is no limit on the depth of inheritance allowed in C++ (as far as it is within the limits of your compiler)

Syntax for multi-level Inheritance

Multipath inheritance

• In this inheritance, a derived class is created from another derived classes and the same base class of another derived classes.

Multipath inheritance

• In the given example above, class D inherits the properties and behavior of class C and class B as well as Class A.
• Both class C and class B inherits the Class A. So, Class A is the parent for Class B and Class C as well as Class D.
• D inherits features of class A via different Paths
• So it's making it Multipath inheritance.

Hierarchical Inheritance

• In this inheritance, more than one derived classes are created from a single base class and further child classes act as parent classes for more than one child classes.
• It is the one parent-many childs relationship.
• In the given example in previous slide:
– class A has two childs class B and class C.
– Further, class B has two childs D and E.
– And class C has two childs class F and G
• This makes the hierarchical inheritance
• It is just like opposite of multiple inheritance
where there was many parent- one child
relation

Hybrid inheritance

• This is combination of more than one inheritance.
• Hence, it may be a combination of:
– Multilevel and Multiple inheritance
– or Hierarchical and Multilevel inheritance
– or Hierarchical and Multipath inheritance
– or Hierarchical, Multilevel and Multiple
inheritance.
A hybrid inheritance : Combination of Hierarchical and Multiple Inheritance

Constructor in Derived Class

• The derived class need not have a constructor as long as the base class has a no- argument constructor.
• If base class has constructor with arguments (one or more) then derived class must have a constructor explicitly defined such that it passes arguments to the base class constructor.
• In the application of inheritance, objects of derived class are usually created instead of the base class.
• So derived class should have constructor and pass arguments to the constructor of base class.
• When an object of a derived class is created, the constructor of the base class is executed first and later the constructor of the derived class is executed.

Example: Constructor in Derived Class

Explanation of example

• In the previous example, there is no constructor in derived class but we have create object obj_d1 of derived class.
• There is no constructor in derived class which is not required since base class contains no parameterized constructor.
• When object of derived class has been instantiated, the base class constructor has executed, which we can see in output of the program
• Similarly, when constructor is present only derived class it is invoked at the object instantiation of derived class

Constructor in base and derived class

• When both base and derived class have constructor with arguments, the derived class constructor must explicitly call the base class constructor.
• The syntax of explicit call is :
Derived(data_type arg):Base(arg)
{
// other def if any
}
• Derived() and Base() are Derived class and Base Class constructors respectively.

Example: Base and Derived class Conatructor

Example: Base and Derived class Conatructor

Constructor in multiple inherited classes with explicit invocation

• In the derived class with multiple inheritance, the data members of all the parent classes are inherited which are protected or public.
• For no argument constructor, it is optional for defining constructor in either classes.
• For the parameterized constructor in derived class, the base class constructors must be called explicitly
• Look the examples in successive slides.

Example: Multiple Inheritance

Order of Execution of Base and Derived constructors

• When an object of derived class is instantiated, Base class constructor executes firs and then only Derived class constructor.
• For multiple inheritance, the order of explicit call of constructor does not affect the order of execution of the constructor.
• The execution of multiple base class constructor for derived class object depends on the order of derivation of class.
• In previous example, the explicit call of constructor is:
derived():base1(),base2()
// explicit call
• But the derivation order is:
class derived : public base1, public base2
• Notice that ,the execution order of constructor is: base1, base2 and derived

Example : Constructor in Multiple inheritance

0 Comments