问答题
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中包含抽象类Shape的声明,以及在此基础上派生出的类Rectangle和Circle的声明,二者都有计算对象面积的函数GetArea()和计算对象周长的函数GetPerim()。程序中位于每个“//****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:TheareaoftheCircleis78.5TheperimeteroftheCimleis31.4TheareaoftheRectangleis24TheperimeteroftheRectangleis20注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>usingnamespacestd;classShape{public:Shape(){}~Shape(){}//**********found**********________floatGetArea()=0;//**********found**********________floatGetPerim()=0;};classCircle:publicShape{public:Circle(floatradius):itsRadius(radius){}~Circle(){}floatGetArea(){return3.14*itsRadius*itsRadius;}floatGetPerim(){return6.28*itsRadius,}private:floatitsRadius;};classRectangle:publicShape{public://**********found**********Rectangle(floatlen,floatwidth):________{};~Rectangle(){};virtualfloatGetArea(){returnitsLength*itsWidth;}floatGetPerim(){return2*itsLength+2*itsWidth;}virtualfloatGetLength(){returnitsLength;}virtualfloatGetWidth(){returnitsWidth;}private:floatitsWidth;floatitsLength;};intmain(){//*****'k****found**********sp=newCircle(5);cout<<"TheareaOftheCircleis"<<sp->GetArea()<<end1;cout<<"TheperimeteroftheCircleis"<<sp->GetPerim()<<end1;deletesp;sp=newRectangle(4,6);cout<<"TheareaoftheRectangleis"<<sp->GetArea()<<end1;cout<<"TheperimeteroftheRectangleis"<<sp->GetPerim()<<end1;deletesp;return0;}
【正确答案】
正确答案:(1)virtual (2)virtual (3)itsLength(len), itsWidth(width) (4)Shape* sp;
【答案解析】
解析:(1)和(2)主要考查考生对纯虚函数定义的掌握,纯虚函数前要添加关键字virtual。 (3)主要考查考生对构造函数的掌握,使用成员列表初始化。 (4)主要考查考生对指针的掌握,由下一条语句:sp = new Circle(5),可知sp为Shape型指针。
提交答案
关闭