user3677752
user3677752

Reputation: 1

Array of derived class stored in parent class

I don't think I quite understand how to store an array of a derived class in its parent class. I keep getting errors

Error C3646 'list': unknown override specifier
Error C2065 'list': undeclared identifier

Here is the code I have

#include <iostream>
#include <string>
using namespace std;

class GameScores
{
public:
    GameEntry list[9];
    void inputList(GameEntry x);
    void sortList();
    void removeList(int r);
    void printList();
    GameScores();
};

class GameEntry :public GameScores
{
public:
    GameEntry(const string& n = "", int s = 0, const string d = "1/1/99");
    string getName() const;
    int getScore() const;
    string getDate() const;
    string setName(string n);
    int setScore(int s);
    string setDate(string d);

private:
    string name;
    int score;
    string date;
};

GameScores::GameScores()
{
    GameEntry list[9];
}

void GameScores::inputList(GameEntry x)
{
    for (int i = 0; i < 10; i++)
        if (x.getScore() >= list[i].getScore())
        {
            list[i + 1] = list[i];
            list[i] = x;
        }
}

void GameScores::sortList()
{
    GameEntry swap;

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10 - 1; j++)
        {
            if (list[j].getScore() > list[j].getScore() + 1)
            {
                swap = list[j];
                list[j] = list[j + 1];
                list[j + 1] = swap;
            }
        }
    }
}

void GameScores::removeList(int r)
{
    for (int i = r; i < 10; i++)
    list[i - 1] = list[i];

    list[9].setScore(0);
    list[9].setName(" ");
    list[9].setDate(" ");
}

void GameScores::printList()
{
    cout << "Top Scores" << endl;

    for (int i = 0; i < 10; i++)
        cout << list[i].getScore() << " " << list[i].getName() << " " << list[i].getDate() << endl;
}

GameEntry::GameEntry(const string& n, int s, const string d)    // constructor
    : name(n), score(s), date(d) { }
                                                                // accessors
string GameEntry::getName() const { return name; }
int GameEntry::getScore() const { return score; }
string GameEntry::getDate() const { return date; }

string GameEntry::setName(string n)
{
    name = n;
}

int GameEntry::setScore(int s)
{
    score = s;
}
;
string GameEntry::setDate(string d)
{
    date = d;
}

int main()
{
    GameEntry p1("John", 90, "9/9/98"), p2("Jane", 95, 8/21/98), p3("Bob", 60, "7/11/99"), p4("Jo", 92, "6/4/97");

    GameScores topScores;
    topScores.inputList(p1);
    topScores.inputList(p2);
    topScores.inputList(p3);
    topScores.inputList(p4);
    topScores.printList();

    return 0;
}

Upvotes: 0

Views: 100

Answers (2)

Paul Kienitz
Paul Kienitz

Reputation: 878

This design is very questionable. What purpose is being served by making the second class inherit the first? It looks like you'd end up with each member of the array containing an additional array with all its siblings. Don't you want only one array? You need to rethink this from an earlier point.

If you really have a reason for a parent class to contain an array of the child class, maybe you should define an interface (abstract base class) that both classes implement.

Upvotes: 1

Hatted Rooster
Hatted Rooster

Reputation: 36483

To use GameEntry as a type in your GameScores class , you must forward-declare the class like so :

class GameEntry;

class GameScores
{
public:
    GameEntry list[9];
    void inputList(GameEntry x);
    void sortList();
    void removeList(int r);
    void printList();
    GameScores();
};

Upvotes: 0

Related Questions