1 of 11

Pure Virtual Function

2 of 11

Pure Virtual Function`

  • Usually, a function declared inside the base class and redefined in the derived class.

  • Not defined any object of class and function in the base class have been defined “empty”.

  • Such functions are called “do-nothing” functions.

3 of 11

  • Such functions are defined as:

Virtual void display()=0;

  • Such functions are called Pure virtual functions.
  • A class containing pure virtual function can not be used to declare any object of its own.
  • Such classes are called Abstract base classes.
  • The main objective of base class is to create base class ptr required for achieving run-time polymorphism.

4 of 11

#include<iostream>

using namespace std;

class Base

{

int x;

public:

virtual void fun() = 0;

int getX() { return x; }

};

class Derived: public Base

{

int y;

public:

void fun()

{

cout << "fun() called"; }

};

int main(void)

{

Derived d;

d.fun();

return 0;

}

5 of 11

class B

{    

public:    

  virtual void s() = 0;

};

class D:public B

{  

 public:    

void s()

{        

 cout << "Virtual Function in Derived class\n";      

}

};

int main()

{    

B *b;  

 D dobj;    

b = &dobj;  

 b->s();

}

6 of 11

  • class A
  • {
  • Int a;
  • public:
  • A()
  • {
  • a=1;�}
  • virtual void show()
  • {
  • cout<<a;
  • }
  • };
  • class B:public A
  • {
  • int b;
  • public:
  • B()
  • {
  • b=2;
  • }
  • virtual void show()
  • {
  • cout<<b;
  • }
  • };
  • int main()
  • {
  • A *ptr;
  • B ob;
  • ptr= &ob;
  • ptr->show();
  • return 0;
  • }

7 of 11

Virtual Constructor/Virtual Destructor

  • The virtual mechanism works only when we have a base class pointer to a derived class object.
  • Destructors in the Base class can be Virtual.
  • Destructors of the Base class must be made virtual for proper destruction of the object when the program exits.
  • NOTE: Constructors are never Virtual, only Destructors can be Virtual.

  • New & delete operators/keyword
  • initialize/destroying objects

8 of 11

  • class b
  • {
  • public:
  • b()
  • { cout<<"Constructing base \n";
  • }
  • virtual ~b()
  • { cout<<"Destructing base \n";
  • }
  • };
  • class d: public b
  • {
  • public:
  • d()
  • { cout<<"Constructing derived \n";
  • }
  • ~d()
  • { cout<<"Destructing derived \n";
  • }
  • };
  • int main()
  • {
  • b *ptr=new d();
  • delete ptr;
  • return 0;
  • }

9 of 11

  • Constructing base
  • Constructing derived
  • Destructing derived
  • Destructing base

  • (Without virtual)

  • Constructing base
  • Constructing derived
  • Destructing base

10 of 11

  • class b
  • {
  • public:
  • b()
  • { cout<<"Constructing base \n";
  • }
  • ~b()
  • {
  • cout<<"Destructing base \n";
  • }
  • };
  • class d: public b
  • {
  • public:
  • d()
  • {
  • cout<<"Constructing derived \n";
  • }
  • ~d()
  • {
  • cout<<"Destructing derived \n";
  • }
  • };
  • int main()
  • {
  • b *ptr1= new b();
  • b *ptr2=new d();
  • delete ptr1;
  • delete ptr2;
  • return 0;
  • }

11 of 11

  • Constructing base
  • Constructing base
  • Constructing derived

  • Destructing base
  • Destructing derived
  • Destructing base
  • (Without virtual)
  • Constructing base
  • Constructing base
  • Constructing derived
  • Destructing base
  • Destructing base