Classes
I''ve just learned classes and I can see how they can very useful, but I had a hard time deciding what I should include in them. Should I write a segment of code first and then decide what to group in classes or should I make them from the start?
I always first decide what I would need a class for, then I decide what this new object should have to complete it''s task.
If I had an input class CInput then I would include functions like ReadKeyboard and ReadMouse, etc.
Invader X
Invader''s Realm
If I had an input class CInput then I would include functions like ReadKeyboard and ReadMouse, etc.
Invader X
Invader''s Realm
Yah... you should always decide on what all your classes will be, and what they will all do, before you write a single line of code. Breaking up a program like this will greatly simplify coding, as well as avoid unforseen flaws in your design.
Just to give an example of what I''d use classes for:
// A class to allow for the creation of a RPG-type character
// in ya .h file
class PLAYER
{
public:
PLAYER();
int GetHealth();
int GetLevel();
int GetWeapon();
void SetHealth(int);
void SetLevel();
void SetWeapon(int);
private:
int health;
int level;
int weapon;
};
// in ya .cpp file
PLAYER:LAYER()
{
health = rand()%10; // random health maximum 10
level = 1; // beginner level
weapon = 0; // check this value to a lookup table. 0 = No weapon
}
PLAYER::GetHealth() { return health; }
PLAYER::GetLevel() { return level; }
PLAYER::GetWeapon() { return weapon; }
PLAYER::SetHealth(int new_health) { health = new_health; }
PLAYER::SetLevel() { level++; } // increase level by 1
PLAYER::SetWeapon(int new_weapon) { weapon = new_weapon; }
-----------------------------
Student - "How do I make my first game?"
Lecturer - "Based on your marks from your last assessment?"
// A class to allow for the creation of a RPG-type character
// in ya .h file
class PLAYER
{
public:
PLAYER();
int GetHealth();
int GetLevel();
int GetWeapon();
void SetHealth(int);
void SetLevel();
void SetWeapon(int);
private:
int health;
int level;
int weapon;
};
// in ya .cpp file
PLAYER:LAYER()
{
health = rand()%10; // random health maximum 10
level = 1; // beginner level
weapon = 0; // check this value to a lookup table. 0 = No weapon
}
PLAYER::GetHealth() { return health; }
PLAYER::GetLevel() { return level; }
PLAYER::GetWeapon() { return weapon; }
PLAYER::SetHealth(int new_health) { health = new_health; }
PLAYER::SetLevel() { level++; } // increase level by 1
PLAYER::SetWeapon(int new_weapon) { weapon = new_weapon; }
-----------------------------
Student - "How do I make my first game?"
Lecturer - "Based on your marks from your last assessment?"
-----------------------------Empires! Visit online at http://www.daleszone.comProgramming for a funner world.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement