close
A member function with the same name as its class is called a constructor.
A constructor defines a way to initialize an object of its class.
A constructor is similar to a function, but with the following differences.
- No return type
- No return statement
Here is a example of constructors :
class Point
{
public:
Point(); // parameterless default constructor
Point(int new_x, int new_y); // constructor with parameters
int getX();
int getY();
private:
int x;
int y;
};
Point::Point() // default constructor
{
x = 0; y = 0; }
全站熱搜