Site icon Learn C++

Learn About Pure Virtual Destructors In C++ App Classes

If you construct an object, sometimes you need operations to deconstruct. Destructors not only used in classes but also used with struct and union data types. Do you want to learn what a Pure Virtual Destructor is in a C++ App, or what kind of methods we have that we can declare and use a pure virtual destructor? In this post, we will try to explain how to use Pure Virtual Destructor in C++ Classes with some short examples.

The Constructor in C++ is a function, a method in the class, but it is a ‘special method’ that is automatically called when an object of a class is created. We don’t need to call this function. Whenever a new object of a class is created, the Constructor allows the class to initialize member variables or allocate storage. This is why the name Constructor is given to this special method. Here is a simple constructor class example below,

[crayon-67426140c176c132860115/]

The Destructor in classes (i.e class_name) is a special member function that occurs when we delete objects, in other words it is called when the lifetime of an object ends. The purpose of the destructor is to perform operations like tidying up memory and notifying things when the object is destroyed. The object may have acquired or allocated data on memory during runtime. If so, they need to be freed too when objects are being deleted otherwise, we could ‘leak’ memory – where memory is allocated but can’t be used or deallocated since the owner object is defunct. The destructor is the function that frees the resources of the object such as that memory and related handles. When we construct an object, sometimes we need operations to deconstruct. Destructors are not only used in classes but also used with struct and union data types.

[crayon-67426140c1776444290564/]

Pure Virtual Destructor

A Virtual Destructor is usually required in a base class, and it is used by the class which has this base class. Note that, deleting a class object through a pointer to base invokes undefined behavior unless the destructor in the base class is virtual:

Syntax,

[crayon-67426140c177b198250380/]

Virtual destructor declaration example in a class,

[crayon-67426140c1785780042439/]

A Pure Virtual Destructor is defined in a base class with =0; statement, and its body is declared outside of the class. Pure Virtual Destructors are mostly used in C++ standard and it is generally used in libraries (dynamic DLL or static)although there are use cases for virtual destructors. Note that, if your class contains a pure virtual destructor, it should provide a function body for the pure virtual destructor. If you are asking why a pure virtual function requires a function body the answer is destructors are not actually overridden, rather they are always called in the reverse order of the class derivation. That means, firstly a derived class destructor will be invoked, and then the base class destructor will be called. If the definition of the pure virtual destructor is not provided, then the compiler and linker enforce the existence of a function body for pure virtual destructors.

Syntax,

[crayon-67426140c1787191021810/]

Virtual destructor declaration example in a class,

[crayon-67426140c1789399048369/]

An example to Pure Virtual Destructor in C++ is given below,

[crayon-67426140c178b034381075/]

Note that a class becomes an Abstract Class when it contains a Pure Virtual Destructor and abstract classes are used often in professional applications.

You can download a free trial of C++ Builder and try this code today.

Exit mobile version