Sunday, September 11, 2011

Constructor and Destructor

example:

Constructor and Destructor

#include <iostream>

using namespace std;

class MyClass{
public:
MyClass(string mName);
~MyClass();
private:
string myName;
};

MyClass::MyClass(string mName){
myName = mName;
cout << "MyClass Constructor: " << myName << endl;
}

MyClass::~MyClass(){
cout << "MyClass Destructor: " << myName << endl;
}

int main()
{
cout << "Hello world!" << endl;
MyClass myClass("MyClass 1");
MyClass* myClassPtr = new MyClass("MyClass 2: created using new");
delete myClassPtr;
return 0;
}



No comments:

Post a Comment