Data types like Money or Price

B

bb

Hi,

Are there any open source data types ( c++ classes ) for Money and/or
Price that I can inherit and customize for my use instead of writing
from the scratch?

Any help would be much appreciated.

Thanks.
 
A

anon

bb said:
Hi,

Are there any open source data types ( c++ classes ) for Money and/or
Price that I can inherit and customize for my use instead of writing
from the scratch?

Any help would be much appreciated.

Thanks.

Here they are:

class Money
{
};

class Price
{
};
 
I

Ioannis Gyftos

typedef int?

Besides, what do you want those classes to do? I doubt there is
anything you'd find that suits your needs.
 
T

tragomaskhalos

Are there any open source data types ( c++ classes ) for Money and/or
Price that I can inherit and customize for my use instead of writing
from the scratch?

Martin Fowler has such a beast in one of his books, and
googling "martin fowler money class" yields these links
(among others):
http://martinfowler.com/ap2/quantity.html
http://huntjason.spaces.live.com/blog/cns!9D2E96F2AA6AE85F!231.entry

Unfortunately these are not C++, but if you have no luck
locating a C++ implementation they should give you a good
basis for writing your own.
 
B

bb

Martin Fowler has such a beast in one of his books, and
googling "martin fowler money class" yields these links
(among others):http://martinfowler.com/ap2/quantit....live.com/blog/cns!9D2E96F2AA6AE85F!231.entry

Unfortunately these are not C++, but if you have no luck
locating a C++ implementation they should give you a good
basis for writing your own.

Thanks mate. Probably a good reference.

I have started hand coding all the required operators, bounds checking
etc. specific to my project requirement. ( inheritance heirarchy :
MyDouble <- MyPrice <- MyMoney )

Also, looking at boost http://www.boost.org/libs/mpl/doc/tutorial/representing-quantities.html
that has some interesting related material.

Cheers.
 
P

Puppet_Sock

I have started hand coding all the required operators, bounds checking
etc. specific to my project requirement. ( inheritance heirarchy :
MyDouble <- MyPrice <- MyMoney )

If you've got a careful and complete specification of how
your money class is supposed to behave, you are already
much farther along than the typical application involving
money. Eventually most such projects wind up here asking
how they get the roundoff of pennies correct. It's a lot
of fun telling them that I can't quite read their spec
from where I'm sitting, then having them say they don't
have a spec. Usually they get confused at that point.

Just a suggestion: Consider whether you really need that
inheritance. Consider whether what you really want is a
Currency class. (Or maybe call it Cash or Money or some
such.) Then when you have a price you can make that class
contain a Currency as a data member.

So Currency may not be a kind of Price, nor Price a kind
of Currency. But a Price may have a Currency value.
Why do it that way? Well, maybe a Price is different
to different people, cross border for example. And
the difference may not simply be the exchange rate.

Without getting too much into your specific app it's hard
to know whether the inheritance is the correct approach.
Socks
 
J

James Kanze

[snip]
I have started hand coding all the required operators,
bounds checking etc. specific to my project requirement. (
inheritance heirarchy : MyDouble <- MyPrice <- MyMoney )
If you've got a careful and complete specification of how
your money class is supposed to behave, you are already
much farther along than the typical application involving
money. Eventually most such projects wind up here asking
how they get the roundoff of pennies correct. It's a lot
of fun telling them that I can't quite read their spec
from where I'm sitting, then having them say they don't
have a spec. Usually they get confused at that point.

At least in the EU, the spec is a given; EU directives say
exactly how you have to round the cents.
Just a suggestion: Consider whether you really need that
inheritance. Consider whether what you really want is a
Currency class. (Or maybe call it Cash or Money or some
such.) Then when you have a price you can make that class
contain a Currency as a data member.

His inheritance doesn't make any sense at all. Generally
speaking, you probably need: a general Decimal class, to handle
the basic arithmetic; a monitary value class (MoneyAmount, or
some such) which has a Decimal in its implementation (but
controls rounding, etc., in a specific way), and a third class
which has both a MoneyAmount and a currency indicator (Euro,
GBP, USD, etc.).

[...]
Without getting too much into your specific app it's hard to
know whether the inheritance is the correct approach.

These are values, so inheritance is almost certainly the wrong
approach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top