Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism

Virtual Destructors

C++ Program

Question: 1. Explain virtual destructor with example C++ Program.

Virtual Destructors

• 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.

 

Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : C++ Program - Virtual Destructors


Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 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


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

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


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation