Currency For A Game

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Currency {
  //left out other boiler plate stuff
  public:
    Currency(int v)
    : value(v)
    {}
    std::string getType() const
    {
        if (value == 100)
        {
            return "Gold";
        }
        else if (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)
Last edited on
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?

(edit: added more detail)
Last edited on
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.



Building on the example I gave earlier, here's the start of a Currrency class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>
using namespace std;

enum currency_t
{  GOLD = 1, 
   SILVER = 2, 
   DIAMOND = 3
};
const int MAX_CURRENCIES = 3;
const string currency_names[MAX_CURRENCIES] = 
{   "Gold",
    "Silver",
    "Diamonds",
};

class Currency
{   int  	curr_amt[MAX_CURRENCIES];

public: 
    //  Default constructor
    //	All amounts are initialized to zero.
    Currency ()
    {   for (int i=0; i<MAX_CURRENCIES; i++)
            curr_amt[i] = 0;
    }

    //	Initialize object with an amount of a specific currency
    Currency (currency_t typ, int amt)
    {	curr_amt[typ] = amt;
    }

    //  Convert current type and amount to a new type using exchange rates
    bool Exchange (currency_t oldtyp, int oldamt, currency_t newtype)
    {   //	Left for you to do
        return true;
    }

    //  Spend specified amount of a currency
    bool Spend (currency_t typ, int amt)
    {   if (curr_amt[typ] < amt)
            	return false;  // Not enough of this currency
        curr_amt[typ] -= amt;
        return true;
    }

    // Receive amount of specified currency
    void Receive (currency_t typ, int amt)
    {   cout << "You receive: ";
        cout << amt << " " << currency_names[typ] << endl;
        curr_amt[typ] += amt;
    }

    // Receive another Currency object 
    void Receive (const Currency & curr)
    {    cout << "You receive: ";
        for (int i=0; i<MAX_CURRENCIES; i++)
        {    if (curr.curr_amt[i])
                cout << curr.curr_amt[i] << " " << currency_names[i] << endl;
            curr_amt[i] += curr.curr_amt[i];
        }
     }

    //	Display the currency values
    void Display () const
    {   cout << "You hold: " << endl;
        for (int i=0; i<MAX_CURRENCIES; i++)
	if (curr_amt[i])
	    cout << curr_amt[i] << " " << currency_names[i] << endl;
    }
};
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.
Last edited on
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).
Last edited on
Alright thanks, you have been very helpfull guys. Problem solved
Topic archived. No new replies allowed.