0% found this document useful (0 votes)
48 views

Unit 3

Inheritance allows classes to inherit properties and characteristics from other classes. The class that inherits is called the subclass or derived class, while the class being inherited from is called the base class or superclass. Inheritance avoids duplicating code and properties across multiple classes. The constructor and destructor of base classes are always called before those of the derived class.

Uploaded by

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

Unit 3

Inheritance allows classes to inherit properties and characteristics from other classes. The class that inherits is called the subclass or derived class, while the class being inherited from is called the base class or superclass. Inheritance avoids duplicating code and properties across multiple classes. The constructor and destructor of base classes are always called before those of the derived class.

Uploaded by

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

Object Oriented Programming

(Inheritance)
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/child class/ derived class
● The class that inherits properties from another class is called Sub class or
Derived Class.
Super Class/base class
● The class whose properties are inherited by sub class is called Base Class
or Super class.
class A
class B:protected A
class C:public B
Why and when to use inheritance?
● Consider a group of vehicles. You need to create classes for Bus, Car and
Truck. The methods fuelAmount(), capacity(), applyBrakes() will be
same for all of the three classes. If we create these classes avoiding
inheritance then we have to write all of these functions in each of the
three classes as shown in below figure:
● You can clearly see that above process results in duplication of same code 3
times. This increases the chances of error and data redundancy. To avoid this
type of situation, inheritance is used. If we create a class Vehicle and write
these three functions in it and inherit the rest of the classes from the vehicle
class, then we can simply avoid the duplication of data and increase reusability.
Look at the below diagram in which the three classes are inherited from
vehicle class:
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.
● 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 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.
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.

● Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};
Multiple Inheritance
● Multiple Inheritance is a feature of C++ where a class can inherit from more
than one classes. i.e one sub class is inherited from more than one base classes.

● Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
//body of subclass
};
Multilevel Inheritance
● In this type of inheritance, a derived class is created from another derived
class.
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.
Hybrid (Virtual) Inheritance
● Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and
Multiple Inheritance.
A special case of hybrid inheritance :
Multipath inheritance
● A derived class with two base classes and these two base classes have
one common base class is called multipath inheritance. An ambiguity can
arise in this type of inheritance.
Ambiguity
● In the above example, both ClassB & ClassC inherit ClassA, they both
have single copy of ClassA. However ClassD inherit both ClassB &
ClassC, therefore ClassD have two copies of ClassA, one from ClassB
and another from ClassC.

● If we need to access the data member a of ClassA through the object of


ClassD, we must specify the path from which a will be accessed, whether
it is from ClassB or ClassC, because compiler can’t differentiate between
two copies of ClassA in ClassD.
Ambiguity(contd)
● There are 2 ways to avoid this ambiguity:
○ Use scope resolution operator
○ Use virtual base class
Ambiguity (contd)
● Avoiding ambiguity using scope resolution operator:
● Using scope resolution operator we can manually specify the path from
which data member a will be accessed, as shown in statement 3 and 4, in
the above example.
Ambiguity (contd)
● Avoiding ambiguity using virtual base class
#include<iostream>
class B:virtual public A
using namespace std;
{
class A
int b;
{
public:
int a;
int input()
public:
{
int input()
{
cout<<"enter the value of b";
cout<<"enter the value of a";
cin>>b;
cin>>a;
return 0;
return 0;
}
}
int output()
int output()
{
{
cout<<b;
cout<<a;
return 0;
return 0;
}
}
};
};
class C:public virtual A class D:public B, public C
{ {
int c; int d;
public: public:
int input() int input()
{ {
cout<<"enter the value of c"; cout<<"enter the value of d";
cin>>c; cin>>d;
return 0; return 0;
} }
int output() int output()
{ {
cout<<c; cout<<d;
return 0; return 0;
} }
}; };
int main()
{
D d1;
d1.A::input();
d1.B::input();
d1.C::input();
d1.D::input();

return 0;
}
Order of Constructor/ Destructor Call in C++
● Whenever we create an object of a class, the default constructor of that
class is invoked automatically to initialize the members of the class.
● If we inherit a class from another class and create an object of the derived
class, it is clear that the default constructor of the derived class will be
invoked but before that the default constructor of all of the base classes
will be invoke, i.e the order of invokation is that the base class’s default
constructor will be invoked first and then the derived class’s default
constructor will be invoked.
#include<iostream>
using namespace std;
class A class C:public A,public B
{ {
int a; int c;
public:
A() public:
{ C()
a=10; {
cout<<a;
} c=30;
~A() cout<<c;
{
cout<<"destructor a called";
} }
}; ~C()
class B {
{
int b; cout<<"destructor c called";
public: }
B() };
{
b=20;
cout<<b; int main()
} {
~B()
{ C c1;
cout<<"destructor b called"; return 0;
} }
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy