多态的应用,麻烦高手看下
#include <iostream.h>class base
{
protected:
char name[20];
public:
virtual void set(){};
virtual void fun(){};
virtual float get(){ return 0;}
};
class stu:public base
{ float score;
public:
void set()
{
cout<<"please input student name:"<<endl;
cin>>name;
cout<<"please input "<<name<<" score:"<<endl;
cin>>score;
}
void fun(){cout<<"student:"<<endl;cout<<name<<endl<<score<<endl;}
float get(){ return score;}
};
class tec:public base
{ int num;
public:
void set()
{
cout<<"please teacher name:"<<endl;
cin>>name;
cout<<"please input "<<name<<" zuoping num:"<<endl;
cin>>num;
}
void fun(){cout<<"teacher:"<<endl;cout<<name<<endl<<num<<endl;}
float get(){return float(num);}
};
int main()
{
base *p=NULL;
stu t[3];
tec z[3];
p=t;
for( int i=0;i<3;i++)
{
(p+i)->set();
}
for(i=0;i<3;i++)
{ if ((p+i)->get()>90)
(p+i)->fun();}
p=z;
for( i=0;i<3;i++)
(p+i)->set();
for(i=0;i<3;i++)
if ((p+i)->get()>3) (p+i)->fun();
return 0;
}
这个程序可以运行,也没错误,但是,输入一次后,就报错,不知道怎么回事,麻烦高手解读下!