Skip to content

Instantly share code, notes, and snippets.

@raarce
Created August 17, 2012 20:17
Show Gist options
  • Save raarce/3382276 to your computer and use it in GitHub Desktop.
Save raarce/3382276 to your computer and use it in GitHub Desktop.
Destructor Demo
// 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;
}
@raarce
Copy link
Author

raarce commented Aug 17, 2012

Noten el orden en que se ejecutan los constructores y destructores.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment