#include <iostream> using namespace std; class demo { 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; } private: int x; }; class rectangle { public: rectangle() { x = 1000; cout << "rectangle的默认构造函数!" << x << endl; } rectangle(int i, int j, int k) : x(i), width(j), length(k) {//调用过程按照成员变量的先后顺序 cout << "rectangle的带三个参数的构造函数!" << "长方形b的面积为:" << length.get() * width.get() << endl; } ~rectangle() { cout << "rectangle的默认析构函数!" << x << endl; } int area() { return length.get() * width.get(); } private: demo length;//成员变量列表包含其他类对象叫做包容 demo width; int x; }; int main() { rectangle a(100,200,300); return 0; }