Chapter 3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

Inheritance, Abstract Class

and Virtual in C++


Objective

In this chapter learner able to understand:


• Inheritance in C++
• Abstract Classes and Virtual functions
Inheritance

 Inheritance is a feature by which a class acquires attributes of another


class.
 The mechanism of deriving a new class from an old one is called
inheritance( or derivation ).
 The old class is referred to as the base class and the new one is called the
derived class.
 The derived class inherits some or all of the members from the base class.
 It allow a programmer to enhance a class without breaking or
reconstructing it(Extensibility).
 Inheritance supports reusability of code and is able to simulate the
transitive nature of real life objects.
Types of Inheritance
 Single Inheritance
 Multiple Inheritance

 By combining above following categories are also possible:


 Hierarchical Inheritance
 Multilevel Inheritance
 Hybrid Inheritance
Single Inheritance
A derived class with only one base class is called Single Inheritance.

EMPLOYEE

MANAGER
Multiple Inheritance
A derived class with more than one base class is called Multiple Inheritance.

VC MD

MANAGER
Hierarchical Inheritance
When more than one class inherits from the same class it is called Hierarchical
Inheritance.

EMPLOYEE

MD VC PM
Multilevel Inheritance
When a derived class is the base class of another class, it is called Multilevel
Inheritance.

EMPLOYEE

PM

TL
Hybrid Inheritance

When a class inherits from multiple base classes and all of its base
classes are subclasses of some other class, it is called Hybrid
Inheritance.

TRAIN

RAJDHANI SHATABDI

EXPRESS
Implementing Inheritance

 A derived class can be defined by specifying its relationship


with the base class in addition to its own details.
 Syntax of defining a derived class:
class derived_class_name:visibility_mode base_class_name
{
--------
--------
// members of derived class
--------
--------
};
 The colon indicates that the derived_class name is derived from
the base_class_name.
 The visibility_mode is optional and if not present then the
default visibility_mode is private.
For Example :-
class Derived: public MyBase
{
-------
-------
};
Direct and Indirect base Classs
A base class is called direct if it is mentioned in the base list.

class A
{ };

class B : public A
{ }; // where class A is a direct base class.

An indirect base class can be written as:

class A
{ };
class B : public A
{ };
class C : public B
{ }; // Here class A is a indirect base class or supper class.
Visibility modes in Inheritance
 There are three types of visibility modes.
1. Private
2. Protected
3. Public

 The visibility modes control the accessibility for the inherited members of
base-class in the derived class
Visibility mode table
Access rules for Base class members
EXAMPLE:

class BaseClass //Base class


{
private:
int priv_a;
protected:
int prot_b;
public:
int pub_c;
};
class Derive : public BaseClass //sub class
{
public:
void message();
};
int main()
{

BaseClass o1; //base class object


o1.priv_a = 1; //Error
o1.prot_b = 1; //Error
o1.pub_c = 1; //valid

Derive obj1; //derived class object


obj1.priv_a = 1; //Error
obj1.prot_b = 1; //Error
obj1.pub_c = 1; //valid
return 0;

}
Constructor and Destructor with Inheritance
Constructor and destructor with inheritance

 When an object of a derived class is created , the program first calls the constructor
for base class, then the constructor for derived class .
 Similarly , when an object expires , the program first calls the derived destructor ,if
any ,and then the base class destructor .
 One important point is that constructors and destructors of
base class are not inherited by the derived class.
 Whenever the derived class needs to invoke base class’s
constructor or destructor ,it can invoke them through
explicitly calling them.
 As long as no base class constructor takes any arguments ,
the derived class need not have a constructor function.
 But if any base class contains a constructor with one or
more arguments, then it is mandatory for the derived class
to have a constructor and pass the arguments to the base
class constructors.
Passing Argument to Base from Derived
 To pass the base class arguments , the constructor of the derived
class uses a special argument passing mechanism.
Syntax =>
Derived :: Derived (type1 x, type2 y, …) : baseclass(x,y)
{
-------
-------
}
Polymorphism

1-Compile Time Polymorphism: When an object is bound to its


function call at compile time is called compile time
polymorphism.
 Function Overloading
 Operator Overloading
 Function Hiding/Overhiding
2-Runtime Polymorphism
 Function Overriding
Runtime Polymorphism
Run Time Polymorphism

 When an object is bound to its function call at run time is called run time
polymorphism.
 It is also called late or dynamic binding.
 It’s possible by implementing:-
1. Virtual function
2. Pure virtual function
Run time polymorphism

• In run time polymorphism, an appropriate member function is


selected while the program is running.
• When a function is made virtual,c++ determines which function
is used at run time based on the type of object pointed to by the
base pointer, rather than the type of the pointer.
• By making the base pointer to point to different object, we can
execute different versions of the virtual function.
• Run time polymorphism is achieved only when a virtual
function is accessed through a pointer to the base class.
• It can not be achieved using object name along with the dot
operator to access virtual function.
Virtual function

 A function preceding by keyword virtual is called a virtual function.


virtual void show()
{
………..
}
 When base pointer is made to point to the derived object, it executes derived
class function.
Rules for Virtual function

 The virtual function must be member of some class.


 They can not be static members.
 They are accessed by using object pointers.
 A virtual function can be a friend of another class.
 The prototype of the base class version of a virtual function and all the
derived class versions must be identical. If two functions with the same
name have different prototypes, C++ considers them as overloaded
functions, and the virtual function mechanism is ignored.
Pure Virtual functions
Pure Virtual functions

 It is normal practice to declare a function virtual inside the base class and
redefine it in the derived classes.
 Sometime the function inside the base class is not used for performing any
task,such functions are called do-nothing functions.
 A do-nothing function may be defined as follows:-
virtual ret-type fun-name(parameter-list)=0;

Example: virtual void show()=0;

 Such functions are called pure virtual functions.


 A pure virtual function is a function declared in a base class that has no
definition relative to the base class .
 A class containing pure virtual functions cannot be used to declare any objects
of its own, such classes are called abstract class.
Abstract Classes
 A class containing one or more pure virtual functions is called an
abstract class.
 Only useful as a base class to be inherited into a useable derived class.
 No objects of an abstract class can be created.
class Test
{
public:
virtual void vfun()=0;
}

Test t //invalid
Hybrid Inheritance
 Lets consider a situation where multilevel ,multiple and hierarchical
inheritance, are involved.
 All the public and protected members of ‘grand-Parent’ are inherited into
child twice via two different path.
 This means, ‘child’ would have duplicate sets of members inherited from
grand-Parent . This introduces ambiguity.
How to avoid this ambiguity
 The duplication of inherited members due to these multiple paths can be
avoided by making the common base class as virtual base class while
declaring the direct or intermediate base classes .
 When a class is made a virtual base class , C++ takes necessary care to
see that only one copy of that class is inherited .
 The keyword virtual and public may be used in either order.
Virtual Base Class Example:

class base {
public: int i;
};
class derive1 :virtual public base {
public: int j;
};
class derive2 :virtual public base {
public: int k;
};
class derive3 : public derive1, public derive2 {
public:int sum;
};
int main()
{
derive3 ob;
ob.i = 10;
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << ob.i << " " << ob.j << " " << ob.k << endl;
cout << ob.sum;
return 0;
}
Thank You

You might also like