65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class cB
{
public:
cB()
{
B_value=1;
cout <<"construct cB.B_value=" <<B_value <<endl;
}
cB(int v) //这儿不能给默认值
//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;
}
};
int main()
{
cA a1(5,10);
cA a2(5);
cB b1(18);
cB b2;
return 0;
}
#include <iostream>
using namespace std;
class cB
{
public:
cB()
{
B_value = 1;
cout << "construct cB.B_value=" << B_value << endl;
}
cB(int v)
{
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;
}
};
int main()
{
cA a1(5, 10);
cA a2(5);
cB b1(18);
cB b2;
return 0;
}