Created
August 17, 2012 20:17
-
-
Save raarce/3382276 to your computer and use it in GitHub Desktop.
Destructor Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This program demonstrates a destructor. | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
// Class specification | |
class Demo { | |
public: | |
Demo(); // Constructor | |
Demo(string); | |
~Demo(); // Destructor | |
private: | |
string name; | |
}; | |
// Class implementation | |
Demo::Demo() { | |
cout << "Default constructor!\n"; | |
} | |
Demo::Demo(string n) { | |
name = n; | |
cout << "\tConstructor with parameter: " << name << endl; | |
} | |
Demo::~Demo() { | |
cout << "\tDestructor for: " << name << " \n"; | |
} | |
// | |
// Main function | |
// | |
Demo globalDemo("Global"); | |
void foo() { | |
Demo fooDemo("Foo"); | |
cout << "I am the body of the foo function" << endl; | |
return; | |
} | |
int main() { | |
Demo demoObject("InMain"); | |
cout << "I am the body of main function before calling foo" << endl; | |
foo(); | |
cout << "I am the body of main function after calling foo" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Noten el orden en que se ejecutan los constructores y destructores.