填空题 以下程序运行时输出到屏幕的结果中,第一行是 1,第二行是 2,第三行是 3
#include<iostream.h>
#include<string.h>
class Person{
public:
Person(char *s)
{
name=new char[strlen(s)+1];
strcpy(name,s);
}
virtual void print()
{
cout<<"我的名字是:"<<name<<"/n";
}
protected:
char *name;
};
class Student:public Person
{
public:
Student(char *s,char *p):Person(s)
{
strcpy(num,p);
}
void print(char *s)
{
cout<<s;
cout<<"我的名字是:"<<name<<",学号是:"<<num<<". /n";
}
private:
char num[10];
};
class Professor:public Person
{
public:
Professor(char *s,int n):person(s)
{
publs=n;
}
void print()
{
cout<<"我的名字是:"<<name<<",发表论文数为:"<<publs<<"篇
/n";
}
private:
int publs;
};
void main()
{
Person *p;
Person x("张明");
Student y("王海","20120501");
Professor z("李涛",5);
p=&x;
p->print();
p=&y;
p->print();
p=&z;
p->print();
}
【正确答案】
【答案解析】我的名字是:张明、我的名字是:王海、我的名字是:李涛,发表论文数为:5篇