C++ 基本派生类成员重写。挑战活动11.3

C++ 基本派生类成员重写。挑战活动11.3,c++,C++,我对学校提出了挑战 为类PetData定义一个成员函数PrintAll(),该函数按如下方式打印输出。提示:使用基类的PrintAll()函数。 姓名:Fluffy,年龄:5岁,身份证号:4444 然后他们给了我下面的代码和一个我可以修改的小片段。之间 //修复:添加PrintAll()成员函数 和 /*您的解决方案位于此处*/ 是我添加的代码,但我没有得到正确的结果 MyCode //FIXME:Add PrintAll()成员函数 void PrintAll(){ 动物数据; data.Pr

我对学校提出了挑战

类PetData
定义一个成员函数
PrintAll()
,该函数按如下方式打印输出。提示:使用基类的
PrintAll()
函数。
姓名:Fluffy,年龄:5岁,身份证号:4444

然后他们给了我下面的代码和一个我可以修改的小片段。之间
//修复:添加PrintAll()
成员函数 和
/*您的解决方案位于此处*/
是我添加的代码,但我没有得到正确的结果

MyCode

//FIXME:Add PrintAll()成员函数
void PrintAll(){
动物数据;
data.PrintAll();

您似乎对如何在PetData中调用超类AnimalData的方法有一点误解:

void PrintAll(){
      AnimalData data;
      data.PrintAll();
      cout << ", ID: " << idNum;
      }

为什么不能简单地删除
数据
如果删除要在上调用
PrintAll
的对象,
data

void PrintAll(){
    PrintAll();
    cout << ", ID: " << idNum;
}
这是你的问题

void PrintAll(){
  AnimalData data;
  data.PrintAll();
  cout << ", ID: " << idNum;
  }

这比我想象的要简单得多,实际上是从PetData调用printAll方法,并附加ID num

public void printAll(){
    super.printAll();
    System.out.print(", ID: " + idNum);
}

AnimalData数据;data.PrintAll();
->
AnimalData::PrintAll();
?请让您的老师知道,他必须学习如何为函数、变量等命名。您知道,当您给人取假(如不正确)时语句作为第一件事,而不是揭穿它,人们忘记了揭穿,但只记得第一件事?(我的意思是,你首先给出了错误的无限递归解,我很确定它会被卡住)。最好把它放在答案的末尾,解释为什么这不起作用。@SergeyA同意并修正了。这是一个非常有用的解释,非常感谢
void PrintAll(){
    PrintAll();
    cout << ", ID: " << idNum;
}
#include <iostream>
#include <string>
using namespace std;

class AnimalData
{
public:
    virtual ~AnimalData() {}; // with polymorphism you must make sure the 
                              // correct destructor is always called.
                              // Derived classes will override this 
                              // destructor whether you explicitly define 
                              // them or not. 
    void SetName(string givenName)
    {
        fullName = givenName;
    }
    void SetAge(int numYears)
    {
        ageYears = numYears;
    }

    // virtual function. Derived classes can, but do not have to, replace 
    // this function with a version better suited to the derived class
    virtual void PrintAll()
    {
        cout << "Name: " << fullName;
        cout << ", Age: " << ageYears;
    }

private:
    int ageYears;
    string fullName;
};

class PetData: public AnimalData
{
public:
    void SetID(int petID)
    {
        idNum = petID;
    }

    // Replacing virtual function. Note if the base class function is 
    // virtual, then child class overrides are automatically virtual
    void PrintAll() override // override keyword notifies with a compiler 
                             // error if the function does NOT override when 
                             // it should.
    {
        AnimalData::PrintAll(); // call base class version for basic data
        cout << ", ID: " << idNum; // adding derived class-specific stuff
    }

private:
    int idNum;
};

// can add WildData here to handle wild animals.

int main()
{
    PetData userPet;

    userPet.SetName("Fluffy");
    userPet.SetAge(5);
    userPet.SetID(4444);
    userPet.PrintAll();
    cout << endl;

    // add an vanilla animal for demo
    AnimalData generic;
    generic.SetName("Fang");
    generic.SetAge(7);
    generic.PrintAll();
    cout << endl;

    // demonstrate polymorphism
    AnimalData * ptr = & generic;
    ptr->PrintAll();
    cout << endl;

    ptr = & userPet;
    ptr->PrintAll(); // runtime calls the correct PrintAll function
    cout << endl;

    return 0;
}
void PrintAll(){
  AnimalData data;
  data.PrintAll();
  cout << ", ID: " << idNum;
  }
void PrintAll(){
  AnimalData::PrintAll(); // call base class method
  cout << ", ID: " << idNum;
  }
public void printAll(){
    super.printAll();
    System.out.print(", ID: " + idNum);
}