问答题

请使用VC6或使用[答题]菜单打开考生文件夹projl下的工程proj1,其中有枚举DOGCOLOR、狗类Dog和主函数man的定义。程序中位于每个“//ERROR ****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
There is a white dog named Hoho.
There is a black dog named Haha.
There is a motley dog named Hihi.
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
enum DOGCOLOR BLACK,WHITE,YELLOW,BROWN,PIEBALD,OTHER;
class Dog //狗类
DOGCOLOR color;
char name[20];
static int count;
public:
Dog(char name[],DOGCOLOR color)
strcpy(this->name,name);
strcpy(this->color,color);

DOGCOLOR getColor () constreturncolor;
const char* getName () const return * name;
const char * getColorString ()const
switch (color)
case BLACK:return"black";
case WHITE:return"white";
case YELLOW:return"yellow";
case BROWN:return"brown";
case PIEBALD:return"piebald";

return"motley";

void show () const
cout<<"There is a "<<getColorString()<<"dog named"<<name<<’.’<<endl;

;
int main ()
//ERROR **********found**********
Dog dog1("Hoho",WHITE),dog2("Haha",BLACK);dog3("Hihi",OTHER);
dog1.show();
dog2.show();
dog3.show();
return 0;

【参考答案】

(A)this->color=color;
(B)const char getName()const {ret......

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

问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有坐标点类point、线段类Line和三角形类Triangle的定义,还有main函数的定义。程序中两点间距离的计算是按公式 实现的,三角形面积的计算是按公式 实现的,其中 。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: Side 1:9.43398 Side 2:5 Side 3:8 area:20 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“ ****found****”。 #include<iostream> #include<cmath> using namespace std; class Point 坐标点类 public: const double x,y; Point(double x=0.0,double y=0.0):x(x),y(y) **********found********** double distanceTo(______) const 到指定点的距离 return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); ; class line 线段类 public: const Point p1,p2; 线段的两个端点 **********found********** Line(Point p1,Point p2):______ double length()const return p1.distanceTo(p2); 线段的长度 ; class Triangle 三角形类 public: const Point pl, p2,p3; 三角形的三个顶点 **********found********** Triangle(______):p1(p1),p2(p2),p3(p3) double length1()const 边p1,p2的长度return Line(p1,p2).length(); double length2()const 边p2,p3的长度 return Line(p2,p3).length(); double length3()const 边p3,p1的长度 return Line(p3,p1).length(); double area() const 三角形面积 **********found********** double S=______; return sqrt(s*(s-lengthl())*(s-length2())*(s-length3())); ; int main () Triangle r(Point(0.0,8.0),Point(5.0,0.0),Point(0.0,0.0)); cout<< Side 1: <<r.length1()<<endl; cout<< Side 2: <<r.length2()<<endl; cout<< Side 3: <<r.length3()<<endl; cout<< area: <<r.area()<<endl; return 0;
问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。sort成员函数的功能是将当前数据表中的元素升序排列。请编写这个sort函数。程序的正确输出应为: 排序前:7,1,3,11,6,9,12,10,8,4,5,2 排序后:1,2,3,4,5,6,7,8,9,10,11,12 要求: 补充编制的内容写在“ ********333********”与“ ********666********”两行之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out. dat中。输出函数writeToFile已经编译为obj文件,并且在本程序调用。 DataList.h #include<iostream> using namespace std; class Datalist 数据表类 int len; double*d; public: DataList(int len,double data[]=NULL); ~DataList()delete[]d; int length()const return len; 数据表长度(即数据元素的个数) double getElement(int i)constreturn d[i]; void sort(); 数据表排序 void show () const; 显示数据表 ; void writeToFile (char*, constDataList&); main.cpp #include DataList.h DataList::DataList(int len,double data[]):len(len) d=new double[len]; forint i=0;i<len;i++) d[i]=(data==NULL0.0:data[i]); void DataList::sort() 数据表排序 ********333******** ********666******** void Datalist::show()const 显示数据表 for(int i=0;i<len-1;i++)cout<<d[i]<< , ; cout<<d[len-1]<<endl; int main () double s[]=7,1,3,11,6,9,12,10,8,4,5,2; DataList list(12,s); cout<< 排序前: ; list.show(); list.sort(); cout<<endl<< 排序后: ; list.show(); writeToFile( ,list); return 0;