| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 798 人关注过本帖
标题:(继承类)帮忙找原因 能说的详细点
取消只看楼主 加入收藏
guchao2009
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:101
专家分:106
注 册:2009-4-13
结帖率:77.78%
收藏
已结贴  问题点数:20 回复次数:2 
(继承类)帮忙找原因 能说的详细点
//定义一个基类person类,数据成员包含age,name,从该类派生一个employee(雇主)类,新增数据成员number(职工号);
//再从employee类派生一个executive(执行官)类,新增数据成员level(头衔),每个类中都定义相应的函数为其赋值,
//并定义函数显示相关的信息,如( "Smith is 28 years old,is an employee")或( "Smith is 30 years old,is an executive,level is the 2"),
//编写一个main函数,生成两个数组,一个包含三个executive对象,一个包含三个employee对象,然后显示他们的相关信息;

#include<iostream.h>
#include<string.h>
#define maxsize 20
class person
{
private:
    int age;
    char name[maxsize];
public:
    person(int a,char n[maxsize])
    {
        cout<<"执行构造函数person"<<endl;
        age=a;
        strcpy(name,n);
    }
    void Aprint();
};

void person::Aprint()
{
    cout<<name;
    cout<<" is "<<age<<" years old,";
}


class employee:public person
{
private:
    char number[maxsize];
public:
    employee(char N[maxsize],int a,char n[maxsize]):person(a,n)
    {
        cout<<"执行构造函数employee"<<endl;
        strcpy(number,N);
    }
    void Bprint()
    {
        Aprint();
        cout<<number<<" is a employee. ";
    }
};

class executive:public employee
{
private:
    char level[maxsize];
public:
    person Q;
    executive(char L[maxsize],int a,char n[maxsize],char N[maxsize]):Q(a,n),employee(N)
    {
        cout<<"执行构造函数executive:"<<endl;
        strcpy(level,L);
    }
    void Cprint();
};

void executive::Cprint()
{
    Bprint();
    cout<<" level is a "<<level<<"."<<endl;
}


void main()
{
    employee x("#001",19,"顾超");
    x.Bprint();
    executive y("科长",29,"顾超","@007");
    y.Cprint();
}

搜索更多相关主题的帖子: 继承 
2009-11-25 11:06
guchao2009
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:101
专家分:106
注 册:2009-4-13
收藏
得分:0 
错误原因:
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
H:\c++\9\1.cpp(55) : error C2664: '__thiscall employee::employee(const class employee &)' : cannot convert parameter 1 from 'char []' to 'const class employee &'
        Reason: cannot convert from 'char []' to 'const class employee'
        No constructor could take the source type, or constructor overload resolution was ambiguous
执行 cl.exe 时出错.

1.obj - 1 error(s), 0 warning(s)
2009-11-25 11:06
guchao2009
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:101
专家分:106
注 册:2009-4-13
收藏
得分:0 
改成这样就没有问题了:executive(char L[maxsize],int a,char n[maxsize],char N[maxsize]):Q(a,n),employee(N,a,n)

2009-11-25 12:15
快速回复:(继承类)帮忙找原因 能说的详细点
数据加载中...
 
   



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

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