Virtual functions
As we had seen that polymorphism refers to the properly by which objects belonging to different classes are able to respond to the same message but in different forms. An essential properly or requirements of polymorphism is therefore the ability to refer to objects of classes without any regard to which class theory belong. This allows us to use a single pointer variable to refer objects of different classes. This is a called as late binding of an object to a function and achieved by using a virtual function.
A v
+irtual 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 preceded to by the keyword ‘virtual’ The redefinition of the function in the derived class overrides the definition of the function in the base class. When accessed normally. Virtual functions behave just like any other type of member function pointer points to the derived object that contains a virtual function c++ determines which version at that function to call based upon the type of object pointed by there pointer. Thus when different objects are pointed to different versions of the virtual function are executed.
Rule for virtual functions:-
When virtual functions are created for implementing late binding we should observe some basic rules that satisfy the compiler requirements.
1. The virtual functions must be members of some class.
2. They cannot be static members.
3. Using object pointers accesses them.
4. A virtual function can be a friend of another class.
5. The prototype of the base class version of a virtual function and all the derived class members must be identified if two functions with the same name have different prototypes consider them as overloaded functions and the virtual function mechanism is ignored.
6. A virtual function in a base class must be defined even though it may be used.
7. We cannot have virtual constructors but we can have virtual destructors.
8. While a base pointer can point to any type of the derived object the reverse is not true That is to say we cannot use a pointer a derived class to access an object of the base type.
9. when a base pointer points to derived class incrementing or decrementing it will not make it to point to the next object of the derived class It is incremented or decremented only relative to its base type. Therefore we should not use this method to move the pointer to the next object.
10. If a virtual function is defined in the base class it need not be necessarily redefined in derived class. In such cases calls will invoke the base function .
Example: 1-
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n display base";
}
void show()
{
cout<<"\n show base \n ";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n display derived";
}
void show()
{
cout<<"\n show derived";
}
};
void main()
{
base b;
derived d;
base *ptr;
clrscr();
cout<<"\n ---ptr points to base---\n";
ptr= &b;
ptr->display();
ptr->show();
cout<<"\n ---ptr points to dervied---\n";
ptr= &d;
ptr->display();
ptr->show();
getch();
}
2. Program on virtual function
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<<"Base Class";
}
};
class der1:public base
{
public:
void show()
{
cout<<"\n der1 class";
}
};
class der2:public base
{
public:
void show()
{
cout<<"\n der2 class";
}
};
void main()
{
clrscr();
base *ptr;
base b;
ptr=&b;
ptr->show();
der1 d1;
der2 d2;
ptr=&d1;
ptr->show();
ptr=&d2;
ptr->show();
getch();
}
Assignment:-
1) W.A.P to declare virtual function and execute the same function defined in base and derived classes.
2) W.A.P to use pointer for both base and derived classes and call the member function .Use virtual keyword.
Pure virtual functions:-
Virtual function which does not have any body or statements can be declared as ‘pure virtual function’ we can implement it by assigning the value zero(0) to a virtual function
e.g.
Virtual void display()=0;
Here the display functions is known pure virtual because it is having value.
Example:-
1. Program on pure virtual function.
#include<iostream.h>
#include<conio.h>
class shape
{ public:
virtual void draw()=0; //pure virtual function
};
class cir:public shape
{ public: void draw()
{
cout<<"\n draw a circle"; }
};
class rect:public shape
{ public: void draw()
{
cout<<"\n draw a rectangle..";
}
};
class tri:public shape
{ public: void draw()
{
cout<<"\n draw a triangle..";
}
};
void main()
{ int opt;
clrscr();
cir c;
rect r;
tri t;
shape *s; //base class pointer
cout<<"\n1: circle\n2: rectangle\n3: triangle";
cout<<"\n enter ur choice..";
cin>>opt;
switch(opt)
{
case 1: s=&c;
s->draw();
break;
case 2: s=&r;
s->draw();
break;
case 3: s=&t;
s->draw();
break;
default:
cout<<"\ wrong choice";
}
getch();
}
Abstract classes:-
An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
The following is an example of an abstract class:
class ab
{
public:
virtual void fun()=0;
};
Example:-
class ab
{ public:
virtual void fun()=0;
};
class d2: public ab
{ void g();
};
int main()
{
d2 d;
}