This document discusses inheritance in programming fundamentals. It defines inheritance as deriving a new class from an existing class. The existing class is the base class and the new class is the derived class. The document describes different types of inheritance like single, multiple, hierarchical, multilevel, and hybrid inheritance. It also discusses virtual base classes and abstract classes.
Water Industry Process Automation & Control Monthly - April 2024
Ritik (inheritance.cpp)
1. NAME - RITIK
ROLL NO. - UE198077
SUBJECT - PROGRAMMING FUNDAMENTAL
SUBMITTED TO - SUKHVIR SIR
TOPIC - INHERITANCE: EXTENDING CLASSES
B.E.(IT),1st year
2. Inheritance:
The mechanism of deriving a new class from an old one is called Inheritance.
The old class is referred to as the base class.
The new one is called the derived class or subclass.
3. General form of defining a derived class
Syntax:
Class derived-class-name : visibility mode base-class-name
{
……..//
…….// members of derived class
…….//
};
The colon indicate that the derived-class-name is derived from the base-class-name. The visibility mode is
optional and, if present, may be either private or public. The default visibility mode is private.
Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
4. Types of Inheritance
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance
5. Single Inheritance
In single inheritance there exists single
base class and single derived class.
It is the most simplest form of
inheritance.
8. Multiple inheritance
Multiple inheritance occurs when a class
is inherit from more than one base class.
Derived class can inherit features from
multiple base classes using multiple
inheritance.
11. Hierarchical inheritance
In this type of inheritance, more than one sub
class is inherited from a single base class. i.e.
more than one derived class is created from a
single base class.
14. Multilevel inheritance
In this type of inheritance, a derived class is
created from another derived class.
The class C serves as a base class for the
derived class B, which in turn serves as a base
class for the derived class A. The class B as
intermediate base class since it provides a link
for the inheritance between A and C the chain
ABC is known as inheritance path.
Class C {………}; //base class
Class B: public C {………}; //B derived from C
Class A: public B {……}; //A derived form B
17. Hybrid inheritance
Hybrid inheritance is implemented by
combining more than one type of
inheritance. For example: combining
hierarchical inheritance and multiple
inheritance.
20. Virtual base class
Consider a situation where three kind of inheritance, namely, multilevel, multiple and
hierarchical inheritance, are involved. This is illustrated in the figure.
The ‘child’ has two direct base classes ‘parent1’ and
‘Parent2’ which themselves have a common base class
‘grand parent’. The ‘child’ inherits the traits of ‘grandparent’
via two separate paths. It can also inherits the base class
directly. The ‘grand parent’ is sometimes referred to as the indirect base class.
This means ‘child’ is inherit the same property twice. To remove this duplication we
declare the base class ‘grandparent’ as the virtual while declaring the direct or
intermediate base class as shown:
21. Syntax
class A //grandparent
{ …….};
Class B1:virtual public A //parent1
{……..};
Class B2:public virtual A //parent2
{…….};
Class C:public B1,public B2 //child
{…….}; //only one copy of A will be inherited
22. Abstract classes
An abstract class is one that is not used to create objects. An abstract class is
designed only to act as a base class (to be inherited by other classes). It is a design
concept in program development and provides a base upon which other classes may
be built.
The concept can be explained through a simple example of an abstract base class
vehicle which may be declared in a program for deriving LMV (light motor vehicle),HMV
(heavy motor vehicle) and TW(two wheeler) derived classes. A pure virtual function
spec() may be set the specification of LMV as per its four wheel and smaller engine
and that of HMV as per its eight or more wheels and relatively larger engine.
23. Example
Class vehicle //abstract base class
{ private:
Datatype d1,d2;
public:
virtual void spec()=0; //pure virtual funtion
};
Class LMV : public vehicle
{ public:
void spec(){ ……. } //LMV definition of spec function
};
24. Example
Class HMV : public vehicle
{ public:
void spec(){ …….. } HMV definition of spec function
};
Class TW : public vehicle
{ public:
void spec(){ …….. } //TW definition of spec function
};
25. Advantages of inheritance
Inheritance promote reusability. When a class inherits or derives another class, it
can access all functionality of inherited class.
Reusability enhanced reliability. the base class code is already tested and
debugged.
Inheritance helps to reduce code redundancy and supports code extensibility.
26. Disadvantages of inheritance
Inherited functions work slower than the normal function as there is indirection.
Improper use of inheritance lead to wrong solution.
Inheritance increases the coupling between base class and derived class. A change
in base class will affects all the child classes.
Hinweis der Redaktion
NOTE:
To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image.