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

sos,一道c++编程题

淑人公子 发布于 2010-11-27 13:05, 639 次点击
#include<iostream>
using namespace std;
class Teacher{
private:
    char *name;
    int age;
    char *title;
public:
    Teacher(char *name=0,int age=0,char *title=0){
        }
    void get(){
    name=new char[10];
        title=new char[10];   
    cout<<"input name"<<endl;
    cin>>name;
    cout<<"input age"<<endl;
    cin>>age;
    cout<<"input title"<<endl;
    cin>>title;
    };
    ~Teacher(){delete[] name;delete[] 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=0,int age=0,char *post=0){}
    get(){
        name=new char[10];
        post=new char[10];   
    cout<<"input name"<<endl;
    cin>>name;
    cout<<"input age"<<endl;
    cin>>age;
    cout<<"input post"<<endl;
    cin>>post;
    }
    ~Cadre(){delete[] name;delete[] 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(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();
Cadre c1;
 c1.get();
 tc.getwage();
 
 tc.show();
 tc.put();
}
调试时可以输进去数字,但到输出时显示内存错误了
9 回复
#2
laoyang1032010-11-27 14:02
Teacher_Cadre(int wage=0){}
初始化列表  初始化列表  怎么全是这个问题啊......
#3
淑人公子2010-11-27 16:09
谢谢,终于成功了,这个动态分配的对了不知道那个数组为什么不对
#4
laoyang1032010-11-27 18:00
你传递参数如果是数组的话  难道你要把数组中所有的元素都作为参数吗?

所以只要传递一个首地址就可以啦  
#5
shi198719872010-11-27 19:49
这个有bug程序想要表达的意思是什么,没表达有意义的结果。
#6
shi198719872010-11-27 20:15
#include<iostream>
using namespace std;
class Teacher
{
private:
    char *name;
    int age;
    char *title;
public:
    Teacher(char *name=0,int age=0,char *title=0)
    {    }
    void get()
    {
        name=new char[10];
        title=new char[10];   
        cout<<"input name:"<<endl;
        cin>>name;
        cout<<"input age:"<<endl;
        cin>>age;
        cout<<"input title:"<<endl;
        cin>>title;
    };
    ~Teacher()
    {
        delete[] name;delete[] 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=0,int age=0,char *post=0)
    {    }
   void get()
    {
        name=new char[10];
        post=new char[10];   
        cout<<"input name:"<<endl;
        cin>>name;
        cout<<"input age:"<<endl;
        cin>>age;
        cout<<"input postion:"<<endl;
        cin>>post;
    }
    ~Cadre(){delete[] name;delete[] post;}
    void 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(int wage=0)
    {    }
    void show()
    {
    Teacher::display();
    }
   void getwage()
    {
    cout<<"input wage:"<<endl;
    cin>>wage;
    }
    void put()
    {
    cout<<"the wage is"<<wage<<endl;
    cout<<"the post is"<<endl;
      Cadre::display();
    }
};
int main()
{
Teacher_Cadre tc;
Teacher t1;
t1.get();
Cadre c1;
c1.get();
tc.getwage();
tc.Teacher::get();
tc.Cadre::get();
tc.show();
tc.put();
}
tc.show()没有初始化,增加初始化即可。而且,这段代码很混乱,建议LZ不要这种方法写。
#7
woshiniulei2010-11-27 21:22
kj
#8
gongzeyang2010-11-28 10:26
有点专业
#9
淑人公子2010-11-28 15:41
#include<iostream>
using namespace std;
class Teacher{
private:
    char *name;
    int age;
    char *title;
public:
    Teacher(char *name=0,int age=0,char *title=0){
        name=new char[10];
        title=new char[10];   
        }
    void get(){
    cout<<"input name"<<endl;
    cin>>name;
    cout<<"input age"<<endl;
    cin>>age;
    cout<<"input title"<<endl;
    cin>>title;
    };
    ~Teacher(){delete[] name;delete[] 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=0,int age=0,char *post=0){
        name=new char[10];
        post=new char[10];   }
    get(){
     cout<<"input name"<<endl;
    cin>>name;
    cout<<"input age"<<endl;
    cin>>age;
    cout<<"input post"<<endl;
    cin>>post;
    }
    ~Cadre(){delete[] name;delete[] 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(int wage=0,char *name=0,int b=0,char *post=0,char *title=0):Teacher(name,b,title),Cadre(name,b,post){}
    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;
tc.Teacher::get();
tc.show();
tc.Cadre::get();
tc.getwage();
  tc.put();
}
这样错了但是像上面那样把name=new char[10];title=new char[10];写在get的函数体中就对了,是不是在构造函数中把name指向0了就不能再开辟空间了?   

#10
淑人公子2010-11-28 18:06
因为形参名字与数据成员的名字相同了,如果改成 Teacher(char *name=0,int age=0,char *title=0){this->name=new char[10];就对了,可是系统都不看形参名为什么还不能和数据成员的名字相同
1