读书人

大家帮看看,小弟我这个继承错在哪里了

发布时间: 2012-03-23 12:06:21 作者: rapoo

大家帮看看,我这个继承错在哪里了啊?
谢谢大家了
#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
void display();
protected:
string name;
int age;
char sex;
string addr;
string tel;
string title;
};
void Teacher::display()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"addr:"<<addr<<endl;
cout<<"tel:"<<tel<<endl;
cout<<"title:"<<title<<endl;
}
class Cadre
{
public:
void display();
protected:
string name;
int age;
char sex;
string addr;
string tel;
string post;
};
void Cadre::display()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"addr:"<<addr<<endl;
cout<<"tel:"<<tel<<endl;
cout<<"post:"<<post<<endl;
}
class Teacher_Cadre: public Teacher,public Cadre
{
public:
void display1();
void get_value();
private:
float wages;
};
void Teacher_Cadre::get_value()
{
cin>>name>>age>>sex>>addr>>tel>>title>>post>>wages;
}
void Teacher_Cadre::display1()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"addr:"<<addr<<endl;
cout<<"tel:"<<tel<<endl;
cout<<"title:"<<title<<endl;
cout<<"post:"<<post<<endl;
cout<<"wages:"<<wages<<endl;
}
int main()
{
Teacher_Cadre t;
t.get_value();
t.display1();
return 0;
}


[解决办法]

C/C++ code
//域的问题void Teacher_Cadre::get_value() {     cin>>Teacher::name>>Teacher::age>>Teacher::sex>>Teacher::addr>>Teacher::tel>>        Teacher::title>>post>>wages; } void Teacher_Cadre::display1() {     cout <<"name:" <<Teacher::name <<endl;     cout <<"age:" <<Teacher::age <<endl;     cout <<"sex:" <<Teacher::sex <<endl;     cout <<"addr:" <<Teacher::addr <<endl;     cout <<"tel:" <<Teacher::tel <<endl;     cout <<"title:" <<Teacher::title <<endl;     cout <<"post:" <<post <<endl;     cout <<"wages:" <<wages <<endl; }
[解决办法]
尽量不要使用多重继承!
在你的程序中使用了多重继承,两个父类中有相同的名字的属性,这是会产生二义性的,例如name,修改属性名字来改正你的程序。
[解决办法]
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
void display();
protected:
string name;
int age;
char sex;
string addr;
string tel;
string title;
};
void Teacher::display()
{
cout <<"name:" <<name <<endl;
cout <<"age:" <<age <<endl;


cout <<"sex:" <<sex <<endl;
cout <<"addr:" <<addr <<endl;
cout <<"tel:" <<tel <<endl;
cout <<"title:" <<title <<endl;
}
class Cadre
{
public:
void display();
protected:
string name;
int age;
char sex;
string addr;
string tel;
string post;
};
void Cadre::display()
{
cout <<"name:" <<name <<endl;
cout <<"age:" <<age <<endl;
cout <<"sex:" <<sex <<endl;
cout <<"addr:" <<addr <<endl;
cout <<"tel:" <<tel <<endl;
cout <<"post:" <<post <<endl;
}
class Teacher_Cadre: virtual public Teacher, public Cadre
{
public:
void display1();
void get_value();
private:
float wages;
};
void Teacher_Cadre::get_value()
{
cin>>Teacher::name>>Teacher::age>>Teacher::sex>>Teacher::addr>>Teacher::tel>>Teacher::title>>post>>wages;
}
void Teacher_Cadre::display1()
{
cout <<"name:" <<Teacher::name <<endl;
cout <<"age:" <<Teacher::age <<endl;
cout <<"sex:" <<Teacher::sex <<endl;
cout <<"addr:" <<Teacher::addr <<endl;
cout <<"tel:" <<Teacher::tel <<endl;
cout <<"title:" <<Teacher::title << endl;
cout <<"post:" <<post <<endl;
cout <<"wages:" <<wages <<endl;
}
int main()
{
Teacher_Cadre t;
t.get_value();
t.display1();
return 0;
}
//多重继承 主要赵成不明域!使用作用域就可以,使用virtual继承也可以决绝问题。

读书人网 >C++

热点推荐