@
JeremyBenson11
Just so you know, C++ system header files such as string has the angle brackets, and no .h extension:
#include <string>
Header files that one creates themselves are in double quotes, and normally does have an extension:
#include "character.h"
The double quotes tell the compiler to look for the file in a short list of possible locations (starting with the current directory), while the angle brackets tells it to look in standard locations for system include files.
Perhaps
character
is not a good name for that file and it's class. Sure it's a character in the game, but I think you could come up with a more meaningful name that that.
A convention that I personally like, is to name cpp header files with the .hpp extension: it means a header file with cpp code inside, as opposed to .h which may be implied as meaning C code. It's just a convention, a lot of people don't do it, but I find it meaningful.
There is another way of setting member variables initially: use a constructor. The purpose of a constructor is exactly that. Keep your existing constructor, add another which takes the arguments which will be assigned to all the member variables. Rather that assign the values, use a member initalization list:
1 2 3 4 5 6
|
character::character(const std::string& nameArg)
: // colon introduces member initalization list
name(nameArg) // direct initialization, if there are more than 1 variable separate them by commas, put 1 per line
{
// do validation here, if required
}
|
The reason for doing this is here:
https://isocpp.org/wiki/faq/ctors#init-lists
Another concept is when to have get and set functions. One should try to avoid falling into blindly making get and set functions for every member variable. One only need set functions if the values are going to change after construction. Even then it may be possible to have one function that sets several tightly related member variables. For example, say we have a class which represents a Triangle shape. Instead of having a variable for each vertex and functions to set and get each one (6 functions in all), have an array of 3 points, then a function which takes a Point and a position in the array to set it, another to do the same for getting a value. Also one that sets all 3 at once, and its corresponding accessor function (4 functions in all).
Good Luck !!