Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Currency Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

5.7 Creating Custom Exchange Objects and Groups

Imagine that you have the following problem. You have exchange rates for many currencies, but all of the rates are for converting currencies to and from US Dollars. There are no rates for directly converting currency A to currency B when neither A nor B are US Dollars. However, it's likely that you have rates for exchanging A and B to and from US Dollars. In such cases, you can employ an indirect conversion strategy for converting A to B, using US Dollars as the base currency.

This section describes how you can create a class called USDollarBaseExchange to implement such an exchange strategy. The class created here can be used by itself or as an implementation for the RWExchange class.


Source code for the classes and programs described throughout Section 5.7, "Creating Custom Exchange Objects and Groups," are available in buildspace\examples\currency.

5.7.1 Class USDollarBaseExchange

The class USDollarBaseExchange inherits from RWExchangeImpl<T>, and implements a currency conversion method that converts a source currency to a US Dollar base, then converts the US Dollar base to a target currency.

Here is the code that declares the class:

First, this class declares a default and copy constructor, and a constructor that provides a source currency mnemonic, a target currency mnemonic, a conversion factor for source to US Dollars, and a conversion factor for US Dollars to target.

Next, it adds definitions for the following inherited virtual functions, and for the assignment operator:

After defining the virtual functions, the class defines sourceToUsDFactor() and usdToTargetFactor(), which return the conversion factors required for this method of conversion. Similarly, it defines functions setSourceToUsDFactor() and setUsdToTarget, which allow the conversion factors to be set.

5.7.2 Class USDollarBaseGroup

Class USDollarBaseGroup inherits from RWExchangeGroupImpl<T>, and implements a currency exchange group that produces USDollarBaseExchange objects, which convert a source currency to a US Dollar base, then convert the US Dollar base to a target currency. Here is the code that declares the class:

First, this class declares the default constructor. Then it adds definitions for the following inherited virtual functions:

5.7.3 Using the Custom Classes

The groupexam example demonstrates how to add the classes described in Section 5.7.1, "Class USDollarBaseExchange," and Section 5.7.2, "Class USDollarBaseGroup," to an exchange factory, and how to perform some currency exchanges. Here is an excerpt from the code that creates and populates a currency exchange factory:

The currency exchange factory contains four exchange groups, in the following order: RWMultiplicationGroup, RWDivisionGroup, RWEuroGroup, and USDollarBaseGroup. When the exchange factory gets a request for an exchange object, it will ask each exchange group, in order, to create the exchange object. For example, the following line of code requests an exchange object for converting currency ARP to AUD:

In this example, the exchange object, exchanger, is used to perform the actual currency conversion. The example defines function doConversion(), which takes a source currency and an exchange object, and returns a RWMoney object target, which contains the conversion. The doConversion() function prints the results as well. Here is its code:

Let's see how all of these pieces are brought together. The following code excerpt creates two RWMoney objects, source and target, requests an exchange object from the factory, and, if the exchange object is valid, uses the doConversion() function to perform a conversion:

You can see the complete source code for this example in buildspace\examples\currency\groupexam.cpp.



Previous fileTop of DocumentContentsIndex pageNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.