DYNAMIC POLYMORPHISM
INTRODUCTION:
Dynamic polymorphism includes dynamic or late binding. Virtual, Pure virtual functions and abstract classes are also discussed here. The association of an object to its corresponding class at compile time, is called static binding. Dynamic or Late binding means, an object can be created and dynamically during runtime based on certain conditions.
Dynamic polymorphism is nothing but late or dynamic binding. Late binding is implemented using virtual functions and base class pointers.
VIRTUAL FUNCTIONS:
To enable late binding, a function is declared as virtual. A virtual function is a function that is declared as virtual in a base class and redefined in the derived class. To declare a function as virtual, its declaration is predicted by the keyword virtual. The redefinition of the function in the derived class overrides the definition of the function in the base class. A base class pointer can be used to point to any class, derived object that contains a virtual function. C++ determines which version of that function to call based upon the type of object pointed to by the pointer. Thus, when different objects are pointed to, different versions of the virtual function are executed.
// Ex-33: VIRTUAL FUNCTION
#include<conio.h>
class Base
{
public:
virtual void display()
{
cout<<"\n\t Base class virtual function"<<endl;
}
};
class Derived1: public Base
{
public:
void display()
{
cout<<"\n\t Derived1 class display function"<<endl;
}
};
class Derived2: public Base
{
public:
void display()
{
cout<<"\n\t Derived2 class display function"<<endl;
}
};
void main()
{
clrscr();
Base *b;
b=new Base;
b->display();
b=new Derived1;
b->display();
b=new Derived2;
b->display();
getch();
}
PURE VIRTUAL FUNCTIONS:
When a derived class does not redefine a virtual function, the version defined in the base class will be executed. In situations like this, a base class may not be able to define an object sufficiently to allow a base class virtual function to be created and in some other situations like all derived classes override a virtual function. To handle these two cases, C++ introduces pure virtual functions. The declaration of the pure virtual function has assignment to zero notation to indicate that there is no definition for the function. The prototype for a pure virtual function is
virtual return_type function_name(parameter_list) = 0;
When a function is declared as a pure virtual function, all derived class must override the function. If the derived class fails to override the pure virtual function, an error is encountered.
// Ex-34: PURE VIRTUAL FUNCTION
#include<conio.h>
class number
{
protected:
int value;
public:
virtual void show()=0; // Pure virtual function
void setvalue(int v)
{ value = v; }
};
class hexa : public number
{
public:
void show()
{
cout<<"The given number is : "<<value<<endl;
cout<<"The hexa number is : "<<hex<<value<<endl<<endl;
}
};
class octal : public number
{
public:
void show()
{
cout<<"The octal number for 150 is "<<oct<<value<<endl;
}
};
void main()
{
octal o;
hexa h;
h.setvalue(100);
clrscr();
h.show();
o.setvalue(150);
o.show();
getch();
}
ABSTRACT CLASSES:
A class that contains at least one pure virtual function (function declared, but not defined) is said to be an abstract class. No object of an abstract class can be created. Instead, an abstract class constitutes an incomplete type that is used as a foundation for derived class.