0

I have to modify my program and create a new (Gamefigures) class from which my current classes(Rabbit and Hedg) inherit. The code is a small game where two animals race until they reach the goal, but in the second task I have to make sure that it is possible that multiple iterations of these animals can race. (5 vs 5 or X vs X for instance). I am allowed to move some variables or methods to the Gamefigures class. Both of the animals use different rules to walk. How do I make a new class which creates multiple objects dynamically of the same class from which my current classes inherit?

I have tried to use the new expression to create a new object but I don't know if it is the right thing to do.

I tried:

Hedg* nHedg = new Hedg[numFigures];

Here is the rest of my code:

class Hedg: public Gamefigures
{
private:
    int salat = 1;
protected:
    int position1 = 0;
public:
    bool turn(int fields)
    {
        int counter = 10;
        if (fields < 11)//Less than 10 fields
        {
            while ((counter > 0))
            {
                if (counter < fields)//max 10 fields
                {
                    fields = counter;
                }
                position1 += fields;//walk
                counter -= fields;
                if (counter <= 0)
                {
                    salat = 0;
                }
            }
            getSalat();
            return true;
        }
        else
            return false;
    }
    Hedg()
    {
    }
    int getPosition1()
    {
        return position1;
    }
    int getSalat()
    {
        return salat = 1;
    }
    int getStock1()
    {
        return salat;
    }
    ~Hedg()
    {
    }
};

class Game :public Hedg, public Rabbit
{
private:
    int goal = 0;
    int numFields = 0;
protected:
    Rabbit theRabbit;
    Hedg theHedg;
public:
    Game();
    Game(int numFields);
    int getGoal();
    int dice();
    void doyourturn();
    bool getStand();
    ~Game();
};

Here is the error message:

Error code C4430 missing typespecifier

2
  • Please read this: How to Ask. Then edit your question accordingly. Commented May 24, 2019 at 14:16
  • 2
    You'd better use std::vector<Hedg> or std::vector<std::unique_ptr<Hedg>> and avoid raw new. Commented May 24, 2019 at 14:16

1 Answer 1

1

I think that the polymorphism is what you need for your use case and will solve your problems.

Let's suppose you have a base class for your animals:

class Animal
{
    // ...
    // Create all the (pure) virtual methods to be redefined by a derived class
    virtual void walk() = 0; // For example
};

Then you defines your two specific animals, the rabbit and the hedgehog:

class Rabbit : public Animal
{
    // ...
    // Redefine here the (pure) virtual methods of Animal for a Rabbit
    void walk() override;
};
class HedgeHog : public Animal
{
    // ...
    // Redefine here the (pure) virtual methods of Animal for a HedgeHog
    void walk override;
};

And you can use polymorphism to handle your list of animals:

std::vector<Animal*> race_competitors;
race_competitors.push_back(new Rabbit);
race_competitors.push_back(new HedgeHog);

And this way, when you will call the walk() method over a competitor, it will automatically execute the proper walking rule of the corresponding animal.

Of course, at the end of the race, don't forget to delete the content of the vector because the animals was created with new ("manually" allocated memory, on the heap).


Just for information, the Game class doesn't have to inherit from Rabbit and HedgeHog, it just has to know them as class members, or even better, store a std::vector<Animal*> as a list of competitors.

I hope it will help you to improve your design and solve your problems.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.