| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 705 人关注过本帖
标题:虚基类构造函数的问题
只看楼主 加入收藏
mosquiyan
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2014-12-12
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:2 
虚基类构造函数的问题
/*设计一个虚基类base,包含name和私有数据成员以及相关的成员函数,
由它派生出领导类leader,包含职务和部门私有数据成员以及相关的成员函数(自定义)。
再由base派 生出工程师类engineer,包含职称和专业私有数据成员以及相关的成员函数(自定义)。
然后由leader和engineer类派生出主任工程师类chairman。
采用一些数据进行测试,注意构造函数调用的先后顺序。*/

#include <iostream>
#include <string>
using namespace std;

class Base
{
    public:
        Base(string na):name(na)
        {
            cout<<"Constructing Base"<<endl;
        }//构造函数
        
        Base();

        string getN() const
        {
            return name;
        }
        
        void showBase()
        {
            cout<<"Name:"<<name<<endl;
        }
    private:
        string name;
};

class Leader:virtual public Base
{
    public:
        /*Leader(string na,string nj,string nd):Base(na),jobs(nj),department(nd)
        {
            cout<<"Constructing Leader"<<endl;
        }//构造函数*///参数不符合问题
        
        Leader(string nj,string nd):Base(),jobs(nj),department(nd)
        {
        cout<<"Constructing Leader"<<endl;
        }

        string getJ() const
        {
            return jobs;
        }
        
        string getD() const
        {
            return department;
        }
        
        void showLeader()
        {
            cout<<"Jobs:"<<jobs<<endl<<"Department:"<<department<<endl;
        }
    private:
        string jobs;
        string department;   
};

class Engineer:virtual public Base
{
    public:
        /*Engineer(string na,string nk,string nm):Base(na),nick(nk),major(nm)
      {
        cout<<"Constructing Engineer"<<endl;
      }//构造函数*/

        Engineer(string nk,string nm):Base(),nick(nk),major(nm)
        {
            cout<<"Constructing Engineer"<<endl;
        }

        string getK() const
      {
        return nick;
      }
   
        string getM() const
      {
        return major;
      }
   
        void showEngineer()
      {
        cout<<"Nickname:"<<nick<<endl<<"Major:"<<major<<endl;
      }
    private:
        string nick;
        string major;
};

class Chairman:public Leader,public Engineer
{
    public:
        Chairman(string na2,string nj,string nd,string nk,string nm,int ne):Base(na2),Leader(nj,nd),Engineer(nk,nm),age(ne)
        {
            cout<<"Constructing Chairman"<<endl;
        }//构造函数
        
        int getE() const
        {
            return age;
        }
        
        void showChairman()
        {
            cout<<"Age:"<<age<<endl;
        }
    private:
        int age;
};

int main()
{
    Chairman myp("Ben","journalist","economic","manager","writing",28);
    myp.showBase();
    myp.showLeader();
    myp.showEngineer();
    myp.showChairman();
    return 0;
}

在devC++能正常运行,但是在vc++链接时出错,请大家帮忙看看这个程序有错吗?
还有被注释掉的构造函数错在哪里?
搜索更多相关主题的帖子: include public leader 工程师 职称 
2015-05-05 16:55
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:10 
Base::Base() 未定义
2015-05-06 08:27
hzj199603
Rank: 2
来 自:南京
等 级:论坛游民
帖 子:18
专家分:32
注 册:2015-3-29
收藏
得分:10 
回复 楼主 mosquiyan
#include <iostream>
#include <string>
using namespace std;

class Base
{
    public:
        Base(string na):name(na)
        {
            cout<<"Constructing Base"<<endl;
        }//构造函数
        
        Base(){};                                //构造函数错了

        string getN() const
        {
            return name;
        }
        
        void showBase()
        {
            cout<<"Name:"<<name<<endl;
        }
    private:
        string name;
};

class Leader:virtual public Base
{
    public:
        /*Leader(string na,string nj,string nd):Base(na),jobs(nj),department(nd)
        {
            cout<<"Constructing Leader"<<endl;
        }//构造函数*///参数不符合问题                    //虚基类不用在这儿定义
        
        Leader(string nj,string nd):Base(),jobs(nj),department(nd)
        {
        cout<<"Constructing Leader"<<endl;
        }

        string getJ() const
        {
            return jobs;
        }
        
        string getD() const
        {
            return department;
        }
        
        void showLeader()
        {
            cout<<"Jobs:"<<jobs<<endl<<"Department:"<<department<<endl;
        }
    private:
        string jobs;
        string department;   
};

class Engineer:virtual public Base
{
    public:
        /*Engineer(string na,string nk,string nm):Base(na),nick(nk),major(nm)
      {
        cout<<"Constructing Engineer"<<endl;
      }//构造函数*/

        Engineer(string nk,string nm):Base(),nick(nk),major(nm)
        {
            cout<<"Constructing Engineer"<<endl;
        }

        string getK() const
      {
        return nick;
      }
   
        string getM() const
      {
        return major;
      }
   
        void showEngineer()
      {
        cout<<"Nickname:"<<nick<<endl<<"Major:"<<major<<endl;
      }
    private:
        string nick;
        string major;
};

class Chairman:public Leader,public Engineer
{
    public:
        Chairman(string na2,string nj,string nd,string nk,string nm,int ne):Base(na2),Leader(nj,nd),Engineer(nk,nm),age(ne)
        {
            cout<<"Constructing Chairman"<<endl;
        }//构造函数
        
        int getE() const
        {
            return age;
        }
        
        void showChairman()
        {
            cout<<"Age:"<<age<<endl;
        }
    private:
        int age;
};

int main()
{
    Chairman myp("Ben","journalist","economic","manager","writing",28);
    myp.showBase();
    myp.showLeader();
    myp.showEngineer();
    myp.showChairman();
    return 0;
}

[ 本帖最后由 hzj199603 于 2015-5-7 21:24 编辑 ]
2015-05-07 21:09
快速回复:虚基类构造函数的问题
数据加载中...
 
   



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

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