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!