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;
}



arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JerryCheng 的頭像
    JerryCheng

    KwCheng's blog

    JerryCheng 發表在 痞客邦 留言(0) 人氣()