簡體   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