类里面的变量初始化默认的两种方式
第一种
写里面
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;}
平常都是这样写,如果不传参就默认,传参就用传的值
class node{ public: node(int a = 0, int b = 0){ x = a; y = b; } private: int x, y; };