You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Destructors are invoked to automatically free memory that has been assigned to objects.
However the destructor member function of the derived class is not invoked to free the memory storage which was allocated by the constructor of the derived class.
This is because destructor functions are non-trivial and the message does not reach the destructor member function under dynamec binding.
In such a situation it is better to have a virtual destructor function.
In the program shown below we will use a base class which does not have a virtual destructor.
You can see from the output displayed that the destructor of the derived class is not invoked.
*/
#include<iostream>
#include<cstring>
usingnamespacestd;
classSentence
{
private:
char* str;
public:
Sentence();
~Sentence();
};
classPhrase:publicSentence
{
private:
char* phr;
public:
Phrase();
~Phrase();
};
Sentence::Sentence() // Base constructor
{
str = newchar[50];
strcpy(str, "This sentence is worth considering\n");