Ok so i have my class that im making for a game in CryEngine, and i have 2 mechs and each mech is supposed to have its own stats but i cant seem to ge tthem to change, what do i do.
#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED
#include <iostream>
#include <string>
usingnamespace std;
class BaseMech //This class will be used for enemy and player mechs
{
public:
virtualvoid attack()
{
health = 5000;
Weapon_Damage = 400;
Ammo = 200;
cout << health << endl;
cout << Weapon_Damage << endl;
cout << Ammo << endl;
}
private:
size_t health; //How much health the mech has
size_t Weapon_Damage; //How much damage each bullet does
size_t Ammo; //How many missiles does the mech have
};
class WorldDestroyer: public BaseMech //All classes now inherit functions in ENEMY class
{
public:
void attack()
{
cout << "\n";
health = 10000;
Weapon_Damage = 600;
Ammo = 40000;
cout << health << endl;
cout << Weapon_Damage << endl;
cout << Ammo << endl;
}
};
class Pulverizer: public BaseMech
{
public:
void attack()
{
cout << "\n";
health = 6000;
Weapon_Damage = 200;
Ammo = 4000;
cout << health << endl;
cout << Weapon_Damage << endl;
cout << Ammo << endl;
}
};
#endif // CLASSES_H_INCLUDED
Use the protected access specifier for members that are to be accessed from derived classes.
1 2 3 4 5 6 7 8 9 10
class BaseMech //This class will be used for enemy and player mechs
{
// ...
/* private: */ protected:
size_t health; //How much health the mech has
size_t Weapon_Damage; //How much damage each bullet does
size_t Ammo; //How many missiles does the mech have
};
obj\Debug\main.o||In function `WorldDestroyer':|
virtual and inline\Classes.h|34|undefined reference to `BaseMech::BaseMech()'|
obj\Debug\main.o||In function `~WorldDestroyer':|
virtual and inline\Classes.h|34|undefined reference to `BaseMech::~BaseMech()'|
obj\Debug\main.o||In function `Pulverizer':|
virtual and inline\Classes.h|52|undefined reference to `BaseMech::BaseMech()'|
obj\Debug\main.o||In function `~Pulverizer':|
virtual and inline\Classes.h|52|undefined reference to `BaseMech::~BaseMech()'|
||=== Build finished: 4 errors, 0 warnings ===|
You don't need the default constructor, the compiler will implicitly define it for you, but you should have: virtual ~BaseMech() { }
in your BaseMech class
Ok this is my first attempt at polymorphism am i doing this ok? The virtual function will have something in them and the functions in each mech class will do something too.
that's ok but there are two problems: 1) you still don't have virtual destructor in your base class, that will cause memory leaks. 2) what's the purpose of those data members?!!! I mean why do you assign data members a constant value each call?
Every class with at least one pure virtual function is an abstract base class(i.e you can not make an instance of that class). Here I believe you are using BaseMech for that purpose so I made it abstract.
The virtuals in ravager are not needed, you can make them int, but what if you later decided to derive another class from ravager and access them through a "BaseMech *" or "Ravager *" ?
I have a suggestion for you, go to sfml-dev.org, see the documentation. Take a look at the Drawable class and the Sprite class, The Sprite class "is a" Drawable. You can view the code online.