2.1练习(一):1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。#include "iostream.h"class CBase{public:CBase(int a) :a(a){}protected:void print(){ cout<<"a="<<a<<endl;}private:int a;};class CDerive : public CBase{public:void print(){ CBase::print(); cout<<"b="<<b<<endl;}private:int b;};void main(){CDerive d;d.print();CBase b;b.print();}问题一:以上程序有两大错误,试指出来,并改正之?2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。#include "iostream.h"class CBase{public:CBase(int a) :a(a){ cout<<"base structure"<<endl;}~CBase(){ cout<<"base destructure"<<endl;}void print(){ cout<<"a="<<a<<endl;}protected:int a;};class CDerive : public CBase{public:CDerive(int a, int b,int c) :CBase(a),b(b),c(c){ cout<<"derive structure"<<endl;}~CDerive(){ cout<<"derive destructure"<<endl;}void print(){ CBase::print(); cout<<"b.a="<<b.a<<endl; cout<<"c="<<c<<endl;}private:CBase b;int c;};void main(){CDerive d(1,2,3); -----------------------------------------------------①d.print();}问题一:以上程序的输出结果是什么,说明为什么?问题二:①处语句执行完后,d.b.a的值为多少?2.2练习(二):1.定义点CPoint类作为基类,在此基础上派生出直线CLine类和圆CCircle类,并要求基类和各派生类具有以下特点:a.CLine类含有计算直线长度和斜率的成员函数;b.CCircle类含有计算圆面积的成员函数。
1 回答
- 1 回答
- 0 关注
- 6581 浏览
添加回答
举报
0/150
提交
取消