A constructor is a method that gets called immediately when an object is allocated (on the stack or the heap).
It is the constructor’s job to initialize the object’s attributes to sensible initial values.
A constructor may have parameters that can inform the initial values. A constructor has the same name as the class.
You can have more than one constructor.
A constructor has no return type and no return statement.
C++ gives every class a default (implicit) constructor that takes no arguments, and does nothing.
A destructor is a method that gets called immediately when an object is de-allocated.
It is the destructor’s job tidy up. It may need to deallocate memory on the heap, or close a file.
A destructor may not have parameters.
A destructor has the same name as the class, preceded with a “∼”. You can have only one constructor.
A constructor has no return type and no return statement.
It is the constructor’s job to initialize the object’s attributes to sensible initial values.
A constructor may have parameters that can inform the initial values. A constructor has the same name as the class.
You can have more than one constructor.
A constructor has no return type and no return statement.
C++ gives every class a default (implicit) constructor that takes no arguments, and does nothing.
A destructor is a method that gets called immediately when an object is de-allocated.
It is the destructor’s job tidy up. It may need to deallocate memory on the heap, or close a file.
A destructor may not have parameters.
A destructor has the same name as the class, preceded with a “∼”. You can have only one constructor.
A constructor has no return type and no return statement.
C++ gives every class a default (implicit) destructor that does nothing.
class Point
{
// sample class private:
float x; // stores the x coordinate
float y; // stores the y coordinate
public:
Point(); //the constructor
void setX(float newX);
void setY(float newY);
float getX();
float getY();
~Point(); //the destructor
};
Point::Point()
{
x = 0;
y = 0;
}
Point::~Point()
{
//do nothing
}