求助 解释下 运行结果
#include<iostream.h>class A
{
public:
A(){cout<<"类A的构造函数"<<endl;}
~A(){cout<<"类A的析构函数"<<endl;}
};
class B:public A
{
public:
B(int x=0):b(x){cout<<"类B的构造函数"<<endl;}
~B(){cout<<"类B的析构函数"<<endl;}
int get(){return b;}
private:
int b;
};
class C:public A
{
public:
C(int y=0):c(y){cout<<"类C的构造函数"<<endl;}
~C(){cout<<"类C的析构函数"<<endl;}
int get(){return c;}
private:
int c;
};
class D:public B,public C
{
public:
D(int x=0,int y=0):B(x),C(y){cout<<"类D的构造函数"<<endl;}
~D(){cout<<"类D的析构函数"<<endl;}
D add(B &p,C &q)
{
D D1;
D1.d=p.get()+q.get();
return D1;
}
void put(){cout<<d<<endl;}
private:
int d;
};
void main()
{
B B1(1);
C C1(2);
D D2(3,4);
D2=D2.add(B1,C1);
D2.put();
}