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

(继承类)帮忙找原因 能说的详细点

guchao2009 发布于 2009-11-25 11:06, 798 次点击
//定义一个基类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();
}

4 回复
#2
guchao20092009-11-25 11:06
错误原因:
--------------------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)
#3
gz812009-11-25 11:35
employee(N)

employee类根本没有这样一个构造函数employee(char*),你怎样调用??
#4
guchao20092009-11-25 12:15
改成这样就没有问题了:executive(char L[maxsize],int a,char n[maxsize],char N[maxsize]):Q(a,n),employee(N,a,n)

#5
flyingcloude2009-11-25 12:59
程序代码:
executive(char L[maxsize],int a,char n[maxsize],char N[maxsize]):Q(a,n),employee(N) //employee(char N(maxsize));employee类中没这样的构造函数,所以出错
    {
        cout<<"执行构造函数executive:"<<endl;
        strcpy(level,L);
    }

1