A useful rule of thumb is that if a class needs a destructor, it will also need the assignment operator and a copy constructor. This rule is often referred to as the Rule of Three , indicating that if you need a destructor, then you need all three copycontrol members.
An important difference between the destructor and the copy constructor or assignment operator is that even if we write our own destructor, the synthesized destructor is still run .
class Sales_item {
public:
// empty; no work to do other than destroying the members,
// which happens automatically
~Sales_item() { }
// other members as before
};
When objects of type Sales_item are destroyed, this destructor, which does nothing, would be run. After it completes, the synthesized destructor would also be run to destroy the members of the class. The synthesized destructor destroys the string member by calling the string destructor, which frees the memory used to hold the isbn . The units_sold and revenue members are of built-in type, so the synthesized destructor does nothing to destroy them.