简体   繁体   中英

Missing Derived class Destructor when object is created with the base class pointer

In the following Code sample, the derived class destructors are not being called. Any idea why? I have a base class which has virtual functions. Now I am using Base class pointer to create new objects of the derived class. My understanding was when the derived class objects are destroyed, the destructor of the derived class gets called first and then base class. However I see only destructor of the base class being called. Does any one know what is it I am doing wrong or where my understanding of c++ is incorrect?

#include <iostream>
#include <bitset>
using namespace std; 

class DomesticAnimals 
{
    public:
        DomesticAnimals() {
            cout <<"Domestic Animal Base" <<endl;
        }; 
        ~DomesticAnimals() {
            cout <<"Domestic Animal kill " <<endl;
        }; 
        virtual void Speak() = 0;
        virtual void EatFood()= 0; 
        virtual int NoOfLegs() {
            return 4; 
        } ;
};


class Cat : public DomesticAnimals
{
    public:
        Cat(); 
        ~Cat(); 
        void Speak() override; 
        void EatFood() override; 
};

Cat::Cat()
{
    cout << "Kat was born" << endl;
}

Cat:: ~Cat()
{
    cout << "Kill de Cat" << endl; 
}
void Cat:: Speak()
{
    cout << "Meow Meow " << endl; 
}

void Cat::EatFood()
{
    cout <<"Kat eet de muis vandaag !! " <<endl; 
}


class Dog : public DomesticAnimals
{
    public:
        Dog();
        ~Dog();
        void Speak() override; 
        void EatFood() override; 
};

Dog::Dog()
{
    cout << "A puppy was born" << endl; 
}

Dog::~Dog()
{
    cout << "Kill de hond" << endl; 
}

void Dog :: Speak()
{
    cout <<"bow bow woof woof" <<endl;
}

void Dog :: EatFood()
{
    cout << "de hond eet een kip voor middageten" <<endl;
}

void DogActions()
{
    DomesticAnimals* dog = new Dog;
    cout<< endl;
    dog->Speak(); 
    dog->EatFood();
    cout<<"No of legs for dog = "<< dog->NoOfLegs() <<endl; 
    cout<<endl;
    delete dog; 
    dog = NULL;
}

void CatActions()
{
    DomesticAnimals* cat = new Cat; 
    cat->Speak(); 
    cat->EatFood(); 
    cout<<"No of legs for cat = "<< cat->NoOfLegs() << endl;
    delete cat; 
    cat = NULL; 
}

int main(void)
{
    DogActions();
    CatActions();
    return 0;
}

The output of the program is as follows

Domestic Animal Base
A puppy was born

bow bow woof woof
de hond eet een kip voor middageten
No of legs for dog = 4

Domestic Animal kill 
Domestic Animal Base
Kat was born
Meow Meow 
Kat eet de muis vandaag !! 
No of legs for cat = 4
Domestic Animal kill 

The destructor of the base class needs to be virtual:

virtual ~DomesticAnimals() {
    cout << "Domestic Animal kill" << endl;
};

To avoid any confusion (see comments): Making the destructor virtual is only necessary in the case where the object is deleted via a pointer to its base class. This is usually the case when polymorphism is desired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM