[英]How to call a function from parent class?
I want to call an inherited function of a superclass (parent class) in C++. 我想在C ++中调用超类(父类)的继承函数。
How is this possible? 这怎么可能?
class Patient{
protected:
char* name;
public:
void print() const;
}
class sickPatient: Patient{
char* diagnose;
void print() const;
}
void Patient:print() const
{
cout << name;
}
void sickPatient::print() const
{
inherited ??? // problem
cout << diagnose;
}
void sickPatient::print() const
{
Patient::print();
cout << diagnose;
}
And also in case you want polymorphic behavior you have to make print virtual
in base class: 如果你想要多态行为,你必须在基类中创建print
virtual
:
class Patient
{
char* name;
virtual void print() const;
}
In that case you can write: 在这种情况下,你可以写:
Patient *p = new sickPatient();
p->print(); // sickPatient::print() will be called now.
// In your case (without virtual) it would be Patient::print()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.