问题描述
- 派生类存放在基类类型vector中,怎么用派生类的函数?
- 我已经成功将派生类存放在基类类型的vector表中,也能定位位置,就是不知道怎么使用派生类的函数,只能用基类的函数,求解啊!
#include
#include
#include
#include
using namespace std;
class people
{
public:
people()
{
name = new char[20];
code = 0;
sex = new char[8];
number = new char[20];};bool people::operator==(const people &rhs) const{ return (code == rhs.code);};void in_mess(){ cout << ""输入名字、编号、性别、身份证号"" << endl; cin >> name; cin >> code; cin >> sex; cin >> number; };const int consttime = 10;void out_mess(){ cout << ""名字:"" << name << endl; cout << ""编号:"" << code << endl; cout << ""性别:"" << sex << endl; cout << ""身份证号:"" << number << endl;};int seach(){ return code;};void eq_SC(int a){ code = a;}
private:
char *name;
int code;
char *sex;
char *number;};
class student : virtual public people
{
public:
student(){};
friend istream&operator>>(istream&in student &p)
{
p.in_mess();
return in;
};
void out_messs()
{
people::out_mess();
cout << ""身份:学生"" << endl;
cout << ""总学时:"" << consttime << endl;}
};
class teacher :virtual public people
{
public:
teacher()
{
teachtime = 0;};friend istream&operator>>(istream&in teacher &p){ p.in_mess(); cout << ""身份:老师"" << endl; cout << ""输入教学学时"" << endl; in >> p.teachtime; return in;}void out_messs(){ people::out_mess(); cout << ""身份:老师"" << endl; cout << ""总学时:"" << teachtime + consttime << endl;}int get_TT(){ return teachtime + consttime;}
private:
int teachtime;
};
class graduate : virtual public student
{
public:
graduate()
{
freetime = 0;
};
friend istream&operator>>(istream&in graduate &p)
{
p.in_mess();
cout << ""身份:研究生"" << endl;
cout << ""输入自由研究时间"" << endl;
in >> p.freetime;
return in;
}
void out_messs()
{
people::out_mess();
cout << ""身份:研究生"" << endl;
cout << ""总学时:"" << freetime + consttime << endl;}int get_FT(){ return freetime+consttime;}
private:
int freetime;
};
class TA : public graduate public teacher
{
public:
TA()
{
resarch_time = 0;
};
friend istream&operator>>(istream&in TA &p)
{
p.in_mess();
cout << ""身份:助教"" << endl;
cout << ""输入做研究和一定教学工作学时"" << endl;
in >> p.resarch_time;
return in;
};
void out_messs()
{
people::out_mess();
cout << ""身份:助教"" << endl;
cout << ""总学时:"" << resarch_time +consttime+get_FT()+get_TT() << endl;
};
private:
int resarch_time;
};
void main()
{
cout << ""当前为空表,请输入第一个人员的信息"" << endl;
cout << ""插入身份为 1.学生 2.老师 3.研究生 4.助教 请输入序号"" << endl;
int num=0;
people first;
cin >> num;
if (num == 1)
{
student first;
cin >> first;
}
else if (num == 2)
{
teacher first;
cin >> first;}
else if (num == 3)
{
graduate first;
cin >> first;}
else if (num == 4)
{
TA first;
cin >> first;};
vector<people>::iterator location;//迭代子vector<people>V(1 first);while (1){ int gong; cout << ""请选择功能"" << endl; cout << ""1.插入新的信息 2.查找 "" << endl; cin >> gong; if (gong == 1) { cout << ""插入身份为 1.学生 2.老师 3.研究生 4.助教 请输入序号"" << endl; int num; cin >> num; if (num == 1) { student human; cin >> human; V.push_back(human); } else if (num == 2) { teacher human; cin >> human; V.push_back(human); } else if (num == 3) { graduate human; cin >> human; V.push_back(human); } else if (num == 4) { TA human; cin >> human; V.push_back(human); }; } else if (gong == 2) { people _new; int studycodee; cout << ""请输入他(她)的编号"" << endl; cin >> studycodee; _new.eq_SC(studycodee); location = find(V.begin() V.end() _new); if (location != V.end()) { V[location - V.begin()].out_mess(); } else cout << ""未找到"" << endl; }}
}
下面查找功能只能用到其基类的函数,用不到派生类的,我觉得是因为vector是存放的people类导致的,但是即使我将其派生并且重新放置派生类进去,它依然只认基类不认派生类函数啊!怎么办呀!
解决方案
你需要用多态,
而且vector中声明基类指针,然后来得到派生类。这样通过基类指针调用虚函数来实现对派生类的调用
base *p = new derive()
解决方案二:
基类和派生类的虚函数表