0% found this document useful (0 votes)
23 views52 pages

Chapter-8 - OOPC Inheritance - Updated

Uploaded by

alex.moon268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views52 pages

Chapter-8 - OOPC Inheritance - Updated

Uploaded by

alex.moon268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Chapter-8 Inheritance

Prepared By: Aayushi Chaudhari


Assistant Professor, CE,CSPIT.

8 April 2022| U & P U. Patel Department of Computer Engineering 1


Contents
Introduction
Defining a derived class
Example of Single Inheritance
Public and private inheritance
Multilevel, multiple and hierarchical Inheritance
Hybrid Inheritance
Virtual Base Class
abstract class nesting of classes
constructors in derived classes

Weightage: 13%
Hours: 6 Hours needed
8 April 2022| U & P U. Patel Department of Computer Engineering
Inheritance in C++
Inherit Definition - Derive quality and characteristics from parents or
ancestors. Like you inherit features of your parents.
Example: "She had inherited the beauty of her mother"
Inheritance in Object Oriented Programming can be described as a
process of creating new classes from existing classes.
The process of obtaining the data members and methods from one
class to another class is known as inheritance. It is one of the
fundamental features of object-oriented programming.
8 April 2022| U & P U. Patel Department of Computer Engineering
Inheritance in C++
• Inheritance is the capability of one class to acquire properties and characteristics from
another class.
• The class whose properties are inherited by other class is called the Parent or Base or
Super class.
• And, the class which inherits properties of other class is called Child or Derived or Sub
class.
• Inheritance makes the code reusable. When we inherit an existing class, all its methods
and fields become available in the new class, hence code is reused.

NOTE : All members of a class except Private, are inherited

8 April 2022| U & P U. Patel Department of Computer Engineering


Advantage of inheritance
• If we develop any application using this concept than that application have following
advantages:
1. Application development time is less.
2. Application take less memory.
3. Application execution time is less.
4. Application performance is enhance (improved).
5. Redundancy (repetition) of the code is reduced or minimized so that we get consistence
results and less storage cost.
6. Use of Virtual Keyword

8 April 2022| U & P U. Patel Department of Computer Engineering


Object Oriented Programming in C++

In the above diagram data members and methods are represented in broken line are
inherited from faculty class and they are visible in student class logically.
8 April 2022| U & P U. Patel Department of Computer Engineering
Object Oriented Programming in C++
Syntax of Inheritance
class Subclass_name : access_mode Superclass_name
{
// data members
// methods
}
While defining a subclass like this, the super class must be already defined or at least declared
before the subclass declaration.
Access Mode is used to specify, the mode in which the properties of superclass will be inherited into
subclass, public, private or protected.
: is operator which is used for inheriting the features of base class into derived class it improves the
functionality of derived class. class Subclass_name : access_mode Superclass_name { // data
members // methods }
8 April 2022| U & P U. Patel Department of Computer Engineering
Example of Inheritance in C++
class Rectangle
{
... .. ...
Parent class/Super class

};
class Area : public Rectangle
{ Sub class

... .. ...
};
class Perimeter : public Rectangle
{ Sub class

.... .. ...
};
8 April 2022| U & P U. Patel Department of Computer Engineering
Example of Inheritance
Class Dog inherits the properties from its super class Animal.

Animal

Dog

8 April 2022| U & P U. Patel Department of Computer Engineering


Example of Inheritance
class Animal
{
• Sub class Dog inherit the Properties of Super class
public: Animal.
};
int legs = 4;
• Dog ‘d’ is the object of subclass.
class Dog : public Animal
{
public: • Output:
int tail = 1;
};
Legs are: 4
int main()
Tail is: 1
{
Dog d;
cout <<"Legs are: "<<d.legs<<endl;
cout << "Tail is: "<<d.tail;
}

8 April 2022| U & P U. Patel Department of Computer Engineering


Defining derived class
• 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 or 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

8 April 2022| U & P U. Patel Department of Computer Engineering


Defining derived class
2) Private Inheritance
• In private mode, the protected and public members of super class become private
members of derived class.
3) Protected Inheritance
• In protected mode, the public and protected members of Super class becomes protected
members of Sub class.
class Subclass : Superclass

Note: By default its private inheritance class subclass : protected Superclass

8 April 2022| U & P U. Patel Department of Computer Engineering


Defining derived class with different visibility mode

Derived Class Derived Class Derived Class

Base Class Public mode Private Mode Protected Mode

Private Not Inherited Not Inherited Not Inherited

Protected Protected Private Protected

Public Public Private Protected

8 April 2022| U & P U. Patel Department of Computer Engineering


Access Control and Inheritance
A derived class can access all the non-private members of its base class. Thus base-class
members that should not be accessible to the member functions of derived classes should
be declared private in the base class.

Access Public Protected Private

Same class Yes Yes Yes

Derived Class Yes Yes No

Outside Class Yes No No

8 April 2022| U & P U. Patel Department of Computer Engineering


Inheritance Visibility Mode
How Members of the Base Class Appear in
the Derived Class
• Private members of the base class are
Member Access Specifier inaccessible to the derived class.
• Protected members of the base class
become private members of the derived
Private class.
• Public members of the base class become
private members of the derived class.

8 April 2022| U & P U. Patel Department of Computer Engineering


Inheritance Visibility Mode
How Members of the Base Class Appear in
the Derived Class
• Private members of the base class are
Member Access Specifier inaccessible to the derived class.
• Protected members of the base class
become protected members of the
Protected derived class.
• Public members of the base class become
protected members of the derived class.

8 April 2022| U & P U. Patel Department of Computer Engineering


Inheritance Visibility Mode
How Members of the Base Class Appear in
the Derived Class
• Private members of the base class are
Member Access Specifier inaccessible to the derived class.
• Protected members of the base class
become protected members of the
Public derived class.
• Public members of the base class become
public members of the derived class.

8 April 2022| U & P U. Patel Department of Computer Engineering


Access Control and Inheritance
• A derived class inherits all base class methods with the following exceptions:

Constructors, destructors and copy constructors of the base class.

Overloaded operators of the base class.

The friend functions of the base class.

8 April 2022| U & P U. Patel Department of Computer Engineering


Example(Accessibility)

8 April 2022| U & P U. Patel Department of Computer Engineering


Types of Inheritance
Based on number of ways inheriting the feature of base class into derived class it have five
types they are:
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
• Hybrid inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Types of Inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Single Inheritance
• In single inheritance there exists single base class and single derived class.
• It is the most simplest form of Inheritance.

Syntax:
class subclass_name : access_mode base_class
{
// body of subclass
};

8 April 2022| U & P U. Patel Department of Computer Engineering


Single Inheritance Example
• When a single class is derived from a single parent class, it is called Single inheritance. It
is the simplest of all inheritance.

• For example,
Animal is derived from living things
Car is derived from vehicle
Manager derived from employee
Circle derived from shapes

8 April 2022| U & P U. Patel Department of Computer Engineering


Syntax of Single Inheritance in C++
class base_classname
{
properties...
Methods...
};
class derived_classname : visibility_mode base_classname
{
properties...
methods...
};
8 April 2022| U & P U. Patel Department of Computer Engineering
Single Inheritance Example
• For example: Car, Bicycle, Motorcycle are all vehicles and have many similar properties
like tire, brakes, seat, etc. So they can be derived from class Vehicle.
• Therefore, vehicle is base class and car, bus, motorcycle are derived classes.

8 April 2022| U & P U. Patel Department of Computer Engineering


Example of Single Inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Multiple Inheritance
• In this type of inheritance a single derived class may inherit from two or more than two
base classes.

8 April 2022| U & P U. Patel Department of Computer Engineering


Example of Multiple inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Multilevel inheritance
• In this type of inheritance the derived class inherits from a class, which in turn inherits
from some other class.
• The Super class for one, is sub class for the other.
• When a class is derived from a class which is also derived from another class, i.e. a class
having more than one parent classes, such inheritance is called Multilevel Inheritance.
• The level of inheritance can be extended to any number of level depending upon the
relation.
• Multilevel inheritance is similar to relation between grandfather, father and child.

8 April 2022| U & P U. Patel Department of Computer Engineering


Example of Multilevel Inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Hierarchical Inheritance
• In this type of inheritance, multiple derived classes inherits from a single base class.
• When more than one classes are derived from a single base class, such inheritance is
known as Hierarchical Inheritance, where features that are common in lower level are
included in parent class.
• Problems where hierarchy has to be maintained can be solved easily using this
inheritance.
For Example:
• Civil, Computer, Mechanical, Electrical are derived from Engineer.
• Natural language, Programming language are derived from Language.

8 April 2022| U & P U. Patel Department of Computer Engineering


Syntax
class base_classname
{ properties; methods; };
class derived_class1:visibility_mode base_classname
{ properties; methods; };
class derived_class2:visibility_mode base_classname
{ properties; methods; };
... ... ...
class derived_classN:visibility_mode base_classname { properties; methods; };

8 April 2022| U & P U. Patel Department of Computer Engineering


Simple Example of Hierarchical Inheritance
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}

8 April 2022| U & P U. Patel Department of Computer Engineering


Example of hierarchical Inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Hybrid Inheritance
Hybrid Inheritance is a method where one or more types of inheritance are combined
together and used.
Example of Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.

8 April 2022| U & P U. Patel Department of Computer Engineering


Example of hybrid Inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Ambiguity in Multiple Inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Resolution of Ambiguity in Multiple Inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Ambiguity Resolution in Single Inheritance

8 April 2022| U & P U. Patel Department of Computer Engineering


Constructors in Derived Classes
• We can use constructors in derived classes in C++.
• If the base class constructor does not have any arguments, there
is no need for any constructor in the derived class.
• But if there are one or more arguments in the base class
constructor, derived class need to pass argument to the base class
constructor
• If both base and derived classes have constructors, base class
constructor is executed first.
8 April 2022| U & P U. Patel Department of Computer Engineering
Constructors in Multiple Inheritances
• Consider the example of three classes “A”, “B”, and “C”.
• In multiple inheritances, base classes are constructed in the order in which they appear in
the class declaration. For example if class “C” is inheriting classes “A” and “B”. If the
class “A” is written before class “B” then the constructor of class “A” will be executed
first. But if the class “B” is written before class “A” then the constructor of class “B” will
be executed first.
• In multilevel inheritance, the constructors are executed in the order of inheritance. For
example class “B” is inheriting classes “A” and the class “C” is inheriting classes “B”.
Then the constructor will run according to the order of inheritance such as the constructor
of class “A” will be called first then the constructor of class “B” will be called and at the
end constructor of class “C” will be called.

8 April 2022| U & P U. Patel Department of Computer Engineering


Passing arguments to multiple base class using derived class’s
constructor
• The constructor of the derived class receives all the arguments at once and then will pass
the call to the respective base classes.
• The body is called after the constructors finishes execution.
Derived-Constructor (arg1, arg2, arg3….): Base 1-Constructor (arg1,arg2), Base 2-
Constructor(arg3,arg4)
{
….
} Base 1-Constructor (arg1,arg2)

8 April 2022| U & P U. Patel Department of Computer Engineering


Example of Constructor

8 April 2022| U & P U. Patel Department of Computer Engineering


Important Points
1. In inheritance, the order of constructors calling is: from child class to parent class (child ->
parent).

2. In inheritance, the order of constructors execution is: from parent class to child class (parent
-> class).

3. In inheritance, the order of destructors calling is: from child class to parent class (child ->
parent).

4. In inheritance, the order of destructors execution is: from child class to parent class (child ->
parent).

8 April 2022| U & P U. Patel Department of Computer Engineering


Example of Destructor

8 April 2022| U & P U. Patel Department of Computer Engineering


Constructors for Virtual Base class
• The constructors for virtual base classes are invoked before any non-virtual base classes.
• If there are multiple virtual base classes, they are invoked in the order in which they are
declared. Any non-virtual bases are then constructed before the derived class constructor is
executed.
Mode of Inheritance Execution Order of calling
class child: public parent Parent(); Parent’s constructor
Child(); child’s constructor
class child: public father, public mother father(); parent-1
Mother(); parent-2
Child(); child
class child: public father, virtual public Mother(); Virtual
mother Father(); normal
Child(); child

8 April 2022| U & P U. Patel Department of Computer Engineering


Virtual Base Class
• The virtual base class is a concept used in multiple inheritances
to prevent ambiguity between multiple instances.
• Consider the situation where we have one class A .This class
is A is inherited by two other classes B and C. Both these class
are inherited into another in a new class D.
• As we can see from the figure that data members/function of
class A are inherited twice to class D. One through class B and
second through class C.
• When any data / function member of class A is accessed by an
object of class D, ambiguity arises as to which data/function
member would be called? One inherited through B or the other
inherited through C. This confuses compiler and it displays
error.
8 April 2022| U & P U. Patel Department of Computer Engineering
Example showing ambiguity

8 April 2022| U & P U. Patel Department of Computer Engineering


Resolution of Ambiguity
when class A is inherited in both class B and class C, it is declared as virtual base class by placing a
keyword virtual.

Syntax:
class B : virtual public A
Note: virtual can be written before or after the public.
Now only one copy of data/function member will be copied
{
to both derived classes and class A becomes the virtual
};
base class.
OR
class B : public virtual A
{
};
8 April 2022| U & P U. Patel Department of Computer Engineering
Benefits of Virtual Base class
• Virtual base classes offer a way to save space and avoid
ambiguities in class hierarchies that use multiple
inheritances.
• When a base class is specified as a virtual base, it can act as
an indirect base more than once without duplication of its
data members.
• A single copy of its data members is shared by all the base
classes that use virtual base.
8 April 2022| U & P U. Patel Department of Computer Engineering
Example

8 April 2022| U & P U. Patel Department of Computer Engineering


Thank You.

6 April 2022| U & P U. Patel Department of Computer Engineering

You might also like