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

3.6 Runtime Polymorphism

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

3.6 Runtime Polymorphism

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Runtime Polymorphism

Function Overriding

#include <iostream> int main() {


using namespace std; Dog d ;
class Animal { d.eat();
public: return 0;
void eat(){ }
cout<<"Eating...";
}
};
class Dog: public Animal
Output:
{
public: Eating bread...
void eat()
{
cout<<"Eating bread...";
}
};
Virtual Function

 It works on principle of method overriding.

 The keyword virtual is used to identify a member function as virtual.

 It support run-time polymorphism with the help of base class pointer.


 The call to a function resolved at run time termed as run-time

polymorphism.
 The “virtual”-ity of the member function continues along the inheritance

chain.
 A class that contains a virtual function is referred to as a polymorphic class.
#include <iostream> void main() {
using namespace std; base b1;
class base b1.show(); // base - (s.b.)
{
public: derived d1;
virtual void show() { d1.show(); // derived – (s.b.)
cout << “base\n”;
} base *pb = &b1;
}; pb->show(); // base - (d.b.)
class derived : public base
{ pb = &d1;
public: pb->show(); // derived (d.b.)
void show() { }
cout << “derived\n”; /* Here,
} s.b. = static binding
}; d.b. = dynamic binding */
#include <iostream> class d2 : public base
using namespace std; {
class base public:
{ void show() {
public: cout << “derived-2\n”;
virtual void show() }
{ };
cout << “base\n”;
} void main()
}; {
base *pb;
class d1 : public base d1 od1;
{ d2 od2;
public: pb = &od1;
void show() pb->show();
{
cout << “derived-1\n”; pb = &od2;
} pb->show();
}; return 0;
}
Virtual Destructors

 Constructors cannot be virtual, but destructors can be virtual.

 It ensures that the derived class destructor is called when a base class

pointer is used while deleting a dynamically created derived class object.


Virtual Destructors
class base void main()
{ {
public: base *p = new derived();
virtual ~base() delete p;
{ }
cout << “destructing base\n”;
} Output:
}; destructing derived
class derived : public base destructing base
{
public:
~derived()
{
cout << “destructing derived\n”;
}
};
Pure virtual function
 If we want to omit the body of a virtual function in a base class, we can use

pure virtual functions.

virtual ret-type func-name(param-list) = 0;


class Base //Abstract base class
{ int main()
public: {
virtual void show() = 0; //Base obj; //Compile Time Error
}; Base *b;

class Derived : public Base Derived d;


{ b = &d;
public: b->show();
void show()
{ return 0;
cout << "Implementation of Virtual }
Function in Derived class";
}
};
 It makes a class an abstract class.

 We cannot create any objects of such classes.

 It forces derived classes to override it.

 Otherwise they become abstract too.

 Pure virtual function

 Helps to guarantee that a derived class will provide its own redefinition.
 We can still create a pointer to an abstract class

 Because it is at the heart of run-time polymorphism

 When a virtual function is inherited, so is its virtual nature.

 We can continue to override virtual functions along the inheritance

hierarchy.
 Run-time polymorphism is not automatically activated in C++.

 We have to use virtual functions and base class pointers to enforce and

activate run-time polymorphism in C++.

 Note:-

 No such keyword called an abstract in c++ so no abstract class can be

written unlike virtual void fun();


 But can be implemented as abstract class by using pure virtual function().
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer
Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune

You might also like