Felanpro

Deconstructors

Mar 31st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. Deconstructors are code that runs after objects get destroyed.
  2. Objects get destroyed when the program ends!
  3.  
  4. Here is one example:
  5.  
  6. #include <iostream>
  7. #include <stdlib.h>     /* system, NULL, EXIT_FAILURE */
  8.  
  9. using namespace std;
  10.  
  11. class Room
  12. {
  13. public:
  14.     Room()
  15.     {
  16.         cout << "I am the constructor!" << endl; //I run when an object gets created!
  17.     }
  18.  
  19.     ~Room()
  20.     {
  21.         cout << "I am the deconstructor!" << endl; //I run when an object gets destroyed! //Also notice the ~ in the beginning!
  22.     }
  23. };
  24.  
  25. int main()
  26. {
  27.     Room object; //Constructor
  28.     cout << "Hello World!" << endl; //Text
  29.     system("pause"); //Pause
  30.     /*And finally here the deconstructor run!*/
  31.  
  32.     return 0;
  33. }
Add Comment
Please, Sign In to add comment