//7.11 多继承
#include
using std::cout;
using std::endl;
class A1
{
public:
int x;
A1(int x1):x(x1)
{
cout<<"A1的构造函数被执行"< }
~A1()
{
cout<<"A1的析构函数被执行"< }
void f()const
{
cout<<"A1::f()被执行"< cout<<"调用者地址:"< cout<<"sizeof(*this)="< cout<<"x的地址:"<<&x< }
};
class A2
{
public:
int x;
A2(int x1):x(x1)
{
cout<<"A2的构造函数被执行"< }
~A2()
{
cout<<"A2的析构函数被执行"< }
void f()const
{
cout<<"A2::f()被执行"< cout<<"调用者地址:"< cout<<"sizeof(*this)="< cout<<"x的地址:"<<&x< }
};
class B:public A1,public A2
{
public:
int x;
B(int x1,int x2,int x3):A2(x2),A1(x1),x(x3)
{
cout<<"B的构造函数被执行"< }
~B()
{
cout<<"B的析构函数被执行"< }
void f()const
{
cout<<"B::f()被执行"< cout<<"调用者地址:"< cout<<"sizeof(*this)="< cout<<"x的地址:"<<&x< }
};
int main()
{
B b(1,2,3);
cout<<"b.A1::x="< cout<<"b.A2::x="< cout<<"b.x="< cout<<"-------------"<
b.A1::f();
b.A2::f();
b.f();
cout<<"&b="<<&b< cout<<"sizeof(b)="< cout<<"-------------"<
return 0;
}
阅读(939) | 评论(0) | 转发(1) |