繁体   English   中英

从子类构造函数中构造基础 class 时出现问题

[英]Problem constructing a base class from within a subclass constructor

我有2节课。 由于医生将被视为员工,我应该在医生 class 中使用员工 class 函数。 class 博士唯一的额外内容是TITLE 基本上,我尝试的是我想向医生的构造函数发送值,设置标题然后将剩余的值发送到员工的 class;但是,我不能。 这是我到目前为止所做的,

雇员.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

雇员.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;

}

医生.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

医生.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";
}

主文件

#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;
}

C++ 中的子类构造起作用,因此在执行子类的构造函数主体时,必须构造基础 class object:

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的构造函数这一事实使我们能够使用 class A方法——例如上面示例中的A::do_stuff()

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

@user4581301 提出了答案:您需要使用成员初始化程序列表 此列表上的初始化/构造在主体之前执行,并且可能包括底层 class。 我将用一个简化的例子来演示。 假设一个Employee只有一个 id 而一个Doctor只有一个额外的 title。

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时,它会使用它获得的id构建其底层Employee实例。 除了简单的成员初始化之外,构造函数主体用于更复杂的代码。


PS:

  1. 您可能希望使用std::move(title)而不仅仅是title初始化title_成员,请参阅此问题了解详细信息。
  2. 当构造函数有两个或三个以上类型兼容的参数时,这是令人困惑的——用户可能会将它们相互混淆。 您可能会考虑大多数字段的默认值并在构造后设置它们,或者使用构建器模式。
  3. 地址,有两个 d,不是地址。
  4. 除非您打算就地编辑char*字段,否则请使用const char *
  5. 他们以您编写类的方式, Doctor方法不会写入对Employee方法的访问; 确保这是您的意图。
  6. 我还有一些其他的挑剔,但我现在会停下来......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM