填空题
[程序] (2分)
#include
#include
class B{
public:
B(char *s){name=new char[strlen(s)+1];strcpy(name,s);}
virtual void print1{cout<<"姓名;"<
protected:
char *name;
};
class P1:public B{
public:
P1(char *s,float g):B(s){x=g;}
void print(int i)
{ cout<<"姓名:"<年薪:"<万元。 /n";}
private:
float x;
};
class P2:public B{
public:
P2(char *s,int n):B(s){ num=n;}
void print2{cout<<"姓名;"<月工资:"<元。/n";}
private:
int num;
};
void main(void)
{
B *p;
B x("张明");
P1 y("王虎",4.2);
P2 z("李建国",5000);
p=&x;p->print3;
p=&y;p->print4;
y.print(1);
p=&z;p->print5;
}
执行以上程序后输出的第二行是 6(17) 7 ,第四行是 8(18) 9。
【正确答案】
1、(17)姓名:王虎, 2、(18)姓名:李建国, 3、月工资:5000元。
【答案解析】