填空题 以下程序运行时输出到屏幕的结果中第一行是 1,第二行是 2,第三行是 3
#include<iostream.h>
#include<string.h>
class CA
{
public:
CA(char *s)
{
str=new char[strlen(s)+1];
strcpy(str,s);
}
virtual void print()
{
cout<<str<<endl;
}
protected:
char *str:
};
class CB:public CA
{
public:
CB(char *s,char *s2):CA(s)
{
strcpy(str1,s2);
}
void print()
{
cout<<str<<",分数:"<<str1<<endl;
}
private:
char str1[10];
};
class CC:public CA
{
public:
CC(char *s,char *s2):CA(s)
{
strcpy(str1,s2);
}
void print()
{
cout<<str<<",性别:"<<str1<<endl;
}
private:
char str1[10];
};
void fun(CA *p)
{
p->print();
}
void main()
{
CA x("张明");
CB y("王海","85");
CC z("李涛","男");
fun(&x);
fun(&y);
fun(&z);
}
【正确答案】
【答案解析】张明、王海,分数:85、李涛,性别:男