|
#include<iostream.h>
#include<string.h>
class Person
{
protected:
char *name;
int age;
public:
Person(char *n=" ",int a=0)
{
name=new char[strlen(n)+1];
strcpy(name,n);
age=a;
}
~Person()
{
delete []name;
}
virtual void show()=0;
};
class Postgraduate:virtual public Person
{
private:
int number;
char *kind;
public:
Postgraduate(char *n=" ",int a=0,int num=0,char *k=" "):Person(n,a)
{
number=num;
kind=new char[strlen(k)+1];
strcpy(kind,k);
}
~Postgraduate()
{
delete []kind;
}
void show()
{
cout<<"姓名:"<<name<<" 年龄:"<<age<<" 学号:"<<number<<" 专业:"<<kind<<endl;
}
};
class Teacther:virtual public Person
{
private:
int work_number;
char *job;
public:
Teacther(char *n=" ",int a=0,int w=0,char *j=" "):Person(n,a)
{
work_number=w;
job=new char[strlen(j)+1];
strcpy(job,j);
}
~Teacther()
{
delete []job;
}
void show()
{
cout<<"姓名:"<<name<<" 年龄:"<<age<<" 工作证号:"<<work_number<<" 职称:"<<job<<endl;
}
};
class Work_Postgraduate:public Postgraduate,public Teacther
{
public:
Work_Postgraduate(char *n=" ",int a=0,int num=0,char *k=" ",int w=0,char *j=" "):Person(n,a),Postgraduate(n,a,num,k),Teacther(n,a,w,j)
{}
~Work_Postgraduate()
{}
void show()
{
Postgraduate::show();
Teacther::show();
}
};
void main()
{
Person *p;
Postgraduate A("张三",20,1,"计算机");
Teacther B("王二",32,9527,"教授");
Work_Postgraduate C("李四",24,9,"计算机",9521,"教师");
p=&A;
p->show();
p=&B;
p->show();
p=&C;
p->show();
}
在类Work_Postgraduate中的show()函数为什么不能同时显示Postgraduate::show()和Teacther::show();呢?
show()是一个虚函数. |
|