类里面的变量初始化默认的两种方式

第一种

写里面

class Rectangle{
     public:
     Rectangle(int a = 10, int b = 10):length(a),width(b),area(a*b){}
     private:
     double length, width, area;
};

写外面

class Rectangle{
    public:
    Rectangle(int a = 10, int b = 10);
    private:
    double length, width, area;
};
Rectangle::Rectangle(int a, int b):length(a),width(b),area(a*b){}

第二种

写外边

class Rectangle{
    public:
    Rectangle();
    private:
    double length, width, area;
};
Rectangle::Rectangle(){length = 5, width = 5, area = 5;}

猜你喜欢

转载自www.cnblogs.com/philo-zhou/p/12510942.html