Example object myClass.
Code:
class myClass
{
private:
int intInt;
long longLong;
public:
myClass(); //this is the constructor
~myClass(); //this is the destructor
}
myClass::myClass() //constructor
{
intInt=0;
longLong=0;
}
myClass::~myClass() //destructor
{
}
The constructor is a member function of the class that has the same name of the class and can't return a value. It's called when the object is created. ie:
Code:
myClass ClassOne; //constructor is called here.
The destructor is the opposite of the constructor and has the same name as the constructor except that it starts with a ~. It's called when the object is destroyed.
Constructors usually set the initial values of the member variables (like setting a pointer to NULL) and destructors do the clean-up (like deleting the pointer and resetting it to NULL).