C++数据成员和成员函数重名问题
运行结果terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
signal: aborted (core dumped)
程序代码
程序代码:
#include<string> #include<iostream> using namespace std; class Teacher { public: Teacher(string name,int age,string gender,string address,string phonenum,string title) { this->name=name; this->age=age; this->gender=gender; this->address=address; this->phonenum=phonenum; this->title=title; } void display(); protected: string name; int age; string gender; string address; string phonenum; string title; //职称 }; void Teacher::display() { cout<<"姓名"<<name<<endl; cout<<"年龄"<<age<<endl; cout<<"性别"<<gender<<endl; cout<<"地址"<<address<<endl; cout<<"电话"<<phonenum<<endl; } class Cadre { public: Cadre(string name,int age,string gender,string address,string phonenum,string post) { this->name=name; this->age=age; this->gender=gender; this->address=address; this->phonenum=phonenum; this->post=post; } void display(); protected: string name; int age; string gender; string address; string phonenum; string post;//职务 }; void Cadre::display() { cout<<"姓名"<<name<<endl; cout<<"年龄"<<age<<endl; cout<<"性别"<<gender<<endl; cout<<"地址"<<address<<endl; cout<<"电话"<<phonenum<<endl; } class Teacher_Cadre : public Teacher,public Cadre { public: Teacher_Cadre(string name,int age, string gender, string address, string phonenum,int wage,string post): Teacher(name,age,gender,address,phonenum,title),Cadre(name,age,gender,address,phonenum,post) { //用基类限定符指明,避免二义性问题 this->Teacher::name=name; this->Teacher::age=age; this->Teacher::gender=gender; this->Teacher::address=address; this->Teacher::phonenum=phonenum; this->post=post; this->wage=wage; } void show(); private: int wage; }; void Teacher_Cadre ::show() { //同上 Teacher::display(); cout<<"职务"<<post<<endl; cout<<"工资"<<wage<<endl; } int main() { Teacher_Cadre l("王二",22,"man","Beng","1811",600,"Eng"); l.show(); return 0; }