OOP and inheritence: am I doing this right?

So I've written something sort of an intro for an RPG game in which you'd assign stat points to stats etc, but I did everything in the main file - I made every 'step' of the intro into a separate globally declared/defined function and linked them together sequentially, and that worked fine. This is how my int main() function looked like at that point:
1
2
3
4
5
6
int main(){
Intro();
Stats();
StatsOverview();
return 0;
}


However, the main file itself was cluttered with function declarations, so I put everything into separate class-files. First, I created the 'Character' class, which contains all the character-related variables (for example, AttackDamage and HP, calculated using formula which is a sum of a base value and an according stat multiplied by X) and created methods to set values for them (in which I put said formulas). Then I created the class for, specifically, the Hero character - 'Hero', which inherits from 'Character'. It has all the declarations of stat int variables that the player would input by assigning given stat points, as well as the 'Name' string, which is also being used to get an input from the player. Now I put the whole intro sequence itself into a class, 'GameIntro'. The problem that arose was that because I declared all the variables that would be inputted by the player in the 'Hero' class which is outside the scope of 'GameIntro', I had to either move them to the 'GameIntro', which I didn't want to do as the whole point is for all the variables and functions to stay in the part of the program where they belong, or make 'GameIntro' inherit from 'Hero', which is what I chose to do. I also had a function ClearScreen() that 'GameIntro' called for several times, but, again, I couldn't put it as a method in there, as it is more of a global thing that would be used in other parts of the game. Furthermore, all the classes except the first 'Character' class already inherited from others, so I created a new one, called 'GameMechanics', put ClearScreen() in there, and made 'Character' inherit from it.

This way I made it work as it did before I started the 'optimization', but I'm not sure if this is how it supposed to be or is it bad design, as the 'GameIntro' class ends up inheriting all the methods and variables from every single class, most of which are unneeded, even though some others are vital.
However, my 'Main' file ended up being quite tidy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include "GameIntro.h"

using namespace std;

int main()
{
    GameIntro Game;
    Game.Intro();
    Game.Stats();
    Game.StatsOverview();
}


So, the question is - did I do it right? Also, I am very open to any suggestions and advices, so please, feel free to speak your mind!
or make 'GameIntro' inherit from 'Hero', which is what I chose to do.

That just sounds wrong. It's saying that your game intro is a kind of character. That just makes no sense.

as the 'GameIntro' class ends up inheriting all the methods and variables from every single class, most of which are unneeded, even though some others are vital.

Definitely not right.

so I created a new one, called 'GameMechanics', put ClearScreen() in there, and made 'Character' inherit from it.

Your character is a kind of game mechanic? Really sounds wrong.

Your design just sounds like it has gone very wrong.

However, the main file itself was cluttered with function declarations, so I put everything into separate class-files.

You could have just put free functions into separate files. No need to create a God Class http://wiki.c2.com/?GodClass
Last edited on
Yeah, but how do I go about that, then? I have a class with a method that uses a variable from another class, which is, of course, outside of its scope.

The best thing I've come up with (using ClearScreen() as an example) is to put #include "GameMechanics.h" in the GameIntro header and declare a GameMechanics object, through which to call the method ClearScreen() in GameIntro class declaration. Same thing with the variables in 'Hero' class.
How about that?

**Edit: This actually seems like the solution. Now the only instance of inheritance is 'Hero' from 'Character' and everything does work properly.
Last edited on
I have a class with a method that uses a variable from another class, which is, of course, outside of its scope.


Functions take parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class One
{
  public:
  int x;
};

class Two
{
  public:
  void doSomeProcessing(One someInstance) 
  { 
    // This function, inside a class of type Two, is using the variables from an object of type One
   cout << someInstance.x;
  }
};

Last edited on
Beware of making classes just because you don't like having free functions.

Why does ClearScreen() need to be part of a class? There's nothing wrong with calling functions.
Well, the declaration of it is too big to include in Main - creates too much clutter. In my mind the 'GameMechanics' class would also have the game loop and other stuff, so it wouldn't be as useless as it is now.

Wait, 'free function' - you mean still in a separate header file, but as a regular function, without a class, right? I didn't even think of doing that - I was under an impression that only classes could be separate files, for some reason... this is very useful, thanks!
Topic archived. No new replies allowed.