从子类构造函数构造基类时遇到问题

时间:2020-04-16 21:59:16

标签: c++ class inheritance member-initialization object-construction

我有2节课。由于Doctor将被视为Employee,因此我应该在Doctor类中使用Employee类的函数。 Doctor类唯一具有的其他功能是 TITLE 。基本上,我尝试的是我想将值发送给Doctor的构造函数,设置标题,然后将剩余的值发送给Employee的类;但是,我不能。这是我到目前为止所做的,

employee.h

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
class Employee {
private:
    int ID;
    char *firstname;
    char *lastname;
    int telno;
    char *adress;
    char *mail;
    int salary; 

public:
    Employee();
    Employee(int,char *,char*,int,char*,char*,int);

    char* getfmame();
    char* getlname();
    char* getadress();
    char* getmail();
    int getID();
    int gettel();
    int getsalary();
    void printall();
};
#endif

Employee.cpp

#include <iostream>
#include "employee.h"

using namespace std;
Employee::Employee() {
    firstname = "Empty";
    ID=0;
    firstname="Empty";
    lastname="Empty";
    telno=0;
    adress="Empty";
    mail="Empty";
    salary=0; 
}

Employee::Employee(int id,char * first,char* last,int tell,char* adres,char* email,int salar){
    ID=id;
    firstname=first;
    lastname=last;
    telno=tell;
    adress=adres;
    mail=email;
    salary=salar;

}
char* Employee::getfmame(){ return firstname; }
char* Employee::getlname(){ return lastname; }
char* Employee::getadress(){ return adress; }
char* Employee::getmail(){ return mail; }
int Employee::getID(){ return ID; }
int Employee::gettel(){ return telno; }
int Employee::getsalary(){ return salary; }

void Employee::printall(){
    cout<<endl<<"EMLOYEE INFORMATION"<<endl<<"------------------"<<endl;
    cout<<endl<<"ID :"<<ID<<endl<<"FIRST NAME: "<< firstname <<endl<<"LAST NAME: "<< lastname << endl << "TELEPHONE NUMBER: "<<telno<<endl<<"ADRESS: "<<adress<<endl<<"MAIL: "<<mail<<endl<<"SALARY: "<<salary<<endl;

}

Doctor.h

#ifndef DOCTOR_H
#define DOCTOR_H
#include "Employee.h"

using namespace std;
class Doctor :Employee {
public:
    enum title {Intern=0,Practitioner=1,Assistant=2,Specialist=3,Docent=4,Professor=5,None=6};
    Doctor();
    Doctor(title a,int id,char * first,char* last,int tell,char* adres,char* email,int salar);  
};
#endif

Doctor.cpp

#include <iostream>
#include "Doctor.h"
#include "Employee.h"

using namespace std;

Doctor::Doctor() {
    title tit = None ;
}

Doctor::Doctor(title a,int id,char * first,char* last,int tell,char* adres,char* email,int salar) {
    title tit=a;
    Employee(id,first,last, tell,adres,email,salar);
    printall();
    cout<<"typed";
}

Main.cpp

#include <iostream>
#include "employee.h"
#include "doctor.h"
using namespace std;

int main(){ 
    Doctor a=Doctor(Doctor::None,12,"a","b",0550550505,"8424 str nu:5","@hotmail",5000);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

C ++中的子类构造有效,因此必须在执行子类的构造函数主体时构造基类对象:

class A {
    /* etc. etc. */ 
public:
    void do_stuff();
};

class B : public A {
    B() {
        // at this point, an A has already been constructed!
        A::do_stuff();
    }
};

请注意,在此示例中,由于我们没有为A实例选择显式构造函数,因此将使用默认构造函数A::A();如果该构造函数不可用-我们会收到编译错误。可以调用A的构造函数的事实是,我们可以使用类A的方法-像上面示例中的A::do_stuff()一样。

但是-我们如何在B构造函数的主体 之前指定其他构造函数?或者在您的情况下,如何在Employee构造函数的主体之前为Doctor使用适当的构造函数?

@ user4581301给出了答案:您需要使用member initializer list。此列表上的初始化/构造是在主体之前执行的,并且可能包含基础类。我将用一个简化的示例进行演示。假设Employee仅具有ID,而Doctor仅具有附加标题。

class Employee {
protected:
    int id_;
public:
    Employee(int id) : id_(id) { };
    int id() const { return id_; }
};

class Doctor : public Employee {
protected:
    std::string title_;
public:
    Doctor(int id, std::string title) : Employee(id), title_(title) { };
    const std::string& title() const { return title_; }
};

因此,在构造Doctor时,它会使用得到的Employee来构造其基础id实例。构造函数主体用于除简单的成员初始化以外的更复杂的代码。


PS:

  1. 您可能想用title_而不是std::move(title)初始化title成员,有关详细信息,请参见this question
  2. 当构造函数具有两个或三个以上具有兼容类型的参数时,这会造成混淆-用户很可能将它们彼此混淆。您可以考虑大多数字段的默认值,并在构造后进行设置,或者使用构建器模式进行设置。
  3. 地址,带有两个d,而不是地址。
  4. 除非您计划就地编辑char*字段,否则请使用const char *
  5. 以这种方式,您可以编写类,Doctor方法不会向Employee方法写入访问权限;确保这就是您想要的。
  6. 我还有其他一些蠢货,但我现在就停下来...