1

I'm attempting to make a command line side scrolling shooter. However, I am struggling to get the inheritance and class/struct structure set up for the different ship types. However, there is a fair chance that I am completely misunderstanding how to go about this.

class Ship
{
int health;
float charging;
bool charged;
coords location;
bool shielded;
int chargeSpeed;
int cannons;
int shieldHealth;

struct enemyShip
{
    int speed;
    shipType type;
};

struct bossShip
{
    int Bonuspoints;
};

struct myShip
{
    int lives;
    int points;
    int isDead;
};
void createShip(shipType type, int speed, int health, bool shields, int cannons, int chargeSpeed, int bonusPoints)
{
    switch (type)
    {
    speeder:
        Ship::enemyShip newShip;
        newShip.speed = 0;

        ships.push_back(newShip);
    bruiser:
    sprayer:
    boss:

    }
}
};

That's theShip class as it stands currently. My idea was that I will be able to pass the parameters to the createShip method (from my gameloop) and that will create a new Ship of the correct type and then dump it into the list of ships (which is global).

However, whilst I can easily access the attributes of my newShip object that belong to whichever struct has been used to create it, in the case shown I can use newShip.speed after newShip = Ship::enemyShip newShip; to access these. However, I cannot work out how to access all of the attributes that it will have inherited from the parent class; such as health, charging, etc.

Any help would be appreciated, I have searched, however, most answers simply say this->x = x::x or something of that ilk, and I have tried newShip->health = 100 and that doesn't work. Consequently, either a little more detail would be appreciated or a completely different answer.

Thanks in advance!

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81

1 Answers1

2

You need to hoist (for example) enemyShip out of Ship and declare it as:

struct enemyShip : Ship
{
    int speed;
    shipType type;
};

The : Ship says that enemyShip derives from Ship.

You will then need to define createShip outside struct Ship (because inside Ship enemyShip won't be properly defined).