问答题

请使用“答题”菜单或使用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:

【参考答案】

A)Person(){name=NULL;}
B)virtual void Disp(){cout<<"My......

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

问答题
请使用“答题”菜单或使用VC6打开考生文件夹proj3下的工程proj3,其中声明了MyString类。MyString是一个用于表示字符串的类。成员函数startsWith的功能是判断此字符串是否以指定的前缀开始,其参数s用于指定前缀字符串。如果参数s表示的字符串是MyString对象表示的字符串的前缀,则返回true;否则返回false。注意,如果参数s是空字符串或等于MyString对象表示的字符串,则结果为true。 例如:字符串 abc 是字符串 abcde 的前缀,而字符串 abd 不是字符串 abcde 的前缀。请编写成员函数startsWith。在main函数中给出了一组测试数据,此情况下程序的输出应该是: s1=abcde s2=abc s3=abd s4= s5=abcde s6=abcdef s1 startsWith s2:true s1 startsWith s3 false s1 startsWith s4 true s1 startsWith s5 f true s1 startsWith s6 false 要求:补充编制的内容写在 ********333********与 ********666********两行之间,不得修改程序的其他部分。 注意:程序最后已经将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 源程序 #include MyString.h bool MyString::startsWith(const char*s)const ********333******** ********666******** int main() char s1[]= abcde ; char s2[]= abc ; char s3[]= abd ; char s4[]= ; char s5[]= abcde ; char s6[]= abcdef ; MyString str(s1); cout<< s1= <<s1<<endl<< s2= <<s2<<endl<< s3= <<s3<<endl << s4= <<s4<<endl<< s5= <<s5<<endl<< s6= <<s6<<endl: cout<<boolalpha<< s1 startsWith s2: <<str.startsWith(s2)<<endl << s1 startsWith s3: <<str.startsWith(s3)<<endl << s1 startsWith s4: <<str.startsWith(s4)<<endl << s1 startsWith s5: <<str.startsWith(s5)<<endl << s1 startsWith s6: <<str.startsWith(s6)<<endl; writeToFile( K: bl0 61000101 ); return 0:
问答题
请使用“答题”菜单或使用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: