Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism

Pure Virtual Functions and Virtual Destructors

C++ Object Oriented Programming

C++ Object Oriented Programming: Pure Virtual Functions and Virtual Destructors

Pure Virtual Functions and Virtual Destructors


1. Pure Virtual Functions

•  A pure virtual function is a virtual function which is to be implemented by derived class. The class that contains the pure virtual function is called the abstract class.

• The pure virtual functions are declared using pure specifier i.e. = 0

• Following program demonstrate the use of pure virtual function.

•  The class A is an abstract class in which the pure virtual function Display() is declared. This function is overridden by the two derived classes ‒ class B and class C.

/*********************************************************************

Program for demonstrating the pure virtual function

*********************************************************************/

#include<iostream>

using namespace std;

class A

{

    public:

     virtual void Display()=0;      // Pure Virtual Function

};

class B public A

{

   public:

     void Display()

     {

          cout<<"\n In Derived Class B"<<endl;

     }

};

class C: public A

{

   public:

     void Display()

     {

           cout<<"\n In Derived Class C"<<endl;

     }

};

int main()

{

A *p;

B b;

Cc;

p = &b;

p‒>Display();  //using pointer the method of derived class is accessed

р = &c;

p‒>Display();

return 0;

}

Output

In Derived Class B

In Derived Class C

Program Explanation :

The base class A derived two classes namely ‒ B and C. In class A, the function Display is defined by assigning = 0.

The Display function is overridden in class B and class C.

In main function, the pointer object to the base class is declared. It is denoted by variable p. By assigning the address of class B to the pointer variable p, the function Display of class B is invoked. Similarly by assigning the address of class C the Display function of class C is invoked.


2. Virtual Destructor

•  Destructor is basically used to de‒allocate the memory allocated for the objects. Thus use of destructor is to clean up the memory allocated for the class members. The destructor is denoted using ~ symbol.

•  When a class is derived from a base class then on calling the destructor, it does not destruct the memory of derived class. This problem can be fixed up by making the base class destructor virtual.

•  Following program illustrates this idea

#include<iostream>

 using namespace std;

class Base

{

public:

Base()

{

   cout<<"\n Calling Base class Constructor";

}

~Base()

{

   cout<<"\n Calling Base class Destructor";

}

};

class Derived:public Base

{

public:

Derived()

{

   cout<<"\n Calling Derived class Constructor";

}

~Derived()

{

   cout<<"\n Calling Derived class Destructor";

}

};

int main()

{

   Base *obj=new Derived(); //object creation

   delete obj;

   return 0;

}

Output

Calling Base class Constructor

Calling Derived class Constructor

Calling Base class Destructor

Now change the above program as

#include<iostream>

using namespace std;

class Base                                    

{

public:

Base()

{

     cout<<"\n Calling Base class Constructor";

}

virtual ~Base()           //Note that this is virtual constructor

{

     cout<<"\n Calling Base class Destructor";

}

};

class Derived:public Base

{

     Public:

     Derived()

     {

          cout<<"\n Calling Derived class Constructor";

     }

     ~Derived()

     {

          cout<<"\n Calling Derived class Destructor";

     }

};

int main()

{

     Base *obj=new Derived(); //object creation

     delete obj;

     return 0;

}

Output

Calling Base class Constructor

Calling Derived class Constructor

Calling Derived class Destructor

Calling Base class Destructor

 

Review Question

1. Explain virtual destructor with example.

 

Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Pure Virtual Functions and Virtual Destructors


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation