在Windows XP+VC++6.0下编译通过并正常运行
#include
#include
using namespace std;
const int N=20;//字符长度的最大值
class BaseEmployee
{
protected:
char serialnumber[N];
char name[N];
public:
BaseEmployee(char *s="Null",char *n="Null");
virtual void display() const;//动态联编
};
BaseEmployee::BaseEmployee(char *s,char *n)
{
strcpy(serialnumber,s);
strcpy(name,n);
}
void BaseEmployee::display() const
{
cout<<"编号"<<"\t"<<"姓名"<
class DerivedEmployee:public BaseEmployee
{
protected:
char gender[N];
int age;
public:
DerivedEmployee(char *s="Null",char *n="Null",char *g="Null",int a=0);
void display() const;
};
DerivedEmployee::DerivedEmployee(char *s,char *n,char *g,int a):BaseEmployee(s,n)
{
strcpy(gender,g);
age=a;
}
void DerivedEmployee::display() const
{
cout<<"编号"<<"\t"<<"姓名"<<"\t"<<"性别"<<"\t"<<"年龄"<
void print(BaseEmployee *maybebase_maybederived)
{
maybebase_maybederived->display();
}
int main()
{
BaseEmployee b("101","张三");
print(&b);
cout<
print(&d);
return 0;
}
/*运行结果:
编号 姓名
101 张三
编号 姓名 性别 年龄
101 张三 男 21
*/