| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 583 人关注过本帖
标题:[求助]急!一个虚函数与虚基类的问题
取消只看楼主 加入收藏
que1
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-5-12
收藏
 问题点数:0 回复次数:0 
[求助]急!一个虚函数与虚基类的问题

#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()是一个虚函数.

搜索更多相关主题的帖子: 基类 函数 char int Postgraduate 
2006-07-02 16:34
快速回复:[求助]急!一个虚函数与虚基类的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016361 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved