简体   繁体   中英

Accessing protected members in derived class C++

void FemaleIn::enterPatientData()
{
    cout << "enter name ";
    cin >> this->name;
    cout << "enter your age ";
    cin >> this->age;
    cout << "enter your diagnosis ";
    cin >> this->diagnosis;
    cout << "enter your insurance name ";
    cin >> this->insuranceName;
    cout << "enter your insurance number ";
    cin >> this->insuranceNumber;
}

This is my code and this function is in FemaleIn class which is derived from Female but Female is also derived from patient. What I am trying to do is I want to use protected members in patient class. There is no error but when I run the program, it's blusted. As a referrence, I am using vector to store patient object based on patient types. Like this std::vector patients

class FemaleIn: virtual public Female, virtual public Inpatient
{
    public:
        FemaleIn();
        void parse();
        void toString();
        void enterPatientData();

    protected:

    private:
};

class Female: virtual public Patient
{
    public:
        Female();

    protected:

    private:
};

class Patient
{
    public:
        Patient();
        virtual void parse();
        virtual void toString();
        virtual void enterPatientData();

    protected:
        char* name;
        char* SSN;
        char* insuranceName;
        char* insuranceNumber;
        char* age;
        char* spouseName;
        char* diagnosis;

};

My question is how I can store the each value from derived class to member variable in base class(patient) ??

Just based on the code you provided, it doesn't appear you allocated any memory to the char * member variables in order to store a string. If my assumption is correct, then your program is failing because it is trying to copy a string into a pointer that is not pointing to any valid memory space, which causes undefined behavior. I am going to give you the minimal, best, safest edits that you can make that would solve your problem.

class Patient
{
    public:
        Patient();
        virtual void parse();
        virtual void toString();
        virtual void enterPatientData();

    protected:
        std::string name;
        std::string SSN;
        std::string insuranceName;
        std::string insuranceNumber;
        std::string age;
        std::string spouseName;
        std::string diagnosis;

};

Change the type of each protected member variable from a char * to a std::string will now allow you to read in strings from the standard input and store them within each member variable, and the std::string object will handle all necessary memory allocation as needed (as well as cleaning it up when it is no longer being used). You should then be able to use your function FemaleIn::enterPatientData as is because the syntax is correct.

Other than that, as others have pointed out, you may want to reconsider your class hierarchy design, but that should not be a problem here. You also may want to reconsider how you store some types of variables (eg, age might be better stored as an int ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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