应用题
请使用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' << 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;
}
【正确答案】Person() virtual void :Person(s), gpa(g) Person*p
【答案解析】[考点] 本题考查的是Person类及其派生类Student类和Professor类,其中涉及动态数组、构造函数、析构函数、虚函数和成员函数。 (1)主要考查考生对构造函数的掌握情况,构造函数使用成员列表初始化name。 (2)主要考查考生对虚函数的掌握情况,虚函数使用关键字virtual,参考派生类中Disp函数可知函数返回类型为void。 (3)主要考查考生对构造函数的掌握情况,使用成员列表初始化。 (4)主要考查考生对指针的掌握情况,由语句:p=&x;p->Disp();可知,要定义p为Person类的指针。