I am part way through a course, and we have just finished overloading a printstats game example. It then says this is not an ideal method, and polymorphism is better. Thing is, even though it talks about polymorphism, it does not give you a working example for the console output game we created. I have read almost every article on google, and still cannot get my head around polymorphism. Could someone please amend the code below, so the stats are printed out using polymorphism. The Vector3.h, and Vector3.cpp, are old reusable codes. I need it to also print out the number of missles the fighter ship has. There is alot more classes involved, but I have reduced the code for brevity. I just think that if I can see polymorphism working on this, I might get a better understanding of it. This is not a project, this is something I am working on.

Code:

// Vector3.h #ifndef VECTOR3_H #define VECTOR3_H #include <iostream> class Vector3 { public: Vector3(); Vector3(float coords[3]); Vector3(float x, float y, float z); bool equals(const Vector3& rhs); Vector3 add(const Vector3& rhs); Vector3 sub(const Vector3& rhs); Vector3 mul(float scaler); float length(); void normalize(); float dot(const Vector3& rhs); float* toFloatArray(); void print(); void input(); float mX; float mY; float mZ; }; #endif

Code:

/ Vector3.cpp #include <iostream> #include <cmath> #include "Vector3.h" using namespace std; Vector3::Vector3() { mX = 0.0f; mY = 0.0f; mZ = 0.0f; } Vector3::Vector3(float coords[3]) { mX = coords[0]; mY = coords[1]; mZ = coords[2]; } Vector3::Vector3(float x, float y, float z) { mX = x; mY = y; mZ = z; } bool Vector3::equals(const Vector3& rhs) { return mX == rhs.mX && mY == rhs.mY && mZ == rhs.mZ; } Vector3 Vector3::add(const Vector3& rhs) { Vector3 sum; sum.mX = mX + rhs.mX; sum.mY = mY + rhs.mY; sum.mZ = mZ + rhs.mZ; return sum; } Vector3 Vector3::sub(const Vector3& rhs) { Vector3 dif; dif.mX = mX - rhs.mX; dif.mY = mY - rhs.mY; dif.mZ = mZ - rhs.mZ; return dif; } Vector3 Vector3::mul(float scaler) { Vector3 p; p.mX = mX * scaler; p.mY = mY * scaler; p.mZ = mZ * scaler; return p; } float Vector3::length() { return sqrtf(mX*mX + mY*mY + mZ*mZ); } void Vector3::normalize() { float len = length(); mX /= len; mY /= len; mZ /= len; } float Vector3::dot(const Vector3& rhs) { float dotP = mX*rhs.mX + mY*rhs.mY + mZ*rhs.mZ; return dotP; } float* Vector3::toFloatArray() { return &mX; } void Vector3::print() { cout << "<" << mX << ", " << mY << ", " << mZ << "> \n"; } void Vector3::input() { cout << "Enter x: "; cin >> mX; cout << "Enter y: "; cin >> mY; cout << "Enter z: "; cin >> mZ; }

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); void landed(); void attacking(); 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 numMissles); void fireLaserGun(); void fireMissle(); private: int mNumMissles; }; #endif

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::landed() { cout << mName <<" has landed" << endl; } void Spaceship::attacking() { cout << mName << " is attacking" << endl; } 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; } HumanFighterShip::HumanFighterShip(const string& name, const Vector3& pos, const Vector3& vel, int fuel, int damage, int numMissles) :HumanShip(name,pos, vel, fuel, damage) { mNumMissles = numMissles; } void HumanFighterShip::fireLaserGun() { cout << "Firing " << mName << " laser guns." << endl; } void HumanFighterShip::fireMissle() { if(mNumMissles > 0) { cout << "Firing " << mName << " missles." << endl; mNumMissles--; } else cout << "Out of missles." << endl; }

Code:

// Main.cpp #include <iostream> #include "SpaceShip.h" #include "Vector3.h" using namespace std; int main() { HumanFighterShip fighter("Human Fighter", Vector3(5.0f, 6.0f, -3.0f), Vector3(1.0f, 2.0f, 3.0f),100, 0, 10); fighter.printStats(); cout << endl; fighter.attacking(); fighter.fireLaserGun(); fighter.fireMissle(); }