Need of Virtual Function
Polymorphism
- Polymorphism is an ability of a single operation to react differently for various object call.
- Choosing function during compilation time is called static or early binding which includes function overloading and operator overloading.
- Choosing function during execution time is called dynamic or late binding which can be done using virtual function.
Why virtual function is needed?
The program is efficient if the appropriate member function, when the base and derived class both have same function name and prototype, could be selected while the program is running rather than at the compilation time. For this purpose, virtual function is necessary. Such process is known as run time polymorphism. Virtual function requires the use of pointers.
Pointer to Derived Class
- C++ allows a pointer in a base class to point to either a base class object or to any derived class object.
- Example:
B *bptr; //Pointer to base class
B b; //Base object
D d; //Derived object
bptr = &b; //bptr pointer points to base class object
bptr = &d; //bptr pointer points to derived class object
- Here, bptr can only access those members of D that are inherited from B but can not access the original members of D.
- For this, C++ permits the pointer declared as pointer to derived class.
Illustration of Pointer to Inheritance
#include
class base { public: int b; void show() { cout << b; } };
class derived : public base { public: int d; void show() { cout << b; cout << d; } };
int main() { base *bptr; base b; bptr = &b; bptr -> b = 100; bptr -> show();
derived d; bptr = &d; bptr -> b = 200; bptr -> show();
derived *dptr; dptr = &d; dptr -> d = 300; dptr -> show();
return 0; }
Output ----------------------- b = 100
b= 200
b= 200 d = 300
Definition of Virtual Function
- A virtual function is one which does not really exist but it appears real in some parts of a program.
- The exact member function is selected at run time.
- The keyword 'virtual' is used for defining virtual function.
- It must be defined in public.
- The syntax is:
class class_name { pivate: // ............... public: virtual return_type function_name(args) { ........ } };
Array of Pointer to Base Class
- In case of hierarchical inheritance, the pointer to base class can access different functions belonging to different derived classes using same object pointer with the help of array of pointers to base class using virtual function.
Illustration of Array of Pointers to Base Class
#include
class point { public: virtual void draw() { cout << "point"; } };
class line : public point { public: void draw() { cout << "Line"; } };
class circle : public point { public: void draw() { cout << "Circle"; } };
int main() { point pt; line ln; circle cr;
point *bptr[] = {&pt, &ln, &cr};
for (int i = 0; i < 3; i++) { bptr[i] -> draw(); } return 0; }
Output ----------------------- point line circle
Pure Virtual Function and Abstract Class
- A virtual function with null body is called pure virtual function.
- It only has function declaration.
- The base class which has at least one pure virtual function is called abstract class or abstract base class.
- Abstract class do not have any object.
- The syntax is:
class class_name { //........... public: virtual return_type function_name() = 0; };
Illustration of Pure Virtual Function
#include
class rectangle { protected: float l, b; public: rectangle() { l = 0; b = 0; } virtual float perimeter() { return (2*(l+b)); } virtual float area() = 0; };
class square : public rectangle { public: square() {
} square(float l, float b) : rectangle(l,b) float area() { return l*b; } };
int main() { rectangle *bptr; square sq(2,2); bptr = &sq; cout << "Perimeter = " << bptr -> perimeter(); cout << "Area = " << bptr -> area(); return 0; }
Virtual Destructor
- Virtual constructor can not be created.
- Only virtual destructor can be created.
- Virtual destructor is a member function which is used to free the memory space effectively under late binding method.
- When base class destructor is made virtual, both base class and derived class destructor are called.
- Example:
class base { public: base() {} virtual ~base() { delete [] ptrbase; cout << "Base class destructor"; } };
class derived
{
//...........
};
void main()
{
base *bptr = new derived;
delete bptr;
}
reinterpret_cast Operator
- It is one of the cast operators that handles unrelated type conversion.
- The syntax is:
reinterpret_cast (expression);
- Example:
int ivar; double dvar; int *pivar; double *pdvar; void *pvoid; pivar = reinterpret_cast (dvar) pdvar = reinterpret_cast (ivar)
Run Time Type Information
- It is the ability of finding object's type and changing their type at run time.
- It is provided by the use of dynamic_cast and typeid.
dynamic_cast operator:
- It performs run time cast along with verifying validity of the cast.
- If cast is invalid, then the cast fails.
- Syntax : dynamic_cast (expression);
- It is used to cast from base class pointer to derived class pointer.
- Example:
animal *bptr, an; cow *cptr, cw; bptr = &cw; cptr = dynamic_cast (bptr);
typeid operator:
- It returns reference to a standard library class type type_info defined in .
- The header file should be included.
- The syntax is:
typeid(expr);
Or,
typeid(type_name);
- by SURAJ AWAL
Ⓒ Copyright ESign Technology 2019. A Product of ESign Technology. All Rights Reserved.