1

We cannot make the object of the abstract class ,then why a destruct is needed for an abstract class ? Is it necessary to make the destruct pure virtual for an abstract class?

class Base()  
{  
public:  
    virtual ~Base() = 0;  
}; 
Base :: ~Base()
{
}
class Derived :: public Base  
{  
public:  
    ~Derived();  
};
Ibra noor
  • 41
  • 6
  • 1
    if you have a pointer to your abstracted base class and call `delete` it will call the derived class's destructor, this is why it's needed see https://stackoverflow.com/questions/1219607/why-do-we-need-a-pure-virtual-destructor-in-c – EdChum Jan 24 '18 at 11:05
  • 3
    Possible duplicate of [Why do we need a pure virtual destructor in C++?](https://stackoverflow.com/questions/1219607/why-do-we-need-a-pure-virtual-destructor-in-c) – EdChum Jan 24 '18 at 11:06
  • 1
    If you create a `Derived` you also create a `Base`. Thus `Base` needs a constructor and destructor like any other class. – nwp Jan 24 '18 at 11:06
  • 1
    I've edited what I thought to be a typo but then rolled it back. Plenty of wrong in the above code. It's not a valid C++ code and will not compile. – Ron Jan 24 '18 at 11:07
  • Obviously you can make an object of an abstract class. When you make a Derived object it includes a Base object, and that Base object might need destructing. – john Jan 24 '18 at 11:12

2 Answers2

4

We cannot make the object of the abstract class

This means that an abstract class cannot be instantiated by itself. It does not mean that objects of abstract class cannot be instantiated as part of a derived object, though: an instance of any derived class is also an instance of its abstract base.

This is where the destructor comes in: if you need one to free private resources allocated in the constructor of the abstract class, the only place to put the clean-up is its destructor.

Is it necessary to make the destruct pure virtual for an abstract class?

It is not necessary to mark the destructor pure virtual. You do it only if you define no other functions that could be marked pure virtual. In any event, you must provide an implementation for the destructor, even if you mark it pure virtual.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

why a destruct is needed for an abstract class ?

In order to release resources used throughout class hierarchy its required to be virtual. Also to make class an abstract (See below)

Is it necessary to make the destruct pure virtual for an abstract class?

Yes only in rare situation where you want to make class abstract and there are no other virtual function inside class.

Additonal Note on Pure Virtual Destructor

  1. You must provide the definition outside class for pure virtual destructor.
  2. Derived class are not required to provide definition for destructor.
sameer chaudhari
  • 928
  • 7
  • 14