标题:多重继承的构造函数出错
只看楼主
firmthinking
Rank: 3Rank: 3
来 自:西南交通大学
等 级:论坛游侠
帖 子:43
专家分:115
注 册:2012-12-9
结帖率:80%
 问题点数:0 回复次数:8 
多重继承的构造函数出错
#include<iostream>
#include<string>
using namespace std;
class Human
{
public:
    Human(string a,string b,int c)
    {
        name=a;
        sex=b;
        age=c;
    }
    void show()
    {
        cout<<"name:"<<name<<endl;
        cout<<"age:"<<age<<endl;
        cout<<"sex:"<<sex<<endl;
    }
    string name,sex;
    int age;
};
class Teacher:public Human
{
public:
    Teacher(string a,string b,int c,string d):Human(a,b,c)
    {
        title=d;
    }
    void show()
    {
        Human::show();
        cout<<"title:"<<title<<endl<<endl;
    }
private:
    string title;
};
class Cader:public Human
{
public:
    Cader(string a,string b,int c,string d):Human(a,b,c)
    {
        position=d;
    }
    void show()
    {
        Human::show();
        cout<<"position:"<<position<<endl<<endl;
    }
    string position;
};
class TeacherCader:public Teacher,public Cader
{
public:
    TeacherCader(string a,string b,int c,string d,string e):Human(a,b,c),Teacher(a,b,c,d),Cader(a,b,c,e){};
    void show()
    {
        Teacher::show();
        cout<<"position:"<<position<<endl<<endl;
    }
};
int main()
{
    Teacher aa("张三",48,"男","教授");
    aa.show();
    Cader bb("李四",55,"男","校长");
    bb.show();
    TeacherCader cc("王五",45"女","教授","院长");
    cc.show();
    return 0;
}
搜索更多相关主题的帖子: void namespace include public title 
2013-05-20 19:01
firmthinking
Rank: 3Rank: 3
来 自:西南交通大学
等 级:论坛游侠
帖 子:43
专家分:115
注 册:2012-12-9
得分:0 
出现一个错误提示:
E:\面向对象\实验五\file03\file03.cpp(54) : error C2614: 'TeacherCader' : illegal member initialization: 'Human' is not a base or member
请问谁能帮帮忙。
2013-05-20 19:03
firmthinking
Rank: 3Rank: 3
来 自:西南交通大学
等 级:论坛游侠
帖 子:43
专家分:115
注 册:2012-12-9
得分:0 
自己发现问题了,打扰了。
2013-05-20 19:11
hpshuaia
Rank: 2
等 级:论坛游民
帖 子:28
专家分:22
注 册:2013-5-16
得分:0 
我改了下。 #include<iostream>
#include<string>
using namespace std;
class Human
{
public:
    Human(string a,string b,int c)
    {
        name=a;
        sex=b;
        age=c;
    }
    void show()
    {
        cout<<"name:"<<name<<endl;
        cout<<"age:"<<age<<endl;
        cout<<"sex:"<<sex<<endl;
    }
    string name,sex;
    int age;
};
class Teacher:public Human
{
public:
    Teacher(string a,string b,int c,string d):Human(a,b,c)
    {
        title=d;
    }
    void show()
    {
        Human::show();
        cout<<"title:"<<title<<endl<<endl;
    }
public:
    string title;
};
class Cader:public Human
{
public:
    Cader(string a,string b,int c,string d):Human(a,b,c)
    {
        position=d;
    }
    void show()
    {
        Human::show();
        cout<<"position:"<<position<<endl<<endl;
    }
    string position;
};
class TeacherCader:public Teacher,public Cader
{
public:
    TeacherCader(string a,string b,int c,string d,string e):Teacher(a,b,c,d),Cader(a,b,c,e){};
    void show()
    {
        Teacher::show();
        cout<<"position:"<<position<<endl<<endl;
    }
};
int main()
{
    Teacher *aa=new Teacher("张三","男",48,"教授");
    aa->show();
    Cader *bb=new Cader("李四","男",55,"校长");
    bb->show();
    TeacherCader *cc=new TeacherCader("王五","女",45,"教授","院长");
    cc->show();
    return 0;
}
2013-05-24 08:36
hpshuaia
Rank: 2
等 级:论坛游民
帖 子:28
专家分:22
注 册:2013-5-16
得分:0 
可以运行了,
2013-05-24 08:36
坚持到底1
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2013-5-23
得分:0 
你的程序中        Teacher(string a,string b,int c,string d):
               Teacher("张三","男",48,"教授");
我觉得如果让实参和形参的类型顺序一样的话也可以不采用 Teacher *aa=new Teacher("张三","男",48,"教授");

2013-05-24 14:25
坚持到底1
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2013-5-23
得分:0 
不好意思,没仔细看你改过的程序
2013-05-24 14:26
坚持到底1
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2013-5-23
得分:0 
你的程序中        Teacher(string a,string b,int c,string d):
               Teacher("张三","男",48,"教授");
我觉得如果让实参和形参的类型顺序一样的话也可以不采用 Teacher *aa=new Teacher("张三","男",48,"教授");

2013-05-24 14:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-407559-1-1.html




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

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