I have got the code to work using polymorphism, so I thought I would give an array of human fighter ships a go. The code complies, prints then says test.exe has stopped working, and I cannot figure out why. Can you see anything from the code below?
Code:
// Spaceship.h
#ifndef SPACESHIP_H
#define SPACESHIP_H
#include <iostream>
#include <string>
#include "Vector3.h"
using namespace std;
class Spaceship
{
public:
Spaceship();
Spaceship(
const string& name,
const Vector3& pos,
const Vector3& vel,
int fuel,
int damage);
virtual void printStats();
protected:
string mName;
Vector3 mPosition;
Vector3 mVelocity;
int mFuelLevel;
int mDamage;
};
class HumanShip : public Spaceship
{
public:
HumanShip(
const string& name,
const Vector3& pos,
const Vector3& vel,
int fuel,
int damage);
};
class HumanFighterShip : public HumanShip
{
public:
HumanFighterShip(
const string& name,
const Vector3& pos,
const Vector3& vel,
int fuel,
int damage,
int type,
int numMissles);
void printStats();
private:
int mType;
int mNumMissles;
};
#endif
Code:
// Main.cpp
#include <iostream>
#include "SpaceShip.h"
#include "Vector3.h"
using namespace std;
int main()
{
Spaceship* fighterHuman[5];
fighterHuman[0] = new HumanFighterShip("Human Fighter Class 1", Vector3(5.0f, 6.0f, -3.0f),
Vector3(1.0f, 2.0f, 3.0f),100, 0, 1, 10);
fighterHuman[1] = new HumanFighterShip("Human Fighter Class 2", Vector3(5.0f, 6.0f, -3.0f),
Vector3(1.0f, 2.0f, 3.0f),100, 0, 2, 10);
fighterHuman[2] = new HumanFighterShip("Human Fighter Class 3", Vector3(5.0f, 6.0f, -3.0f),
Vector3(1.0f, 2.0f, 3.0f),100, 0, 3, 10);
fighterHuman[3] = new HumanFighterShip("Human Fighter Class 4", Vector3(5.0f, 6.0f, -3.0f),
Vector3(1.0f, 2.0f, 3.0f),100, 0, 4, 10);
fighterHuman[4] = new HumanFighterShip("Human Fighter Class 5", Vector3(5.0f, 6.0f, -3.0f),
Vector3(1.0f, 2.0f, 3.0f),100, 0, 5, 10);
for(int i = 0; i < 5; ++i)
{
fighterHuman[i]->printStats();
cout << endl;
}
for(int i = 0; 1 < 5; ++i)
{
delete fighterHuman[i];
}
}
Code:
// Spaceship.cpp
#include "Spaceship.h"
#include <iostream>
#include "Vector3.h"
using namespace std;
Spaceship::Spaceship()
{
mName = "DefaultName";
mPosition = Vector3(0.0f, 0.0f, 0.0f);
mVelocity = Vector3(0.0f, 0.0f, 0.0f);
mFuelLevel = 100;
mDamage = 0;
}
Spaceship::Spaceship(const string& name,
const Vector3& pos,
const Vector3& vel,
int fuel,
int damage)
{
mName = name;
mPosition = pos;
mVelocity = vel;
mFuelLevel = fuel;
mDamage = damage;
}
HumanShip::HumanShip(const string& name,
const Vector3& pos,
const Vector3& vel,
int fuel,
int damage)
{
mName = name;
mPosition = pos;
mVelocity = vel;
mFuelLevel = fuel;
mDamage = damage;
}
void Spaceship::printStats()
{
cout << "Name = " << mName << endl;
cout << "Position = <";
cout << mPosition.mX << ", ";
cout << mPosition.mY << ", ";
cout << mPosition.mZ << ">" << endl;
cout << "Velocity = <";
cout << mVelocity.mX << ", ";
cout << mVelocity.mY << ", ";
cout << mVelocity.mZ << ">" << endl;
cout << "Fuel Level = " << mFuelLevel << endl;
cout << "Damage = " << mDamage << endl;
}
void HumanFighterShip::printStats()
{
Spaceship::printStats();
cout << "Fighter Class = " << mType << endl;
cout << "Missles = " << mNumMissles << endl;
}
HumanFighterShip::HumanFighterShip(const string& name,
const Vector3& pos,
const Vector3& vel,
int fuel,
int damage,
int type,
int numMissles)
:HumanShip(name,pos, vel, fuel, damage)
{
mType = type;
mNumMissles = numMissles;
}
I have not included the Vector3.h and cpp file