问答题

请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有类CPolygon(“多边形”)、CRectangle(“矩形”)、CTriangle(“三角形”)的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序的正确输出结果应为: 20 10 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 #include<iostream> using namespace std; class CPolygon{ public: //********found******** ______//纯虚函数area声明 void printarea(void) //********found******** {cout<<______<<endl;} }; class CRectangle: public CPolygon{ int width; //长方形宽 int height; //长方形高 public: CRectangle(int w, int h): width(w), height(h){} int area(void) {return(width*height); } }; class CTriangle: public CPolygon{ int length; //三角形一边长 int height; //该边上的高 public: CTriangle(int 1, int h): length(1), height(h){} //********found******** int area(void){return(______)/2;) }; int main(){ CRectangle rect(4, 5); CTriangle trgl(4, 5); //********found******** ______*ppoly1, *ppoly2; ppoly1=▭ ppoly2=&trgl; ppoly1->printarea(); ppoly2->printarea(); return 0; }

【参考答案】

(1)virtual int area(void)=0; (2)area() (3)length*height (4)C......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个“ ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:Name: sonny Type: dogName: John Type: dogName: Danny Type: eatName: John Type: dog注意:只修改每个“ ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;enum Pets_type{dog, cat, bird, fish};class Pets{private:char*name;Pets_type type;public:Pets(const char*name= sonny , Pets type type=dog);Pets& operator=(const Pets &s);~Pets();void show()const;};Pets::Pets(const char*name, Pets_type type) 构造函数{this->name=new char[strlen (name)+1];strcpy(this->name, name); ERROR********found********type=type;}Pets::~Pets() 析构函数,释放name所指向的字符串{ ERROR*********found*********name=’ 0’;}Pets& Pets::operator=(const Pets &s){if(&s==this) 确保不要向自身赋值return*this;delete[]name;name=new char[strlen(s.name)+1]; ERROR********found********strcpy(this->name, name);type=s.type;return*this;}void Pets::show()const{cout<< Name: <<name<< Type: ;switch(type){case dog: cout<< dog ; break;case cat: cout<< cat ; break;case bird: cout<< bird ; break;case fish: cout<< fish ; break;}cout<<endl;}int main(){Pets mypet1, mypet2( John , dog);Pets youpet( Danny , cat);mypet1.show();mypet2.show();youpet.show();youpet=mypet2;youpet.show();return 0;}
问答题