| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 639 人关注过本帖
标题:sos,一道c++编程题
只看楼主 加入收藏
淑人公子
Rank: 1
等 级:新手上路
帖 子:9
专家分:5
注 册:2010-11-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:9 
sos,一道c++编程题
#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();
}
调试时可以输进去数字,但到输出时显示内存错误了
搜索更多相关主题的帖子: sos 
2010-11-27 13:05
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:5 
Teacher_Cadre(int wage=0){}
初始化列表  初始化列表  怎么全是这个问题啊......

                                         
===========深入<----------------->浅出============
2010-11-27 14:02
淑人公子
Rank: 1
等 级:新手上路
帖 子:9
专家分:5
注 册:2010-11-27
收藏
得分:0 
谢谢,终于成功了,这个动态分配的对了不知道那个数组为什么不对
2010-11-27 16:09
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
你传递参数如果是数组的话  难道你要把数组中所有的元素都作为参数吗?

所以只要传递一个首地址就可以啦  

                                         
===========深入<----------------->浅出============
2010-11-27 18:00
shi19871987
Rank: 1
来 自:上海
等 级:新手上路
帖 子:8
专家分:6
注 册:2010-10-22
收藏
得分:5 
这个有bug程序想要表达的意思是什么,没表达有意义的结果。
2010-11-27 19:49
shi19871987
Rank: 1
来 自:上海
等 级:新手上路
帖 子:8
专家分:6
注 册:2010-10-22
收藏
得分:0 
#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不要这种方法写。
2010-11-27 20:15
woshiniulei
Rank: 1
等 级:新手上路
帖 子:1
专家分:5
注 册:2010-11-27
收藏
得分:5 
kj
2010-11-27 21:22
gongzeyang
Rank: 1
等 级:新手上路
帖 子:1
专家分:5
注 册:2010-11-28
收藏
得分:5 
有点专业
2010-11-28 10:26
淑人公子
Rank: 1
等 级:新手上路
帖 子:9
专家分:5
注 册:2010-11-27
收藏
得分:0 
#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了就不能再开辟空间了?   

2010-11-28 15:41
淑人公子
Rank: 1
等 级:新手上路
帖 子:9
专家分:5
注 册:2010-11-27
收藏
得分:0 
因为形参名字与数据成员的名字相同了,如果改成 Teacher(char *name=0,int age=0,char *title=0){this->name=new char[10];就对了,可是系统都不看形参名为什么还不能和数据成员的名字相同
2010-11-28 18:06
快速回复:sos,一道c++编程题
数据加载中...
 
   



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

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