I'm new to destructors, and the tutorials i've been following have been pretty clear up until this point. What actually happens when a destructor is called? Why do I still get values from a destroyed object?
class Box {
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) { //Constructor
cout << "Box Created" << endl;
length = l;
breadth = b;
height = h;
}
~Box() {
cout << "Box Destroyed" << endl; // Box Destructor
}
double volume() {
return length*breadth*height;
}
private:
double height;
double breadth;
double length;
};
void main() {
Box Box1(10, 15, 5); //Constructors used
Box Box2(5, 15, 20);
cout << "Box1.volume: " << Box1.volume() << endl;
cout << "Box2.volume: " << Box2.volume() << endl;
Box1.~Box(); //Destructors called
Box2.~Box();
cout << "Box1.volume after destruction: " << Box1.volume() << endl;
cout << "Box2.volume after destruction: " << Box2.volume() << endl;
}