问答题

请使用“答题”菜单或使用VC6打开考生文件夹proj1下的工程proj1。此工程定义了Stop-Watch(秒表)类,用于表示时、分、秒信息,有构造函数StopWatch()、设置时间函数reset()、并且重载了前置和后置++运算符,用于实现增加秒的功能。程序中位于每个//ERROR************found************下的语句行有错误,请加以改正。改正后程序的输出应该是:
00:00:00
00:01:00
注意:只能修改每个//ERROR************found************下的那一行,不要改动程序中的其他内容。
//源程序
#include<iostream>
#include<iomanip>
using namespace std;
class StopWatch //“秒表”类
int hours,minutes,seconds; //小时、分钟、秒
public:
StopWatch():hours(0),minutes (0),seconds(0)
void reset()hours=minutes=seconds=0;
StopWatch operator++(int) //后置++
StopWatch old=*this;
++(*this);
return old;

//前进1秒
StopWatch& operator++() //前置++
//ERROR************found************
if(seconds++==60)
seconds=0;minutes++;
if(minutes==60)
minutes=0;hours++;


//ERROR************found************
return this:

friend void show(StopWatch);

void show(StopWatch watch)
cout<<setfill(’0’);
cout<<setw(2)<<watch.hours<<’:’<<setw(2)<<watch.minutes<<’:’<<setw(2)<<watch.seconds<<endl;

int main()
StopWatch sw;
show(sw);
for (int i=0; i<59; 1++) sw++;
//ERROR************found************
show(sw++);
return 0:

【参考答案】

1)if(++seconds==60){
2)return*this;
3)show(++sw);
热门 试题

问答题
请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2。此工程定义了一个人员类Person,然后派生出学生类Student和教授类Professor。请在程序中的画线处填写适当的代码,然后删除横线,以实现上述定义。此程序的正确输出结果应为: My name is Zhang. my name is Wang and my G.P.A.is 3.88 My name is Li,I have 8 publications.. 注意:只能在画线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动“ ************found************”。 源程序 #include <iostream> using namespace std; class Person public: ************found************ ______name=NULL; Person(char*s) name=new char[strlen(s)+1];strcpy(name,s); ~Person() if(name!=NULL) delete[]name; ************found************ ______Disp() cout<< My name is <<name<< . n ; 声明虚函数 void setName(char*s) name=new char[strlen(s)+1];strcpy(name,s); protected: char*name: ; class Student: public Person public: ************found************ Student(char*s,double g)______ void Disp() cout<< my name is <<name<< and my G.P.A.is il<<gpa<< . n ; private: float gpa; ; class Professor: public Person public: void setPubls(int n)publs=n; void Disp() cout<< My name is <<name<< ,I have <<publs<< publications. n ; private: int publs; ; int main() ************found************ ______; Person x( Zhang ); p=&x;p->Disp(); Student y( Wang ,3.88); p=&y;p->Disp(); Professor z: z.setName( Li ); z.setPubls(8); p=&z;p->Disp(); return 0: