-2

私は2つのクラスを持っています。Doctor は Employee と見なされるため、Doctor クラスで Employee クラス関数を使用する必要があります。Doctor クラスにある唯一の余分なものはTITLEです。基本的に、私が試したのは、ドクターのコンストラクターに値を送信し、タイトルを設定してから残りの値を従業員のクラスに送信することでしたが、できませんでした。これは私がこれまでやってきたことです。

従業員.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;

}

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

ドクター.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";
}

メイン.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;
}
4

1 に答える 1

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クラスのメソッドを使用できるようにするものです。AA::do_stuff()

しかし、コンストラクターの本体のに別のコンストラクターを指定するにはどうすればよいBでしょうか。Employeeまたは、あなたの場合、コンストラクターの本体の前に適切なコンストラクターをどのように使用できますDoctorか?

答えは @ user4581301 によって提案されました: You need to use an member initializer list。このリストの初期化/構築は本体のに実行され、基礎となるクラスが含まれる場合があります。簡単な例で説明します。anEmployeeには id のみがあり、 aDoctorには追加のタイトルがあるとします。

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

したがって、 aDoctorが構築されているとき、取得Employeeした を使用してその基礎となるインスタンスを構築しますid。コンストラクター本体は、単純なメンバーの初期化を超える複雑なコードに使用されます。


PS:

  1. だけでなく、title_でメンバーを初期化することもできます。詳細については、この質問を参照してください。std::move(title)title
  2. コンストラクターに互換性のある型を持つパラメーターが 2 つまたは 3 つ以上あると混乱します。ユーザーはそれらを互いに混同する可能性があります。ほとんどのフィールドのデフォルト値を検討し、構築後にそれらを設定するか、代わりにビルダー パターンを使用します。
  3. アドレスではなく、2 つの d を含むアドレス。
  4. フィールドをその場で編集する予定がない場合はchar*、 を使用してconst char *ください。
  5. クラスを作成した方法では、Doctorメソッドにはメソッドへの書き込みアクセスがありませんEmployee。それが意図したものであることを確認してください。
  6. 他にもネタバレありますがやめておきます…
于 2020-04-16T22:21:44.997 に答える