注册 登录
编程论坛 C++教室

大虾们帮看看哪错了

淑人公子 发布于 2010-11-27 13:41, 535 次点击
#include<iostream>
using namespace std;
class Teacher{
private:
    char name[10];
    int age;
    char title[10];
public:
    Teacher(char name[10]=0,int b=0,char title[10]=0){}
    void get(){
    cout<<"input name"<<endl;
    cin>>name;
    cout<<"input age"<<endl;
    cin>>age;
    cout<<"input title"<<endl;
    cin>>title;
    }
   
    void display(){
     cout<<"the name is"<<name<<endl;
     cout<<"the age is"<<age<<endl;
     cout<<"the title is"<<title<<endl;
    }

};
class Cadre{
public:
Cadre(char name[10]=0,int b=0,char post[10]=0){}
    get(){
    cout<<"input name"<<endl;
    cin>>name;
    cout<<"input age"<<endl;
    cin>>age;
    cout<<"input post"<<endl;
    cin>>post;
    }
   
    display(){
     cout<<"the post is"<<post<<endl;
    }
private:
    char name[10];
    int age;
    char post[10];
};
class Teacher_Cadre:public Teacher,public Cadre{
private:   
    int wage;   
public:
Teacher_Cadre(int wage=0){}
    void show(){
    Teacher::display();
    }
    getwage(){
    cout<<"input wage"<<endl;
    cin>>wage;
    }
    put(){
    cout<<"the wage is"<<wage<<endl;
    cout<<"the post is"<<endl;
Cadre::display();
    }
};
main(){
 Teacher_Cadre tc;
 Teacher t1;
 t1.get();
tc.show();
Cadre c1;
 c1.get();
 tc.getwage();
  tc.put();
}
6 回复
#2
玩出来的代码2010-11-27 13:49
LZ没有搞清楚继承的意义。
#3
laoyang1032010-11-27 14:00
Teacher(char name[10]=0,int b=0,char title[10]=0){}
Teacher t1;  参数类型不匹配
Teacher(char *name,int b=0,char *title){}

Cadre(char name[10]=0,int b=0,char post[10]=0){}
Cadre c1;   一样的错误
Cadre(char *name,int b=0,char *post){}

Teacher_Cadre(int wage=0){}初始化列表没有
应该这样
Teacher_Cadre(int wage=0,char *name,int b=0,char *post):Teacher(name,b,post),Cadre(name,b,post)
{}
你的程序是三角形多继承对吧  你的派生类有两个基类  所以需要给两个基类的构造函数传递参数的   


#4
淑人公子2010-11-27 15:57
#include<iostream>
using namespace std;
class Teacher{
private:
    char *name;
    int age;
    char *title;
public:
   Teacher(char a[10]=0,int b=0,char c[10]=0):name(a),age(b),title(c){}
    void get(){
    cout<<"input name"<<endl;
    cin>>name;
    cout<<"input age"<<endl;
    cin>>age;
    cout<<"input title"<<endl;
    cin>>title;
    }
   
    void display(){
     cout<<"the name is"<<name<<endl;
     cout<<"the age is"<<age<<endl;
     cout<<"the title is"<<title<<endl;
    }
};
class Cadre{
public:

Cadre(char a[10]=0,int b=0,char c[10]=0):name(a),age(b),post(c){}
    get(){
    cout<<"input name"<<endl;
    cin>>name;
    cout<<"input age"<<endl;
    cin>>age;
    cout<<"input post"<<endl;
    cin>>post;
    }
    display(){
     cout<<"the post is"<<post<<endl;
    }
private:
    char *name;
    int age;
    char *post;
};
class Teacher_Cadre:public Teacher,public Cadre{
private:   
    int wage;   
public:
    Teacher_Cadre(char a[10]=0,int b=0,char c[10]=0,char d[10]=0,int e=0):Teacher(a,b,c),Cadre(a,b,d),wage(e){}
    void show(){
    Teacher::display();
    }
    getwage(){
    cout<<"input wage"<<endl;
    cin>>wage;
    }
    put(){
    cout<<"the wage is"<<wage<<endl;
    cout<<"the post is"<<endl;
Cadre::display();
    }
};
 void main(){
Teacher_Cadre tc;
tc.Teacher::get();
tc.show();
tc.Cadre::get();
tc.getwage();
  tc.put();
}
这样还是错误,但如果把main换为int main()
{
    TeacherCadre teachercadre("xiaoqing",12,"boshi","teacher",1200);
    teachercadre.show ();
    return 0;
}
就对了,为什么只能初始化时赋值,不能调用函数输值啊
#5
laoyang1032010-11-27 18:09
那是因为你基类的构造函数已经有参数了

派生类的构造函数必须要向所有基类的构造函数提供相应的参数

具体的表现就是初始化列表
TeacherCadre teachercadre("xiaoqing",12,"boshi","teacher",1200);
这句话应该是Teache_Cadre teachercadre("xiaoqing",12,"boshi","teacher",1200);这样吧
呵呵  反正我改了之后就可以输出啦
#6
淑人公子2010-11-27 19:04
void main(){
Teacher_Cadre tc;
tc.Teacher::get();
tc.show();
tc.Cadre::get();
tc.getwage();
  tc.put();
}
为什么main这样写时不能输入?name的地址和a数组的地址一样,应该可以存入长度为9的字符串的但为什么提示错误
#7
淑人公子2010-11-28 15:55
在构造函数中 Teacher(char a[10]=0,int b=0,char c[10]=0):name(a),age(b),title(c){}a[10]是形参,并没有分配空间所以a[0]指向一个任意的地址,所以不能用get向里面输入,只能用构造函数初始化
1