【发布时间】:2018-03-18 17:23:53
【问题描述】:
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;
}
这是我的代码,这个函数在 FemaleIn 类中,该类派生自女性,但女性也派生自患者。我想做的是我想在病人类中使用受保护的成员。没有错误,但是当我运行程序时,它被吓坏了。作为参考,我使用向量来根据患者类型存储患者对象。像这样的 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;
};
我的问题是如何将派生类中的每个值存储到基类(患者)中的成员变量?
【问题讨论】:
-
女性来源于Patient?我已经有点担心你的等级制度了..
-
@miradulo 是的。它是从患者类派生的。这就像关系 A-B-C Patient-Female-FemaleIn
-
顺便说一句,你不需要
this->语法,直接访问成员。
标签: c++ class inheritance member