- #include <iostream.h>
- class cB
- {
- public:
- cB(int v=7)
- { B_value=v;
- cout<<"construct cB. B_value="<<B_value<<endl;
- }
- ~cB()
- { cout<<"destruct cB. B_value="<<B_value<<endl;
- }
- protected:
- int B_value;
- };
- class cA
- {
- protected:
- int a;
- cB b;
- public:
- cA(int v1,int v2=3):b(v2) //使用冒号语句调用类成员的"有参"构造函数
- { a=v1;
- cout<<"construct cA. a="<<a<<endl;
- }
- ~cA()
- { cout<<"destruct cA."<<endl;
- }
- };
- void main()
- { cA a1(5,10);
- cA a2(5);
- cB b1(18);
- }