64,636
社区成员
发帖
与我相关
我的任务
分享
#include "iostream"
using namespace std;
class A
{
public:
A(){a=0;cout<<"A's default constructor called.\n";}
A(int i){a=1;cout<<"A's constructor called.\n";}
~A(){cout<<"A's destructor called.\n";}
void print() const{cout<<a<<",";}
int geta(){return a;}
private:
int a;
};
class B:public A
{
public:
B(){b=0,cout<<"B's default constructor called.\n";}
B(int i,int j,int k);
~B(){cout<<"B's destructor called.\n";}
void print();
private:
int b;
A aa;
};
B::B(int i,int j,int k):A(i),aa(k)
{
b=j;
cout<<"B's constructor called.\n";
}
void B::print()
{
A::print();
cout<<b<<","<<aa.geta()<<endl;
}
void main()
{
B bb1;
B bb2(1,2,3);
bb1.print();
bb2.print();
}