DISCLAIMER:
I'm going to be mixing C code into this example. Classes are a C++ only thing (pretty much the reason behind C++), but I'm ridiculously more comfortable with C. So instead of using "cout" and the such, prepare to see some fancy "printf" action going on. I know this is the C++ board, but if you don't let this slide, well, I can't write a good example.
Think of a class as an object. In this case, you're creating an object for your player, we'll call this object cPlayer.
Now, cPlayer has a bunch of attributes:
Health, Stamina, Mana.
These are known as member variables of our object and are used to describe our object.
But, in our case, our cPlayer can do more than just store info about itself, it can also perform actions. In this case, they are known as member functions. In the case of our cPlayer, he will be able to do three things for now:
SayHi(), ReturnMana(), WriteMyStatsToFile().
Here is a sample header file. The point of the header file is to outline what our object will look like (prototypes). The rest of the code will come in an actual .cpp file.
cPlayer.h
Code:
//This line eliminates the chance of including our header twice
#ifndef CPLAYER_H //If CPLAYER_H isn't defined yet...
//Note, if CPLAYER_H has already been defined
//by you, or by another header already, then
//nothing below will be executed until the #endif.
//It is good to keep this as unique as possible.
//Here I used the name of the actual file, all in
//capitals. Very slim chance it will have already
//been defined.
#define CPLAYER_H //Note, it's okay that we don't actually give it
//a value. We've defined it now, and that's the
//important part.
#include <stdio.h> //C header used for printf(), fscanf() functions.
//Our class for the player object
class cPlayer
{
//"public" means that the members (functions and variables) can be accessed
//from functions outside of the class. This will be more apparent in main.cpp
public:
//FUNCTIONS
cPlayer(void); //The Constructor. The Constructor is called automatically
//upon the creation of our variable. Explained more in the
//cPlayer.cpp file. This is a great place to initialize
//our member variables.
virtual ~cPlayer(void); //The Destructor. The Destructor is called automatically
//whenever our variable is being destroyed. Usually
//when the program is ending. Although if the user
//manually destroys the variable pre-emptively, this
//will still be called. The "virtual" will ensure that
//our Destructor fires, regardless of the type of exit.
//Note: Every class inherently has a Constructor and Destructor.
void SayHi(void); //A nifty function that will display a message in our console window
long ReturnMana(void); //Returns the object's current Mana (variable defined below)
void WriteMyStatsToFile(void); //Writes Health, Stamina, and Mana to a text file
//VARIABLES
long Health, Stamina; //Public variables to store our object's Health and Stamina, respectively.
//"private" means that the members (functions and variables) can NOT be
//accessed from functions outside of the class. This will be more apparent in main.cpp
private:
//FUNCTIONS
//None in this example, but private functions are useful to aid your public functions.
//If there's a function you need to call from within a public function, but will never
//be necessary to give the programmer to call it whenever they like, then you would
//define it as "private".
//VARIABLES
long Mana;
};
//Note: As of now, none of our variables are actually initialized
#endif
There is the prototype for our player object. Now, onto the actual definitions.
cPlayer.cpp
Code:
#include "cPlayer.h" //Necessary so our compiler knows which prototypes
//we're talking about
//Note: The generic way to declare a function for a class is as follows
//ReturnValue ClassName::FunctionName(Parameters)
//Our CONSTRUCTOR
cPlayer::cPlayer(void)
{
Health = 0;
Stamina = 0;
Mana = 0;
}
//Our DESTRUCTOR
virtual cPlayer::~cPlayer(void)
{
//There is nothing to clean up, so this can
//stay empty. If you were to allocate memory
//through malloc(), new, or anything else, for
//your class, this would be a great place to
//clean it up.
}
void cPlayer::SayHi(void)
{
printf("Hello, this is your Class talking\n");
}
//Sometimes we'll need to know what our Mana is. However, since that
//is a private variable, we can not access it. Therefore we use
//our public class to return the contents of Mana. Another public
//class would be necessary I.e. ChangeMana(long Value) to
//change the value of Mana to "Value". But I'll leave that up to you.
long cPlayer::ReturnMana(void)
{
return Mana; //Return our Mana value
}
//This function writes all the current variables (public and private) which
//describe the player into a text file.
void WriteMyStatsToFile(void)
{
//Note: As mentioned this is C code. I do not know the C++ equivalent.
FILE *OutputFile; //A pointer to a FILE type variable.
//FILE *fopen(const char *FileName, const char *Mode);
OutputFile = fopen("PlayerStats.txt", "w"); //"w" denotes "write"
if(OutputFile != NULL) //Make sure fopen() succeeded
{
fprintf(OutputFile, "Health: %d\n Stamina: %d\n Mana: %d", Health, Stamina, Mana);
printf("Success\n");
}
else
{
printf("Could Not Open File\n");
}
}
And finally, an example of how to use our class:
main.cpp
Code:
#include "cPlayer.h"//Quotations are used when including user-created headers.
//<> signs are used when including official headers.
int main(void)
{
cPlayer MyPlayer; //Upon this creation, the Constructor is called.
MyPlayer.SayHi();
printf("Current Mana: &d", MyPlayer.ReturnMana());
//Note: MyPlayer.Mana is not valid because it is a private variable
//and can NOT be accessed outside of member functions.
MyPlayer.Health = 100; //This is okay because Health is a public
//variable and CAN be accessed outside of member functions
MyPlayer.WriteMyStatsToFile();
return 0;
}
Expected Output in Console Window
Code:
Hello, this is your Class talking
Current Mana: 0
Success
Expected Output in PlayerStats.txt
Code:
Health: 100
Stamina: 0
Mana: 0
DISCLAIMER 2:
This is not the best generic tutorial on classes. It is aimed more towards solving this specific problem. While it does show the basic uses/implementation of a class, there are many more things you can do with classes and further reading should be done to fully understand classes.
DISCLAIMER 3:
This code has not been tested.
If you have any questions about this, feel free to just fire away. If I find anything I've missed since posting I'll edit and inform you of it. I recommend copying/pasting the code into your standard programming environment. It may be easier to read. And I know that it looks like there's a lot to read, well, that's cause there is. I did my best to comment it all, and that's the biggest bulk of it. Code-wise, it's actually relatively small.
I believe I've covered and said everything I've wanted to. If you read it all from the start, it should be appropriately explained as you travel through the example. Any feedback from the community would be great, especially if you catch an error. (I wrote this in one sitting).