| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1654 人关注过本帖
标题:C++数据成员和成员函数重名问题
取消只看楼主 加入收藏
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
结帖率:90%
收藏
已结贴  问题点数:5 回复次数:2 
C++数据成员和成员函数重名问题
运行结果
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

signal: aborted (core dumped)

程序代码
程序代码:
#include<string>
#include<iostream>
using namespace std;
class Teacher
{
   public:
       Teacher(string name,int age,string gender,string address,string phonenum,string title)
       {
           this->name=name;
           this->age=age;
           this->gender=gender;
           this->address=address;
           this->phonenum=phonenum;
           this->title=title;
       }
   void display();
   
   protected: 
   string name;
   int age;
   string gender;
   string address;
   string phonenum;
   string title;  //职称 
};
void Teacher::display()
{
    cout<<"姓名"<<name<<endl;
    cout<<"年龄"<<age<<endl;
    cout<<"性别"<<gender<<endl;
    cout<<"地址"<<address<<endl;
    cout<<"电话"<<phonenum<<endl;
}
class Cadre
{
   public:
           Cadre(string name,int age,string gender,string address,string phonenum,string post)
       {
           this->name=name;
           this->age=age;
           this->gender=gender;
           this->address=address;
           this->phonenum=phonenum;
           this->post=post;
       }
   void display();
   protected: 
   string name;
   int age;
   string gender;
   string address;
   string phonenum;    
   string post;//职务 
    
};
void Cadre::display()
{
    cout<<"姓名"<<name<<endl;
    cout<<"年龄"<<age<<endl;
    cout<<"性别"<<gender<<endl;
    cout<<"地址"<<address<<endl;
    cout<<"电话"<<phonenum<<endl;
}
class Teacher_Cadre : public Teacher,public Cadre
{
    public:
        Teacher_Cadre(string name,int age, string gender, string address, string phonenum,int wage,string post):
        Teacher(name,age,gender,address,phonenum,title),Cadre(name,age,gender,address,phonenum,post)
     {
         //用基类限定符指明,避免二义性问题 
         this->Teacher::name=name;
           this->Teacher::age=age;
           this->Teacher::gender=gender;
           this->Teacher::address=address;
           this->Teacher::phonenum=phonenum;
           this->post=post;
           this->wage=wage;
     }
      void show();
     private:
     int wage;    

 };

 void Teacher_Cadre ::show()

 {
     //同上 
     Teacher::display();
     cout<<"职务"<<post<<endl;
     cout<<"工资"<<wage<<endl; 

 }

 int main()

 {
     Teacher_Cadre l("王二",22,"man","Beng","1811",600,"Eng");
     l.show();
     return 0;

 }
搜索更多相关主题的帖子: string name age address cout 
2018-05-11 16:01
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
收藏
得分:0 
回复 2楼 Jonny0201
老铁,你用的是什么编译器?。。。。。
2018-05-13 20:31
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
收藏
得分:0 
回复 3楼 渐渐鱼
另外那个是因为下面没有用title,所以不需要把title加进去,选择下面要初始化的就ok
2018-05-13 20:33
快速回复:C++数据成员和成员函数重名问题
数据加载中...
 
   



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

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