PreviousNext

Implementing Multiple Managers

Our implementation derives a simple savings account manager class (simpleSave) from the Savings interface class. Since the Savings interface is derived from the Account interface, all nonstatic operations in both interfaces must be declared in the manager class and defined in the manager code. Of course, additional functions and data types (such as constructors and destructors) can also be declared to specifically implement the interface.

Our implementation also derives an overdraft manager class for an overdraft type of savings account. The overdraft account has characteristics of both a savings account and a loan and demonstrates multiple interface inheritance. It is defined to have multiple inheritance by being derived from both the Savings and Loan interface classes.

Note: Applications can create C++ classes that inherit from multiple interface classes, but interface classes cannot inherit from multiple interfaces.

The following code shows the overdraft manager class and its implementation. This example has the manager implementation included within the class definition header file itself, rather than in separate C++ code. C++ allows you to combine implementation as part of the C++ class declarations. This is common practice when the implementation code for each member function is small.

#ifndef overdraft_i_h
#define overdraft_i_h
#include <iostream.h>
#include "savings.h"
#include "loan.h"

class overdraft : public Savings, public Loan {
public:
overdraft(idl_long_float amt)
{
balance = amt;
}
~overdraft(void)
{
return;
}
idl_long_float getBalance()
{
return balance;
}
/////////////// Member Functions from all interfaces ///////////////
idl_long_float deposit(idl_long_float amt)
{
balance += amt;
return balance;
}
void payment(idl_long_float amt)
{
balance += amt;
}
idl_long_float withdraw(idl_long_float amt)
{
balance -= amt;
return amt;
}
void setInterestRate(idl_long_float r)
{
rate = r/loanTerm;
}
void addInterest()
{
balance += (balance * rate);
}
idl_long_float recalculateLoan(idl_long_float r, idl_long_int m)
{
if(balance < 0)
{
loanRate = r;
loanTerm = m;
return abs(balance) / loanTerm;
}
else
return 0;
}
idl_long_float getAccountBalance()
{
return getBalance();
}
idl_long_float getSavingsBalance()
{
return getBalance();
}
idl_long_float getLoanBalance()
{
static idl_long_float loanBalance;
if(balance < 0)
loanBalance = abs(balance);
else
loanBalance = 0;
return loanBalance;
}
private:
idl_long_float balance = 0; //loan is automatic if negative balance
idl_long_float rate = 0.02; //2%
idl_long_float loanRate = 0.15; //15%
idl_long_int loanTerm = 12; //12 months
};

The manager class must declare all the nonstatic functions of all its inherited interfaces. These include all nonstatic operations defined in all three interfaces, including the Account, Savings, and Loan interfaces. Be sure to define the operation signatures exactly as they are declared in each idl-generated header file, or else the C++ compiler may not interpret the function as an implementation but rather as a new function. If this occurs, the class is interpreted as an abstract class, which means that your application cannot create instances of the manager class.

For this example, refer to the savings_mgr.h and loan_mgr.h header files generated by the IDL compiler to find the signatures of all the functions required. For example, the deposit( ), withdraw( ), and getAccountBalance( ) functions are from the Account interface but are redeclared in the derived Savings interface. The payment( ), recalculateLoan( ), and getLoanBalance( ) functions are declared in the Loan interface. The setInterestRate( ),addInterest( ), and getSavingsBalance( ) are declared in the Savings interface.

The static function implementations for the Savings and the Loan interface classes are not shown here but include openSimple( ), openOverdraft( ), and openLoan( ).