简体   繁体   English

访问派生类C ++中的受保护成员

[英]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. 这是我的代码,此函数在FemaleIn类中,该类派生自Female,但Female也派生于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. 作为参考,我正在使用vector根据患者类型存储患者对象。 Like this std::vector patients 像这样的std :: vector患者

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. 仅根据您提供的代码,似乎没有为char *成员变量分配任何内存以存储字符串。 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). 将每个受保护的成员变量的类型从char *更改为std::string现在您可以从标准输入中读取字符串并将其存储在每个成员变量中,并且std::string对象将处理所有必要的内存根据需要进行分配(以及在不再使用它时清理它)。 You should then be able to use your function FemaleIn::enterPatientData as is because the syntax is correct. 然后,您应该能够按FemaleIn::enterPatientData使用函数FemaleIn::enterPatientData ,因为语法正确。

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 ). 您可能还需要重新考虑如何存储某些类型的变量(例如, age可能更好地存储为int )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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