Works for me with Dev-C++, but it doesn't even output "Dog Destructor Called" once or twice.
Works for me with Dev-C++, but it doesn't even output "Dog Destructor Called" once or twice.
That's true jlou, but my original point still stands. It is best to over-ride equals to ensure everything in your class gets copied over correctly.
Added this code and it works
fido.~Dog();
rover.~Dog();
So it now looks like:
Code:#include <iostream> using namespace std; class Dog { private: int age; int weight; public: Dog(); //Constructor ~Dog(); //Destructor void setAge(int age); int getAge(); void setWeight(int weight); int getWeight(); void speak(); }; Dog::Dog() { age = 0; weight = 0; cout << "Dog Constructor Called" << endl; } Dog::~Dog() { cout << "Dog Destructor Called" << endl; } void Dog::setAge(int age) { this->age = age; } int Dog::getAge() { return age; } void Dog::setWeight(int weight) { this->weight = weight; } int Dog::getWeight() { return weight; } void Dog::speak() { cout << "BARK!!" << endl; } int main() { Dog fido; Dog rover; cout << "Rover is " << rover.getAge() << " years old." << endl; cout << "He weighs " << rover.getWeight() << " lbs." << endl; cout << endl; cout << "Updating Rover's Age and Weight" << endl; rover.setAge(1); rover.setWeight(10); cout << "Rover is " << rover.getAge() << " years old." << endl; cout << "He weighs " << rover.getWeight() << " lbs." << endl; cout << endl; cout << "Fido is " << fido.getAge() << " years old." << endl; cout << "He weighs " << fido.getWeight() << " lbs." << endl; cout << "Setting Fido to be the same as Rover" << endl; fido = rover; cout << "Fido is " << fido.getAge() << " years old." << endl; cout << "He weighs " << fido.getWeight() << " lbs." << endl; rover.speak(); fido.speak(); fido.~Dog(); rover.~Dog(); cin.get(); return 0; }
Since the destructors are called at the very end of your program, it is possible you just aren't seeing the output. Try putting the objects inside a smaller scope. The destructors are called as soon as the scope ends. In the original code, that is the end of the main function. In the code below, the extra scope (created with the extra braces {,}) allows the destructors to be called before main ends.Code:#include <iostream> using namespace std; class Dog { private: int age; int weight; public: Dog(); //Constructor ~Dog(); //Destructor void setAge(int age); int getAge(); void setWeight(int weight); int getWeight(); void speak(); }; Dog::Dog() { age = 0; weight = 0; cout << "Dog Constructor Called" << endl; } Dog::~Dog() { cout << "Dog Destructor Called" << endl; } void Dog::setAge(int age) { this->age = age; } int Dog::getAge() { return age; } void Dog::setWeight(int weight) { this->weight = weight; } int Dog::getWeight() { return weight; } void Dog::speak() { cout << "BARK!!" << endl; } int main() { { Dog fido; Dog rover; cout << "Rover is " << rover.getAge() << " years old." << endl; cout << "He weighs " << rover.getWeight() << " lbs." << endl; cout << endl; cout << "Updating Rover's Age and Weight" << endl; rover.setAge(1); rover.setWeight(10); cout << "Rover is " << rover.getAge() << " years old." << endl; cout << "He weighs " << rover.getWeight() << " lbs." << endl; cout << endl; cout << "Fido is " << fido.getAge() << " years old." << endl; cout << "He weighs " << fido.getWeight() << " lbs." << endl; cout << "Setting Fido to be the same as Rover" << endl; fido = rover; cout << "Fido is " << fido.getAge() << " years old." << endl; cout << "He weighs " << fido.getWeight() << " lbs." << endl; rover.speak(); fido.speak(); } cout << "Destructors should have already been called!" << endl; return 0; }
Do you have the latest version of Dev-C++? Dev-C++ uses gcc for its compiler, which I've always considered one of the best.
Yeah, although I would only recommend overriding it if the default ones don't do what you want.Originally Posted by bithub
Bad idea - don't call the destructors explicitly unless you are using special memory management and you know what you are doing. In this case, the destructors will be called twice, which is bad.Originally Posted by stickman
Jlou makes a good point. The destructors may get called after the console window disappears.
How can that be? As soon as it close, the program stops, so wouldn't it be impossible for it to destruct after it closes?Originally Posted by swoopy
More like just before the window closes, and then the window closes and you don't see it.Originally Posted by stickman
Oh, ok.Originally Posted by jlou
>More like just before the window closes, and then the window closes and you don't see it.
Well, if that's the case, then Stickman, can you open up a console from the Start menu, and just run the exe that way? That would tell us for sure.
The destrctor messages should show up if you do this.
The destructor message didn't show up.
And what about in my modified version with the extra scope?
Yeah, it showed up.
Well I'm baffled. But Stickman, you also ran Jlou's program above with the extra scope, and they do show up?
EDIT: question answered