静态类错误

时间:2016-11-01 03:35:56

标签: c++ oop polymorphism

这是它的C ++。

我真的不知道这个问题因为两个静态演员都是同样输入的,但它只显示正确的员工信息,当涉及到患者信息时它只显示日期和日期,但没有人信息,(员工)确实正确打印人员信息)

  void *print( ){
        Node *aux;
        aux = this->head;
        while(aux){
            Employee *employee = static_cast<Employee*>(aux->getPerson());
            Patient *patient = static_cast<Patient*>(aux->getPerson());
            if(employee) {
                employee->info();
            }
            else if (patient){
                patient->info();
                //Should be one of the cases above
            }
            aux = aux->getNext();
        }
        return 0;

就在我打印它的那一刻,它只打印员工信息,但不显示患者信息。

class Patient: public Person {

private:
int Id_Patient;
Person person;
string Date_In;
string Date_Out;

public:
    Patient(){
        this->Date_In;
        this->Date_Out;
    }
    Patient (int Id_Patient, Person person){
        this->Id_Patient=Id_Patient;
        this->person=person;
    }
    Patient (int Id_Patient, Person person, string Date_In, string Date_Out){
        this->Id_Patient=Id_Patient;
        this->Date_Out=Date_Out;
        this->Date_In=Date_In;
    }
    void setId_Patient(int Id_Patient){
        this->Id_Patient=Id_Patient;}
    int getId_Patient(){
        return this->Id_Patient;}

    void setDate_In(string Date_In){
        this->Date_In=Date_In;}
    string getDate_In(){
        return this->Date_In;}

    void setDate_Out(string Date_Out){
        this->Date_Out=Date_Out;}
    string getDate_Out(){
        return this->Date_Out;} 

    void setPerson(Person person) {
        this->person=person;    }       
    Person getPerson() {
        return this->person;}       

    void info() {
        cout <<"=================================" << endl; 
        cout << "Patient ID: " << this->Id_Patient << endl;
        this->person.info();
        cout << "Date_In: " << this->Date_In << endl;
        cout << "Date_Out: "<< this->Date_Out << endl;
        cout <<"=================================" << endl; 
    }               

}; // clase patient

class Employee: public Person {
private:
    int Employee_Code;
    Person person;
    double Salary;
public:
    Employee() {
        this->Employee_Code;
        this->Salary;}

    Employee(int Employee_Code, Person person){
        this->Employee_Code=Employee_Code;
        this->person=person;
    }
    Employee(int Employee_Code, double Salary){
        this->Employee_Code=Employee_Code;
        this->Salary=Salary;
    }
    Employee(int Employee_Code, Person person, double Salary){
        this->Employee_Code=Employee_Code;
        this->person=person;
        this->Salary=Salary;
    }   
    void setEmployee_Code(int Employee_Code){
        this->Employee_Code=Employee_Code;}
    int getEmployee_Code(){
        return this->Employee_Code;}

    void setSalary(double Salary){
        this->Salary=Salary;}
    double getSalary(){
        return this->Salary;}

    void setPerson(Person person) {
        this->person=person;    }       
    Person getPerson() {
        return this->person;}   

    void info() {
        cout <<"=================================" << endl; 
        cout << "Employee_Code: " << this->Employee_Code << endl;
        this->person.info();
        cout << "Salary: " << this->Salary << endl;
        cout <<"=================================" << endl; 
    }

}; // clase employee

1 个答案:

答案 0 :(得分:1)

看来OP已将static_castdynamic_cast混为一谈。但是有一种更好的方法可以做到这一点,无需任何转换:将纯虚方法virtual void info()=0;添加到Person

PatientEmployee&#39; info将实施Person infoprint可以简单地< / p>

aux->getPerson()->info();

全部完成。

关闭主题,从void *返回print有什么意义? void *是想象力,糟糕计划或C ++中C库的接口失败。你几乎不应该使用它,当然也不要使用return 0