0

When executing the following code the constructor messages for both objects appear, but the destructor messages appear only for the 'hattie' object. Weren't destructors for both objects run on exit? Note that the base class destructor is protected, satisfying Sutter's rule #4.

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

class Pup {
protected:
    string name ;
    ~Pup() {cout << "bye " + name << endl;}
};

class Corgi : public Pup {
public:
    Corgi(string name) {this->name = name ;
        cout << "hello " + name << endl;}
    ~Corgi() {cout << "bye-bye " + name << endl;}
};

int main() {
    Corgi hattie("Hattie");
    Corgi* gus = new Corgi("Gus");
    return 0;
}
user3150422
  • 67
  • 1
  • 4

0 Answers0