目录
实验六 派生与继承—多重派生
6.1 实验目的
6.2 实验内容
6.2.1程序阅读
6.2.2 程序设计
6.3思考题
1.理解多重派生的定义;
2.理解多重派生中构造函数与析构函数的调用顺序;
3.理解多重派生中虚拟基类的作用;
1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
class CBase1
{
public:
CBase1(int a)
:a(a)
{
cout<<"base1 structure..."<
}
~CBase1()
{
cout<<"base1 destructure..."<
}
void print()
{
}
protected:
int a;
};
class CBase2
{
public:
CBase2(int b)
:b(b)
{
cout<<"base2 structure..."<
}
~CBase2()
{
cout<<"base2 destructure..."<
}
void print()
{
cout<<"b="<
}
protected:
int b;
};
class CDerive : public CBase1, public CBase2
{
public:
CDerive()
{
cout<<"derive structure..."<
}
~CDerive()
{
cout<<"derive destructure..."<
}
void print()
{
CBase1::print();
CBase2::print();
b1.print();
b2.print();
cout<<"c="<
}
private:
CBase1 b1;
CBase2 b2;
int c;
};
void main()
{
CDerive d;
d.print();
}
问题一:改正以上程序中存在的错误,并分析该程序的输出结果。
答:(1)没有引用头文件,且未声明命名空间。
(2)CBase1和CBase2类缺少默认的构造函数。
分析:main函数创建CDerive类对象d时,CDerive类继承了CBase1和CBase2类,因此输出了前两行。在CDerive类中又有两个成员对象b1和b2,再次调用CBase1和CBase2的构造函数,故有3、4行。随后,CDerive构造函数中有输出第5行内容。6-10行为CDerive::print()函数中内容,由于在调用时均为赋值,因此变量内容不确定。后5行为析构,与构造过程相反。
2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
#include "iostream"
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<
}
问题一:在不改变原有程序意图的前提下,分别用三种方法改正以上程序,并使程序正确输出。
答:(1)方法一:使用虚继承
#include
using namespace std;
class CBase {
public:
CBase(int a)
:a(a)
{
}
int a;
};
class CDerive1 : virtual public CBase{
public:
CDerive1(int a)
:CBase(a)
{
}
};
class CDerive2 :virtual public CBase{
public:
CDerive2(int a)
:CBase(a)
{
}
};
class CDerive : public CDerive1,public CDerive2{
public:
CDerive(int a,int b)
:CDerive1(a),CDerive2(b),CBase(b)
{
}
};
int main(){
CDerive d(1,2);
cout<
(2)添加输出函数print()
#include
using namespace std;
class CBase {
public:
CBase(int a)
:a(a)
{
}
int a;
int print(){
return 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)
{
}
int print(){
return CDerive1::print();
}
};
int main(){
CDerive d(1,2);
cout<
(3)获取变量时限定作用域
#include
using namespace std;
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)
{
}
int getA(){
return CDerive1::a;
}
};
int main(){
CDerive d(1,2);
cout<
1.建立普通的基类building,用来存储一座楼房的层数、房间数以及它的总平方数。建立派生类house,继承building,并存储卧室与浴室的数量,另外,建立派生类office,继承building,并存储灭火器与电话的数目。设计一主函数来测试以上类的用法。
答:
#include
using namespace std;
class building{
protected:
int floor;
int room_num;
int area;
public:
building(int a,int b,int c):floor(a),room_num(b),area(c)
{
}
void buildingInfo(){
cout<<"* 楼层:"<
1.按照下图的类层次结构编写程序,定义属于score的对象c1以及类teacher的对象t1,分别输入每个数据成员的值后再显示出这些数据。
答:
#include
#include
#include
using namespace std;
class person{
protected:
string name;
int id;
public:
person(string a,int b):name(a),id(b)
{
}
void personInfo(){
cout<<"* 姓名:"<