一、实验目的
1.理解多重派生的定义;
2.理解多重派生中构造函数与析构函数的调用顺序;
3.理解多重派生中虚拟基类的作用;
二、实验内容
2.1练习(一):
1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
class CBase1
{
public:
CBase1(int a)
:a(a)
{
cout<<"base1 structure..."<<endl;
}
~CBase1()
{
cout<<"base1 destructure..."<<endl;
}
void print()
{
cout<<"a="<<a<<endl;
}
protected:
int a;
};
class CBase2
{
public:
CBase2(int b)
:b(b)
{
cout<<"base2 structure..."<<endl;
}
~CBase2()
{
cout<<"base2 destructure..."<<endl;
}
void print()
{
cout<<"b="<<b<<endl;
}
protected:
int b;
};
class CDerive : public CBase1, public CBase2
{
public:
CDerive()
{
cout<<"derive structure..."<<endl;
}
~CDerive()
{
cout<<"derive destructure..."<<endl;
}
void print()
{
CBase1::print();
CBase2::print();
b1.print();
b2.print();
cout<<"c="<<c<<endl;
}
private:
CBase1 b1;
CBase2 b2;
int c;
};
void main()
{
CDerive d;
d.print();
}
问题一:改正以上程序中存在的错误,并分析该程序的输出结果。
2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
#include "iostream.h"
class CBase
{
public:
CBase(int a)
:a(a)
{
}
int a;
};
class CDerive1 : public CBase
{
public:
CDerive1(int a)
:CBase(a)
{
}
};
class CDerive2 : public CBase
{
public:
CDerive2(int a)
:CBase(a)
{
}
};
class CDerive : public CDerive1,public CDerive2
{
public:
CDerive(int a,int b)
:CDerive1(a),CDerive2(b)
{
}
};
void main()
{
CDerive d(1,2);
cout<<d.a<<endl;
}
问题一:在不改变原有程序意图的前提下,改正以上程序,并使程序正确输出。
惊回头
断桥丶晓风残月
奋斗的油菜
jfhdibrbfjd