• If we want to provide only interface for the derived class then we use the concept of abstract base class.
  • If we create abstract base class then we can not create object of that base class.
  • Writing pure virtual function means writing (=0) at the end of a prototype of a function without any defination.
  • When we write at least a single pure virtual function inside the class then that class is considered as a abstract base class.
  • A class having pure virtual function only says what to do does not say how to do.
  • When we inherit that abstract base class then the derived class must implement the pure virtual functions are itself became an abstract class. Means a pure virtual function forces inherited classes to provide a definition for it.
  • We can create an abstract class when only want to manipulate a set of classes through a common interface,but the common interface doesn’t need to have an implementation.
  • We can create pure virtual function using the syntax as virtual int Fun() = 0;
  • By using the above syntax we can tell the compiler that reserve the slot for this function in VTABLE, but not to put any address in that perticular slot.
  • Because of which the VTABLE is incomplete. Even if only one function in class is declared as a pure virtual the VTABLE is incomplete.
  • When we create object of abstract base class, the VTABLE is incomplete and by looking at it compiler gives an error message.
  • It’s possible to provide a definition for a pure virtual function in the base class. But still it is not allowed to create the object of abstract class. and in this case also VTABLE is also incomplete.
  • We can not provide the inline definition for the pure virtual functions means we can not provide the definition for the pure virtual function inside the class.
  • If a class is deriving an abstract class, it is compulsion that the given derived class must provide a defination of a pure virtual function which is defined in the abstract base class.
  • If derived class does not want to provide a defination of pure virtual function then writing a prototype of a function is necessary (=0 must be there at the end of the prototype).
  • If there is no such a prototype in a derived class then compiler gives an error.
  • By providing the definition for the pure virtual functions we can call that function inside the derived class functions.
  • We can also provide a partial defination for pure virtual function but still entry of VTABLE is empty.
  • If we want to provide such partial defination then that function must be defined outside the class.
  • And this partially provided defination must be called by a derived class otherwise this defination is of no use.

 Virtual Functions

Example:

class base

{

public:

virtual void demo()=0;

};

void base:: demo()

{

printf(“in pure virtual function”);

}

class derived : public base {

public:

void demo()

{

base::demo();

printf(“Inside the derived class\n”);

}

};

int main()

{

derived obj;

base *d = &obj;

d->demo();

return 0;

}

Output:

in pure virtual function.

Inside the derived class.

For more reading about technology news in singapore and seo to online marketing do view more about other pages.

Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Comment