Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2003-04-27 22:45:00


assume that each currency has a function that can convert into a canonical
measure of value such as dollars, swiss francs or gold.

class pesos
{
        const float dollars_per_peso = .08;
        float amount;
        // define conversion to dollar type with a casting operator
        operator dollars (){
                return dollars(amount * dollars_per_peso );
        }
        pesos(dollars d){
                amount = d / dollars_per_peso;
        }
        pesos(pesos rhs){
                amount = rhs.amount;
        }
};

class bhat
{
        const float dollars_per_bhat = .08;
        float amount;
        // define conversion to dollar type with a casting operator
        operator dollars (){
                return dollars(amount * .002);
        }
        bhat(dollars d){
                amount = d / bhat_per_peso;
        }
        bhat(bhat rhs){
                amount = rhs.amount;
        }
};

bhat bhat_amount bhat(200.00); // amount is 200 bhat;
// to initialize a peso amount given a value in bhat
// convert bhat to dollars, and initalize pesos
pesos pesos_amount(bhat_amount);

Does not require definition of N**2 conversoins

ideas
a) templatize the above, perhaps explicitly instantiate

define

dollars operator+(dollars a1, dollars a2){
        return dollars(a1.amount + a2.amount);
}

this should permit

bhat bhat_amount = pesos_amount + dollar_amount;

Just a rough sketch, but I believe the idea should be clear

Robert Ramey

>From:Terje_Sletteb>From: "Pavel Vozenilek" <pavel_vozenilek_at_[hidden]>

>> "Terje Slettebø" <tslettebo_at_[hidden]> wrote in message
>> news:01fe01c30ca7$1aa134b0$8d6c6f50_at_pc...
>> > If one were to implement currency conversion, how might that be done, in
>> > general?
>> >
>> > Does one need to store the conversion rates between any two currencies
>> > (giving an N^2 size table), or might it be possible to convert any
>> currency
>> > to some "universal currency", and from that again to any other, and
still
>> > getting the right values (giving a 2N table)?
>> >
>> Currency conversion as drop-in library is IMHO not possible - currencies
>> differ from strict units like weight and future applications may require
>> functionality beyond current requirements.

>Well, your library is very general, where you provide conversion functions
>between any two types. If you define each currency as a type, wouldn't this
>cover the functionality for converting between currencies? If not, what
>could be needed?

>I realise that in this case, it may be a lot of functions to define, to
>define the conversion between any two currencies - for e.g. 20 currencies,
>that's around 400 functions to define. I meant if it theoretically would be
>possible for your library, since you say you think it's not possible for a
>"drop-in library" (what is meant by that?).

>Regards,

>Terje


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk