1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include<iostream> using namespace std;
class demo { private: int x; public: demo() { x = 0; cout << "demo的默认构造函数!" << x << endl; } demo(int i) { x = i; cout << "demo的带有一个参数的构造函数!" << x << endl; } ~demo() {cout << "demo的默认析构函数!" << x << endl;} int get() {return x;} void set(int i) {x = i;}
}; class rectangle { private: int x; demo length; demo width; public: rectangle() { x = 1000; cout << "rectangle的默认构造函数!" << x << endl; } rectangle(int i, int j, int k) : x(i), width(j), length(k) { cout << "rectangle的带3个参数的构造函数!" << "长方形b的面积为:" << length.get() * width.get() << endl; } ~rectangle() {cout << "rectangle的默认析构函数!" << x << endl;} int area() {return length.get() * width.get();} }; int main() { rectangle a(100, 200, 300); return 0; }
|