Search code examples
c++inheritancevectorradix

How to check which objects in vector in C++


Well, Hospital is the class which has vector of patients. FemaleIn, FemaleOut, MaleIn, MaleOut are derived classes from patients(Base class). Those classes have toString function(method). What I am trying to do is, in display method in Hospital class, I just want to display only in case of Outpatient which is parent class of FemaleOut and Maleout or Inpatient which is parent class of FemaleIn and MaleIn. From what I am thinking to call only specific method from, for example, Outpatient, I will have to know which objects in which index of vector for automatically. Is there any idea to call only toString for specific class which is, for example, FemaleIn and MaleIn where parent class is Inpatient. Thank you for your any help or suggestion.

void Hospital::determinePatientType()
{
    int selection;
    cout << endl;
    cout << "What is the patient type?" << endl;
    cout << "1. Female Inpatient" << endl;
    cout << "2. Female Outpatient" << endl;
    cout << "3. Male Inpatient" << endl;
    cout << "4. Male Outpatient" << endl;
    cout << endl;
    cin >> selection;

    switch(selection)
    {
    case 1:
        patients.push_back(new FemaleIn());
        cout << patients.back() << endl;
        patients[totalPatients]->enterPatientData();
        totalPatients++;
        break;
    case 2:
        patients.push_back(new FemaleOut());
        cout << patients.back() << endl;
        patients[totalPatients]->enterPatientData();
        totalPatients++;
        break;
    case 3:
        patients.push_back(new MaleIn());
        cout << patients.back() << endl;
        patients[totalPatients]->enterPatientData();
        totalPatients++;
        break;
    case 4:
        patients.push_back(new MaleOut());
        cout << patients.back() << endl;
        patients[totalPatients]->enterPatientData();
        totalPatients++;
        break;
    default:
        return;
    }

}

void Hospital::display(string type)
{


        cout << "Patient Name     Spouse Name    Sex    Patient Type    Unit/Appt. Date    Diagnosis" << endl;
        cout << "===================================================================================" << endl;

        if(type=="All")
        {
            for(int i=0;i<patients.size();i++)
            {
                patients[i]->toString();
            }

        }
        else if(type=="Outpatient")
        {
            for(int i=0;i<patients.size();i++)
            {
                patients[i]->toString();
            }
        }
        else
        {
            for(int i=0;i<patients.size();i++)
            {
                patients[i]->toString();
            }
        }

}

Solution

  • I think this question might be similar to How do I check if an object's type is a particular subclass in C++? .

    I would propose something like:

    Class Patient{
        virtual bool display(string filter);
    };
    Class OutPatient : Patient {
        bool display(string filter) override;
    };
    bool OutPatient::display(string filter) {
        if (filter != "OutPatient")
            return false;
        //Do stuff
        return true;
    }
    Class InPatient : Patient {
        bool display(string filter) override;
    };
    // You could just make this the default definition on display on Patient
    bool InPatient::display(string filter) {
        return false;
    }
    

    And then:

    void Hospital::display(string type)
    {
        for(auto& patient: patients)
            patient->display(type);
    }