设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 1072|回复: 1
打印 上一主题 下一主题

[求助]急!一个虚函数与虚基类的问题

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-3 03:33:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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()是一个虚函数.
2#
发表于 2009-11-3 03:33:34 | 只看该作者
你的中间的类的 void show() 也要声明为 virtual,
因为后面还要继承
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-19 00:40 , Processed in 0.095990 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表