|
|
C++ provides two types of polymorphism, runtime polymorphism and compile time polymorphism.. Compile time polymorphism can be achieved using function overloading and operator overloading, while runtime polymorphism can be achieved using virtual functions.
In C++ Runtime polymorphism is achieved using Virtual functions. A function preceded by virtual keyword makes that function virtual. Class Myclass { Public: Virtual void myVirFunc(){} };
A pure virtual function is virtual function without function body and equated to zero. Declared as follows Virtual void MyPureFunction() =0; The use of pure virtual function is to define an interface that must be implemented and used by inherited classes.
When we declare any pure virtual function inside any class, we can not create objects of that class. But we can create the pointers to such classes. Class MyClass { Public: Virtual void myFunc()=0; }; MyClass myObj;// Does not allowed.This will give an error. MyClass *ptr= NULL; // Can be used.
When any function is declared as virtual inside any class, A Single virtual table is created for that whole class. Virtual table basically holds entries for the virtual functions. This entry is consists of the address of the virtual function.
Every object created from any class that has a virtual function declared has a by default 4 byte pointer called as VPTR. This VPTR is basically a pointer to Virtual Table. Using when virtual mechanism is used it uses this VPTR to access the proper virtual function. VPTR is created in the constructor of the class itself.
Polymorphism means same entity behaving differently or having different forms. In C++ polymorphism is distinguished based on the function call binding. If at the compile time a function call gets resolved I,e, the place where call is made is aware of the address of the function body which Is being called then it is called as static binding or compile time polymorphism. Same function body will be executed all the time. In case of runtime polymorphism address of the function body which is being called is not known at compile time. That will be decided at runtime based on the typical contents of the up-casted pointers. So the function call is bind with the respective function body at the runtime. Thus called as runtime binding or runtime polymorphism
Essentially what this mechanism does is it allows calling derived class methods using base class up casted pointers. This is useful when your program is going to decide or make some decision at runtime to which functionality to be executed. This provides the great flexibility of conditional execution of functionality at runtime. Class Shape {// Abstract Base classdefining an interface for drawing an element Public: Virtual void Draw()=0;//Pure virtual function }; Class Line: public Shape {// Derived Line Shape class Public: Virtual void Draw(){ Cout << “LINE”; } }; Class Shape : public Circle {// Derived Circle Shape class Public: Virtual void Draw(){ Cout << “ Circle”; } }; void main() { Line line1;// Line object Circle circle1;// Circle object Shape*pShapes = NULL;// Pointer to base class pShapes= &line1;// upcasting with Line class object PShapes->Draw();// Calling derived class Line method using base class upcasted pointer PShapes = & circle1; // upcasting with Cirlce class object PShapes->Draw();// Calling derived class Circle method using base class upcasted pointer } OUTPUT LINE Circle
if a class has a virtual function a virtual table for that class is created. This one table per class in top to bottom inheritance hirarchy. This will contain an entry consisting of address of virtual function. If a class is derived from some class and if it overrides a virtual function derived in base class, the virtual table of this class will have entry for this overridden method. If this derived class is not overriding the virtual function from base class. Then Virtual table of the derived class will have entry for base class virtual function itself. When we are calling a function using base class pointer, takes the first four byte of the object which it contains. This is the VPTR of that object. Using VPTR it access the Virtual Table for that class. Form the Virtual table it picks up the address of the called method through base class pointer and executes it. |