2

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;
}
4

1 に答える 1

10
void sickPatient::print() const
{
    Patient::print();
    cout << diagnose;
}

また、ポリモーフィックな動作が必要な場合はvirtual、基本クラスで印刷する必要があります。

class Patient
{
    char* name;
    virtual void print() const;
}

その場合、次のように書くことができます。

Patient *p = new sickPatient();
p->print(); // sickPatient::print() will be called now.
// In your case (without virtual) it would be Patient::print()
于 2012-06-27T13:02:44.597 に答える