A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration.
An abstract class is a class in C++ which have at least one pure virtual function.
Abstract class can have normal functions and variables along with a pure virtual function.
Abstract class cannot be instantiated, but pointers and references of Abstract class type can be created.
Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.
If an Abstract Class has derived class, they must implement all pure virtual functions, or else they will become Abstract too.
We can’t create object of abstract class as we reserve a slot for a pure virtual function in Vtable, but we don’t put any address, so Vtable will remain incomplete.
Example Code
#include<iostream.h> #include<conio.h> class B { public: virtual void s() = 0; // Pure Virtual Function }; class D:public B { public: void s() { cout << "Virtual Function in Derived class\n"; } }; int main() { B *b; D dobj; b = &dobj; b->s(); getch(); }
Output
Virtual Function in Derived class
0 Comments