Class Inheritance and Derived Classes

Tom Kelliher, CS18

Mar. 20, 1996

A Banking Example

checking.h

/**********************************************************************
 * checking.h --- Class declaration for a simple checking account.
 * Tom Kelliher
 *
 * A Checking object consists of an account number and balance.
 * Allowed operations:
 *    Constructor: Create a checking account with a given account
 *       number and balance.
 *    inputTrans: Apply a transaction (from enum TransactionType) to
 *       the account.
 *    printBalance: Print the account number and balance.
 *
 * This class is a base class for a joint checking class.
 **********************************************************************/


#ifndef __CHECKING_H
#define __CHECKING_H


// Types of allowed transaction.
enum TransactionType { DEPOSIT = 'D', WITHDRAWAL = 'W' };


class Checking
{
 private:
   int accntNumber;
   double accntBalance;

 public:
   Checking(int number, double balance);
   void inputTrans(TransactionType type, double amount);
   void printBalance(void);
};


#endif

checking.cc

#include <iostream.h>
#include <iomanip.h>
#include "checking.h"


// These methods are documented in checking.h


/**********************************************************************
 * Checking::Checking
 **********************************************************************/

Checking::Checking(int number, double balance)
: accntNumber(number), accntBalance(balance)
{
}


/**********************************************************************
 * Checking::inputTrans
 **********************************************************************/

void Checking::inputTrans(TransactionType type, double amount)
{
   switch (type)
   {
    case DEPOSIT:
      accntBalance += amount;
      break;

    case WITHDRAWAL:
      accntBalance -= amount;
      break;

    default:
      cout << "Illegal transaction code: " << char(type) << endl;
      break;
   }
}


/**********************************************************************
 * Checking::printBalance
 **********************************************************************/

void Checking::printBalance(void)
{
   // This method twiddles with output formatting.  We need to save
   // the output formatting and then restore it before returning.
   long oldPrecision;
   long oldFixed;

   cout << "The balance for account " << accntNumber << " is ";

   // Save previous formatting.
   oldPrecision = cout.precision(2);
   oldFixed = cout.setf(ios::fixed, ios::floatfield);

   cout << accntBalance << ".\n";

   // Restore previous formatting.
   cout.precision(oldPrecision);
   cout.setf(oldFixed, ios::floatfield);
}

joint.h

/**********************************************************************
 * joint.h --- Class declaration for a simple joint checking account.
 * Tom Kelliher
 *
 * A Joint object consists of a Checking base class with appendant
 * owner and joint owner SSNs.
 * Allowed operations:
 *    Constructor: Create a joint checking account with owner and
 *       joint owner SSNs, an account number, and an account balance.
 *    printSsn: Print the SSNs of the two owner.
 *
 * Public inheritance of the Checking class is used, so see checking.h
 * for other allowed operations.
 **********************************************************************/


#ifndef __JOINT_H
#define __JOINT_H


// Read header file for base class.
#include "checking.h"


class Joint : public Checking   // Checking is inherited with public
                                // access.
{
 private:
   long ownerSsn;
   long jointOwnerSsn;

 public:
   Joint(long oSsn, long jSsn, int number, double balance);
   void printSsn(void);
};

#endif

joint.cc

#include <iostream.h>
#include "joint.h"


// These methods are documented in joint.h


/**********************************************************************
 * Joint::Joint
 **********************************************************************/

Joint::Joint(long oSsn, long jSsn, int number, double balance)
: Checking(number, balance),   // Invoke constructor for base class.
ownerSsn(oSsn), jointOwnerSsn(jSsn)
{
}


/**********************************************************************
 * Joint::printSsn
 **********************************************************************/

void Joint::printSsn(void)
{
   cout << "Owner SSN: " << ownerSsn << endl;
   cout << "Joint SSN: " << jointOwnerSsn << endl;
}

A Simple Driver Program

#include <iostream.h>
#include "checking.h"
#include "joint.h"

int main()
{
   Checking check(569248, 1000.00);
   Joint joint(111223333, 444556666, 987234, 500.00);

   check.inputTrans(DEPOSIT, 100.00);
   check.printBalance();
   check.inputTrans(WITHDRAWAL, 50.00);
   check.printBalance();

   cout << endl;

   joint.printSsn();
   joint.inputTrans(DEPOSIT, 39.00);
   joint.printBalance();

   return 0;
}

Driver Output

The balance for account 569248 is 1100.00.
The balance for account 569248 is 1050.00.

Owner SSN: 111223333
Joint SSN: 444556666
The balance for account 987234 is 539.00.



Thomas P. Kelliher
Tue Mar 19 09:29:50 EST 1996
Tom Kelliher