C++面向对象程序设计实验指导书 实验五
int b; };
void main() {
CDerive d; d.print(); CBase b; b.print(); }
问题一:以上程序有两大错误,试指出来,并改正之?
2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
#include \
class CBase {
public:
CBase(int a) :a(a) { cout<<\< ~CBase() { cout<<\< void print() { cout<<\< class CDerive : public CBase { public: CDerive(int a, int b,int c) :CBase(a),b(b),c(c) { cout<<\< ~CDerive() { cout<<\< 17 C++面向对象程序设计实验指导书 实验五 } void print() { CBase::print(); cout<<\< CBase b; int c; }; void main() { CDerive d(1,2,3); -----------------------------------------------------① d.print(); } 问题一:以上程序的输出结果是什么,说明为什么? 问题二:①处语句执行完后,d.b.a的值为多少? 5.2.2 程序设计 1.定义点CPoint类作为基类,在此基础上派生出直线CLine类和圆CCircle类,并要求基类和各派生类具有以下特点: a.CLine类含有计算直线长度和斜率的成员函数; b.CCircle类含有计算圆面积的成员函数。 18 C++面向对象程序设计实验指导书 实验六 实验六 派生与继承—多重派生 6.1 实验目的 1.理解多重派生的定义; 2.理解多重派生中构造函数与析构函数的调用顺序; 3.理解多重派生中虚拟基类的作用; 6.2 实验内容 6.2.1程序阅读 1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。 class CBase1 { public: CBase1(int a) :a(a) { cout<<\< ~CBase1() { cout<<\< void print() { cout<<\< class CBase2 { public: CBase2(int b) :b(b) { cout<<\< 19 C++面向对象程序设计实验指导书 实验六 } ~CBase2() { cout<<\< void print() { cout<<\< class CDerive : public CBase1, public CBase2 { public: CDerive() { cout<<\< ~CDerive() { cout<<\< void print() { CBase1::print(); CBase2::print(); b1.print(); b2.print(); cout<<\< CBase1 b1; CBase2 b2; int c; }; void main() { CDerive d; d.print(); } 问题一:改正以上程序中存在的错误,并分析该程序的输出结果。 2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。 20 C++面向对象程序设计实验指导书 实验六 #include \ 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< 问题一:在不改变原有程序意图的前提下,分别用三种方法改正以上程序,并使程序正确输出。 21