如何从父类调用函数?

Iva*_*nov 2 c++ oop function

我想在C++中调用超类(父类)的继承函数.
这怎么可能?

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;
}
Run Code Online (Sandbox Code Playgroud)

And*_*rew 10

void sickPatient::print() const
{
    Patient::print();
    cout << diagnose;
}
Run Code Online (Sandbox Code Playgroud)

如果你想要多态行为,你必须virtual在基类中进行打印:

class Patient
{
    char* name;
    virtual void print() const;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,你可以写:

Patient *p = new sickPatient();
p->print(); // sickPatient::print() will be called now.
// In your case (without virtual) it would be Patient::print()
Run Code Online (Sandbox Code Playgroud)