Hey guys, I am trying to learn C++, I am at the point where I think I could try to make an RPG. This is probably a super easy question for all of you to answer so please do.
I would like to have more than 1 type of currency. Therefore, I was wondering if you would need a class for each type of currency?
So say I wanted 3 currency's called Silver, Gold and Diamond. Would i have to have a class for each of them?
I know It's a really beginner question, but i am a beginner. Thanks in advance
Classes for them seems like a good idea, however it also depends on how different "Silver" is from Gold or Diamond for whether or not it warrants its own class or not.
What I mean is, you would have a class called Currency or something.
There's multiple ways on how to store your data. You could store it as a string, or as the value it actually represents (ex: Gold represents 100 value, Diamond represents 500 value)
this is one option, not saying it's better or worse than others:
class Currency {
//left out other boiler plate stuff
public:
Currency(int v)
: value(v)
{}
std::string getType() const
{
if (value == 100)
{
return"Gold";
}
elseif (value == 500)
{
return"Diamond";
}
}
private:
int value;
//alternate: std::string type (either Gold, Diamond, Silver, etc)
};
Depending how how complex your Currency class is and how different "Silver" is from "Gold", you could have a base-class called Currency, and then Silver, Gold, and Diamond classes that would all inherit from your Currency class.
ex:
1 2 3 4 5 6
class Gold : public Currency {
public:
Gold()
: Currency(100) //assuming Currency has a int value constructor.
{}
};
(Also in your code you should separate your code into .h and .cpp files, not have the functions defined within the class definitions. I just put everything together to save typing)
Unless Gold, Silver, Diamond have unusual properties, I would be inclined to have a single Currency class with the type of currency as an enum attribute of the class. You could do as Gando suggested and have the currency type determined by the value held, or you could have different exchange rates for your various currencies. That's up to your creativity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
enum currency_t
{ GOLD = 1,
SILVER = 2,
DIAMOND = 3
};
class Currency
{ currency_t curr_type;
int curr_amt;
public:
Currency (currency_t typ, int amt);
// Convert current type and amount to a new type using exchange rates
bool Exchange (currency_t newtype);
// Spend specified amount of current currency
bool Spend (int amt);
// Receive amount of specified currency
void Receive (currency_t typ, int amt);
};
Another variation on this is to keep any array of amounts.
int curr_amt[3]; // Indexed by enum
This would allow a player to hold multiple currencies. You could allow the player to trade, or simply use most valuable to least valuable as needed.
Okay, thanks for the fast replies guys. (I have already been told about putting stuff in .h and .cpp, thanks for the tip tho)
I was going to put in a drop system later on where when you kill something it drops a certain amount of silver, gold and/or diamond. I think that would need seperate classes wouldn't it?
I was thinking that each currency would allow the player to buy different items. So silver could be potions or something. Gold and Diamond could buy different things. Does that help with how complex they are?
I was thinking of putting exchanges in so they could exchange 20 silver for 1 gold. Does that make sense?
Oh yea i know this is off topic but i was wondering if you could like have different folders within your project to keep the different files, so a folder for all currency code files and stuff? I am just wondering whether that is possible.
I was going to put in a drop system later on where when you kill something it drops a certain amount of silver, gold and/or diamond. I think that would need seperate classes wouldn't it?
Not necessarily. See line 18 in my snippet above. If you kill something, then you would call the Receive function which would "take" the currency that was dropped. Now if the object that you kill "drops" a Currency object, then you might want a Receive function that accepts a Currency object.
1 2
// Add a dropped currency object to this currency instance
void Receive (Currency & dropped);
I was thinking that each currency would allow the player to buy different items. So silver could be potions or something. Gold and Diamond could buy different things.
I was thinking of putting exchanges in so they could exchange 20 silver for 1 gold. Does that make sense?
Again, this is all up to how creative you want to be.
OMG thanks AbstractationAnon, looks like that code must have taken a while to write i appreciate it, right the only thing i would like to know now is about the folder thing. Then i can put this on solved.
Oh yea i know this is off topic but i was wondering if you could like have different folders within your project to keep the different files, so a folder for all currency code files and stuff? I am just wondering whether that is possible.
Also, thanks guys for your time. This is like my first item asking something on a forum so i was a bit nervous on if i would get called a noob and stuff.
Sorry. Meant to answer your folder question earlier.
Yes, you can do that, however, if you only have a single .h and .cpp file for currency stuff, then it's probably more trouble than it's worth.
When I create a folder for a set of files, it usually when I'm creating a library or a DLL, then I create a project in my IDE for the library. Visual Studio knows if I've made changes to the library. If not, then it knows it doesn't need to recompile anything in the library. The library or DLL is usually something I want to share across projects (solutions in VS).