Thread: getting a headache

  1. #16
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    ok i have a question, what are the constructor and destructor for? i mean i know it free's up memory, but whats the point wont it all be freed up when the program exits..... i guesse in a large program if you didn' need the class anymore then freeing the memory would be good.... did i just answer my own question? lol

  2. #17
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    >i guesse in a large program if you didn' need the class anymore then freeing the memory would be good

    you're absolutly right, a constructor and deconstructor are not necissary becasue you can initialize/free memory in any function you want. But they help you organize your large amount of classes..Say you make a function for clearing up memory....but you forget to call that function...then you have BIG program where you have a memory leak cus you forgot to free memory....well freeing up memory in your deconstructor means you dont have to worry about it later.

    same thing with constructors.. once you declare somthing like...eg. Cclassname classvariable; then members (variables, structs, subclasses etc..) in that class will be initialized and you dont have to seperately initliaze them like this...eg. classvaiable.number= 0;

  3. #18
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    Quote Originally Posted by Epo
    Code:
    class CPlayer
    {
    public:
    	CPlayer();
    	virtual ~CPlayer();
    
    	//Abilities - Should be initially set to 0 in constructor, and increase values as
    	//skill improves
    	int Heal, Regeneration; //etc.
    
    	//Spells - Should be initially set to false in constructor, and true during creation
    	bool KnowsFireball, KnowsFrostyball; //etc.
    	//Alternatively, make them ints, set to 0 in constructor, and increase value as
    	//skill improves
    
    	//Weapons - Should be initially set to 0 in constructor, and adjusted during creation
    	int Longsword, Shield, Armour; //Etc.
    };
    what do you mean set to 0 in the constructor? do i need to make a class for abilities also or is abilities just a function within the class CPlayer?

  4. #19
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I assume Epo intended the data members Heal and Rgeneration, which are of type int, to be what he called an Ability attribute of each CPlayer object. He intends to set the values to zero in the constructor and then presumably will increase their value during the process of a "game" as a "reward" for a CPlayer object successfully completed a variety of tasks, and decrease their value if an opponent is successful at achieving their goals.
    Code:
    //CPlayer.h
    class CPlayer
    {
    	//data member with private access
    	int Heal;
    	
    	public:
    	  //initialize data member in default constructor to 0
    	  CPlayer() : Heal(0) {}
     
    	 //provide public methods to change/retrieve value of Heal
    	  void changeHeal(int n) {Heal += n;}
    	  int getHeal() {return Heal;} const;	  
    };
    Last edited by elad; 08-26-2005 at 12:15 PM.
    You're only born perfect.

  5. #20
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    i see.... well maybe it would help if i explained exactly what this program is. What i want it to do is save all the skill selelctions in a file and create sort of a charcter file with everything in it, its not for a computer game or anything like that, just for a charcter guide, maybe that will help a little?

  6. #21
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you overload the << operator you can write all the attributes you want for each CPlayer object you want to whatever file you want.

    Overloading the >> will allow you to read the data in the file back to a CPlayer object.

    Overloading the << and >> are common topics of discussion/tutorials and I would encourage you to look into the process further on your own.

    Here's a very simple version for a simple CPlayer class.

    CAUTION: the following code has not been compiled or tested (no compiler available). You will need to test it and adjust the techniques demonstrated by it for your own use.
    Code:
    #include <iostream>
    #include <ifstream>
    
    class CPlayer
    {
       int Heal;
    
       public:
         CPlayer () : Heal(0) {};
         void changeHeal(int n) {Heal += n;}
         int getHeal() {return Heal;} const;
    
         friend ostream & operator << (ostream & os, const CPlayer & p)
         {
            os << Heal << std::endl;
            return os;
         }
    
         friend istream & operator >> (istream & is, CPlayer & p)
          {
             is >> Heal;
             return is;
          }
    };
    
    
    int main()
    {
       //use default constructor to declare CPlayer object
       CPlayer cp1;
    
       //use accessor method to display default value of Heal
       std::cout << cp1.getHeal() << std::endl;
    
       //use mutator function to change value of Heal
       cp1.changeHeal(11);
    
       //use << operator to display CPlayer, which in this case is just value of Heal
       std::cout << cp1 << std::endl;
    
       //declare ofstream
       std::ofstream fout("CPlayer1.dat");
    
       //use ofstream to write CPlayer object data to file
       fout << cp1;
    
       fout.close();
    
       //declare  ifstream
       std::ifstream fin("CPlayer1.dat");
    
       //declare second CPlayer object
       CPlayer cp2;
    
       //use fin to read into cp2.Heal directly
       fin >> cp2;
    
       //show value of cp1.Heal correctly written to and read from file
       std::cout << cp2;
    };
    You're only born perfect.

  7. #22
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    sorry elad, i dont understand your code, like i said i need to read a good tutorial on classes

  8. #23
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    hmmm.... i have read several tutorials on classes now, and still have no idea on how to go about making this program.... should i put everything into a class and then in createfinal call up all the info? i still dont know how i can save all of the choices in the skills menu grrrr this is confusing me
    edit: sorry meant to edit my last post, not make another

    edit2: that doesn't look like an embarased face lol
    Last edited by sreetvert83; 08-28-2005 at 12:25 PM.

  9. #24
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    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).
    Last edited by Epo; 08-28-2005 at 07:24 PM. Reason: Figured I'd spell words rihgt
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  10. #25
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    well, i understand how that works even though it is in c and i dont know c, but there is no way to know what selections they will make for skills, i suppose i could make a variable for every skill, set it to 0 and if they choose a skill it sets that to 1 , but i would like it to only show the skills they choose. could i make it just not print it if the skill = 0? hmmm maybe thats the solution... am i right?

  11. #26
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Sounds like it should work.
    You're only born perfect.

  12. #27
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    That's a great idea.
    Code:
    if (MyPlayer.KnowsFireball == true)
    {
    //Insert Code Here To Write to File
    }
    However, this may quickly increase the size of your source code if you need a check for every skill.

    Another attempt could be to do something like:
    Code:
    definitions.h
    #define MAX_NUM_SKILLS 100
    
    #define FIREBALL	100
    #define SNOW_EXPLOSION	101
    #define NOVA		102
    //etc...
    Then, using an array, vector, or linked list, keep track of what the user knows (example done with array):
    Code:
    cPlayer.h
    #include "definitions.h"
    ...
    class cPlayer
    {
    public:
    ...
    	//FUNCTIONS
    	void AddSkill(int SkillID);
    	void WriteMyStatsToFile(void);
    
    private:
    	//FUNCTIONS
    	char* ConvertSkillToString(int SkillID);
    
    	//VARIABLES
    	int Skills[MAX_NUM_SKILLS]; //Player can have maximum of 100 skills in this example
    ...
    };
    Code:
    cPlayer.cpp
    //Initializes all Skill IDs in the array to 0
    void cPlayer::cPlayer() //Constructor
    {
    	int X;
    
    	for(X = 0; X<MAX_NUM_SKILLS; X++)
    	{
    		Skills[X] = 0;
    	}
    }
    ...
    //Adds a SkillID to an empty spot in the array.
    //Does nothing if array is full.
    void cPlayer::AddSkill(int SkillID)
    {
    	int X = 0;
    
    	while((Skills[X] != 0) && (X < MAX_NUM_SKILLS))
    	{
    		X++;
    	}
    
    	if(X == MAX_NUM_SKILLS)
    	{
    	//PLAYER CAN'T LEARN ANY MORE SKILLS
    	}
    	else
    	{
    		Skills[X] = SkillID;
    	}
    }
    
    //I'm unsure about whether or not this function actually works
    //Converts an integer to it's programmer defined string version
    char* cPlayer::ConvertSkillToString(int SkillID)
    {
    	switch SkillID
    	{
    	case FIREBALL:
    		return "Fireball";
    	case SNOW_EXPLOSION:
    		return "Snow Explosion";
    	case NOVA:
    		return "Nova";
    	...
    	//Insert All Other Skills Here
    	}
    
    	return NULL;
    }
    
    void cPlayer::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
    	{
    		int X = 0;
    
    		fprintf(OutPutFile, "Skills Known:\n");
    
    		while((Skills[X] != 0) && (X < NAX_NUM_SKILLS))
    		{
    			fprintf(OutPutFile, "%s\n", ConvertSkillToString(Skills[X]));
    		}
    		
    		printf("Success\n");
    	}
    	else
    	{
    		printf("Could Not Open File\n");
    	}
    }
    ...
    And, the usage of said functions would look like:
    Code:
    main.cpp
    
    #include "cPlayer.h"
    
    int main(void)
    {
    	cPlayer MyPlayer;
    
    	MyPlayer.AddSkill(FIREBALL);
    	MyPlayer.WriteMyStatsToFile();
    
    	return 0;
    }
    Code:
    Expected output in PlayerStats.txt
    Skills Known:
    Fireball
    You could also just plain leave out the conversion when writing to a file (write the integer code directly), because as long as you know the definitions in definitions.h, you know which integer stands for which skill.

    To Sum Up
    You store the ID numbers of all known skills in an array (if you want to keep a set amount of skills), linked list (to have an infinitely long amount of skills with efficient memory usage), array (infinitely long amount of skills with possible memory being wasted, but that's another topic). Then, when writing to your file, you merely convert the ID numbers (or just write the ID numbers) to the file to keep track of which skills are known. Anything not written, well, isn't known.
    Last edited by Epo; 08-29-2005 at 04:19 PM. Reason: Added comments, main.cpp, and expected results
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  13. #28
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    know whats funny? i have all these dif. methods of doing what i want, but when it comes down to actually making it work in my source file i just cant make it stick.... maybe i been working too long at this and need a break, that usually works..... grrrrr school tomorrow.... only thing im looking forward to is my java class (there are no other programming classes in my school ) ah well ill keep working on it...

    EDIT: oh! and i dont know how to make my own header files, only reason im bringing this up is because it would be easy just to make the class and include it in my source, or should i just put it in one cpp file?
    Last edited by sreetvert83; 08-29-2005 at 06:46 PM.

  14. #29
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    epo can you elaborate? im not very familiar with vectors and links lists, anyway this is the class i was starting, is this the right way to go about this?
    Code:
    #define Cplayer
    class Cplayer
          {
                 public:
    
    /*---------------------------------------------------------------------------------
     get*/                    
                        //weaponskills
                         int getDagger();
                         int getClub();
                         int getThrown();
                         int getShield();
                         int getUnarmed();
                         int getShortSword();
                         int getStaff();
                         int getAmbidextrous();
                         int getArchery();
                         int getAxe();
                         int getLongSword();
                         int getTwoHandedWeapons();
                         int getStrong();
                         int getSap();
                         int getParry();
                         int getSneakAttack();
                         int getFavoredEnemy();
                         int getRiposte();
                         int getExtraDamage();
                         int getSlay();
    
    /*---------------------------------------------------------------------------------
     set*/                     
                         void setDagger();
                         void setClub();
                         void setThrown();
                         void setShield();
                         void setUnarmed();
                         void setShortSword();
                         void setStaff();
                         void setAmbidextrous();
                         void setArchery();
                         void setAxe();
                         void setLongSword();
                         void setTwoHandedWeapons();
                         void setStrong();
                         void setSap();
                         void setParry();
                         void setSneakAttack();
                         void setFavoredEnemy();
                         void setRiposte();
                         void setExtraDamage();
                         void setSlay();
     
     /*---------------------------------------------------------------------------------
     get*/                    
                         //magic skills
                         int getBardicLearning();
                         int getDruidLearning();
                         int getPaladinLearning();
                         int getRangerLearning();
                         int getSorcererLearning();
                         int getWizardLearning();
                         int getManaPoints();
                         int getRead Magic();
                         int getRitual();
                         int getMass Heal();
                         int getTurn/Rebuke Undead();
                         int getInspire Courage();
                         int getWild Shape();
                         int getEmpty Body();
                         int getDetect Evil/Good();
                         int getLay on Hands();
                         //single spells
                         int getsingleSpelllvlonetothree();
                         int getsingleSpelllvlfourtosix();
                         int getsingleSpelllvlseventonine();
                         int getsingleSpelllvlten();
                         //full spell level
                         int getSpelllvlonetothree();
                         int getSpelllvlfourtosix();
                         int getSpelllvlseventonine();
                         int getSpelllvlten();
    
    /*---------------------------------------------------------------------------------
     set*/                    
                         void setBardicLearning();
                         void setDruidLearning();
                         void setPaladinLearning();
                         void setRangerLearning();
                         void setSorcererLearning();
                         void setWizardLearning();
                         void setManaPoints();
                         void setRead Magic();
                         void setRitual();
                         void setMass Heal();
                         void setTurn/Rebuke Undead();
                         void setInspire Courage();
                         void setWild Shape();
                         void setEmpty Body();
                         void setDetect Evil/Good();
                         void setLay on Hands();
                         //single spells
                         void setsingleSpelllvlonetothree();
                         void setsingleSpelllvlfourtosix();
                         void setsingleSpelllvlseventonine();
                         void setsingleSpelllvlten();
                         //full spell level
                         void setSpelllvlonetothree();
                         void setSpelllvlfourtosix();
                         void setSpelllvlseventonine();
                         void setSpelllvlten();
                         
    /*---------------------------------------------------------------------------------
     get*/                     
                         //special abilitys
         
                         
                 private:
                         //weapon skills
                         int Dagger = 0;
                         int Club = 0;
                         int Thrown = 0;
                         int Shield = 0;
                         int Unarmed = 0;
                         int ShortSword = 0;
                         int Staff = 0;
                         int Ambidextrous = 0;
                         int Archery = 0;
                         int Axe = 0;
                         int LongSword = 0;
                         int TwoHandedWeapons = 0;
                         int Strong = 0;
                         int Sap = 0;
                         int Parry = 0;
                         int SneakAttack = 0;
                         int FavoredEnemy = 0;
                         int Riposte = 0;
                         int ExtraDamage = 0;
                         int Slay = 0;
                        
                         //magic skills
                        
                         int BardicLearning = 0;
                         int DruidLearning = 0;
                         int PaladinLearning = 0;
                         int RangerLearning = 0;
                         int SorcererLearning = 0;
                         int WizardLearning = 0;
                         int ManaPoints = 0;
                         int Read Magic = 0;
                         int Ritual = 0;
                         int Mass Heal = 0;
                         int Turn/Rebuke Undead = 0;
                         int Inspire Courage = 0;
                         int Wild Shape = 0;
                         int Empty Body = 0;
                         int Detect Evil/Good = 0;
                         int Lay on Hands = 0;
                         //single spells
                         int singleSpelllvlonetothree = 0;
                         int singleSpelllvlfourtosix = 0;
                         int singleSpelllvlseventonine = 0;
                         int singleSpelllvlten = 0;
                         //full spell level
                         int Spelllvlonetothree = 0;
                         int Spelllvlfourtosix = 0;
                         int Spelllvlseventonine = 0;
                         int Spelllvlten = 0;
                         //special abilitys
           }
    but, as you can see thats a lot of skills and will GREATLY increase my source size, so i should read up on vectors, or linked lists? or something like that?
    Last edited by sreetvert83; 09-10-2005 at 06:55 AM.
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  15. #30
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    It's a good start

    Just something quick that I noticed. In these examples, and a few other places:
    Code:
    int Mass Heal = 0;
    int Turn/Rebuke Undead = 0;
    These will cause your compiler to throw errors at you.

    Spaces and, as far as I know, slashes (forward or backward) are not permitted in variable names. It's usually just convention to stick with letters, numbers, and underscores. I'm not sure about the slash (if underscores are allowed, why not), but just a heads up.

    Along the same lines (in your Class Definition):
    private:
    //weapon skills
    int Dagger = 0;
    You're not allowed to initialize variables within your actual class. It's best to stick these kinds of things into the constructor.

    Now, I'm not saying what I show you here is the right way to do it. You've got a method there that will work. However, to perhaps reduce the size of your class, something along the lines of this may be handy...

    Code:
    //MyDefinitions.cpp
    #define DAGGER		100
    #define CLUB		101
    #define THROWN		102
    //Define the rest of your Skills/Spells
    Code:
    //cPlayer.h
    #include "MyDefinitions.cpp"
    
    class cPlayer
    {
    public:
    	cPlayer(void); //The Constructor
    	int GetValue(int ID); //This is only possible because I noticed all 
    		your get...() functions returned the same type
    
    private:
    	int DaggerSkill;
    	int ClubSkill;
    	int ThrownSkill;
    };
    Code:
    //cPlayer.cpp
    #include "cPlayer.h"
    
    cPlayer::cPlayer(void) //The Constructor
    {
    //Initializing to my own random values
    	Dagger = 10;
    	Club = 20;
    	Thrown = 30;
    }
    
    int cPlayer::GetValue(int ID)
    {
    	switch(ID)
    	{
    	case DAGGER:
    		return DaggerSkill;
    	case CLUB:
    		return ClubSkill;
    	case THROWN:
    		return ThrownSkill;
    	}
    }
    And then it would be a matter of expanding those functions to include all of your Skills and Spells:

    It's use would be as follows:
    Code:
    main.cpp
    
    #include <stdio.h> //Again, for the printf() C function
    #include "cPlayer.h"
    
    int main(void)
    {
    	cPlayer MyPlayer; //Constructor is called on creation, values are initialized
    
    	//%d represents that the programming will be inserting an integer in that position
    	printf("Dagger Skill: %d", MyPlayer.GetValue(DAGGER); //Note, DAGGER is defined as 100 in MyDefinitions.h, 
    		//and since we included cPlayer.h, we can use it here too.
    
    	printf("\n"); //\n represents a New Line
    
    	printf("Club Skill: %d", MyPlayer.GetValue(CLUB);
    
    	printf("\n");
    
    	printf("Thrown Skill: %d", MyPlayer.GetValue(THROWN);
    
    	return 0;
    }
    Expected Output:
    Dagger Skill: 10
    Club Skill: 20
    Thrown Skill: 30
    That's one path you could take that I believe would structure the code a little more nicely. I'll post one more method next, similar to the one above, that I believe would further reduce the size of your code and make it very easy to Set/Get values...
    Last edited by Epo; 09-16-2005 at 05:45 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. libusb headache
    By cbee in forum C Programming
    Replies: 2
    Last Post: 12-02-2008, 08:27 PM
  2. Headache error message
    By Strait in forum C++ Programming
    Replies: 6
    Last Post: 01-31-2005, 03:48 PM
  3. My headache
    By Morgan in forum C Programming
    Replies: 7
    Last Post: 12-03-2002, 03:33 AM
  4. Pointers + Arrays = Headache
    By Estauns in forum C++ Programming
    Replies: 7
    Last Post: 11-22-2001, 06:53 PM
  5. Linked List headache
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 09-23-2001, 06:42 PM