C++ examples for Class:Destructor
Adding a Destructor to a Class to do cleaning up
#include <cinttypes> #include <iostream> #include <string> using namespace std; class Car/*from ww w . j ava2 s .c o m*/ { private: string m_Name; int m_NumberOfWheels{}; public: Car() = default; Car(string name, int numberOfWheels) : m_Name{ name } , m_NumberOfWheels{ numberOfWheels } { } ~Car() { cout << m_Name << " is being destroyed!" << endl; } int GetNumberOfWheels() { return m_NumberOfWheels; } }; int main(int argc, char* argv[]) { Car myCar{ "myCar", 4 }; cout << "Number of wheels: " << myCar.GetNumberOfWheels() << endl; Car myMotorcycle{ "myMotorcycle", 2 }; cout << "Number of wheels: " << myMotorcycle.GetNumberOfWheels() << endl; Car noWheels; cout << "Number of wheels: " << noWheels.GetNumberOfWheels() << endl; return 0; }