如何创建还包含子对象的父对象数组?

时间:2019-06-15 10:03:43

标签: c++ class inheritance

我有2节课:父母和孩子。 Child是Parent类的子级,并且其变量比其父级更多。我试图创建一个也可以包含Child对象的Parent数组,但是当尝试访问Child的变量时,它给我错误“类Parent没有名为X的成员”。 那是我的代码的示例:

class Player{
    public:
        int id;
        Persona(int id_new)
            id=id_new;
        }
};

class Goalkeeper:public Player{
    public:
        int save;
        Goalkeeper(int save_new, int id_new):Player(id_new){
            save=save_new;
        }
};

class Team{
    public:
        Player* people[2];
        void create(){
            people[0]=new Player(1);
            people[1]=new Goalkeeper(10,10);
            cout<<people[1]->save<<endl;
        }
};

错误:“ class Player”没有名为“ save”的成员

0 个答案:

没有答案
相关问题