selecting an object to use a command on from console

im trying to make a game and i was wondering if you can use shared functions on multiple objects from console.

like i have four classes :warrior,thief,mage,archer

each one has a separate class defining their stats and etc
im curious if you can dynimically use a function on either of those objects
like if i selected warrior in the beginning all the function commands will apply to the warrior object.

instead of having a ton of these lines below in the code

1
2
3
4
warrior.addSTR(number);
thief.addLUK(number);
mage.addINT(number);
archer.addDEX(number);


inside of a conditional statement wrapping those commands
Not necessarily.

All these objects have things in common. What you can do is define a class from which these objects inherit from and share the same interface. Then you define a pointer to a character, and use the characters interface to set the variables that the more specialized class uses.

consider reading the section on inheritance:
http://www.cplusplus.com/doc/tutorial/inheritance/
at the moment they all derived from a class called Character which they do share the stat variables which are initialized when the warrior or etc objects are created. the thing is i have other functions that manipulate the stats in characters(warrior and etc) objects but im trying to avoid making 4 sets of them each for example
1
2
3
4
warrior.addSTR(number);
thief.addSTR(number);
mage.addSTR(number);
archer.addSTR(number);


EDIT: more or less im wondering if there is a universal way to have addSTR change the STR stat in either of the 4 objects derived from class Character
Last edited on
You would pass a character pointer to the function which takes a character class pointer, and the method acts ON THE CHARACTER CLASS, and not on the specific player archetype.

The only thing that the specific player types do is act specifically on actions that are relative only to that archetype. For example, a Mage isn't going to use an axe, but the warrior will. But each will attack, and by overloading the attack function which is virtual in the character base class, each specific player type can behave specifically without much modification to the underlying code. Make sense?
Last edited on
Topic archived. No new replies allowed.