Hello! (Spelled polymorphism wrong in title, I know... it's late!)

Now, I understand the concepts of both just fine, but implementing them is where I really run into trouble; this is especially when I'm given a specific problem to solve by using them (i.e. my current assignment).

Okay so here's an overview of my current assignment:

Create a C++ application to handle Savings and Checking accounts. Start with an Account class as the base class to the Savings and the Checking derived classes. The Account class need only contain the account number and account balance and any functions that may be applied directly to the Account class. The Savings account contains only the interest rate for the account and accumulated interest paid. The Checking account needs a Boolean variable to indicate if the account has overdraft protection and, if so, a member to store the overdrawn amount(s) as “loans.”

All accounts must have set and get functions for their data members (all data members must be private).

Account balance may never be negative.

All accounts must contain a print function.

The Account class must have virtual functions for all functions that may vary in the derived classes.

There must be at least one “pure” virtual function in the Account class.

In addition to deposit and withdrawal functions for each account, the following are needed:
For Savings: a function to change interest rate (min rate 3% - max rate 6.5%) and a function to calculate and update interest for the account.

For Checking: a function to change the overdraft flag (T to F; F to T); a deposit function that processes any deposited amount against an existing overdraft loan before updating Account balance; a check payment function that handles potential overdrawn amounts instead of a withdrawal.

All functions should have an appropriate display in the test program to indicate what action is (has) taken place.

The test program should exercise all account constructors and methods.

It's a toughie, right? Maybe not for most of you, but for me it seems impossible.

Here's what I have so far, a lot of it is probably wrong (and I know it's not much):

-MAIN IS CURRENTLY EMPTY-

ACCOUNT.H

#include <string>

using namespace std;

#ifndef ACCOUNT_CLASS
#define ACCOUNT_CLASS
class Account
{
      private:
             string acctNum;
             double acctBal;
             
      public:
             Account(string aN = "", double aB = 0.0);
             
             virtual void accountUpdate() = 0;
             virtual void deposit() = 0;
              
             void setAcctNum(string aN);
             void setAcctBal(double aB);
              
             string getAcctNum() const;
             double getAcctBal() const;

             virtual void printData() const;
}
#endif

ACCOUNT.CPP

#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

#include "Account.h"

Account :: Account(string aN, double aB)
{
        acctNum = aN;
        acctBal = aB;
}

virtual void Account :: printData() const;
{
     cout << "\n" << "Current balance for acct. # "<< acctNum << ": $"
          << acctBal << endl;
}

//================================Set Mutator Function declarations=============================================
void Account :: setAcctNum(string aN)
{
     acctNum = aN;
}

void Account :: setAcctBal(double aB)
{
     acctBal = aB;
}

//================================Get Accessor Function declarations=============================================
string Account :: getAcctNum() const
{
       return acctNum;
}

double Account :: getAcctBal() const
{
       return acctBal;
}

CHECKING.H

#include <string>

#include "Account.h"

#ifndef CHECKING_CLASS
#define CHECKING_CLASS
using namespace std;

class Checking: public Account
{
      private:
              bool overDraft;
              double loan;
              
      public:
             Checking(bool oD = false, double lN = 0.0);
             
             double withdraw();
             
             void setOverDraft(bool oD);
             void setLoan(double lN);
             
             bool getOverDraft() const;
             double getLoan() const;
             
}
#endif

CHECKING.CPP

#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

#include "Checking.h"

Checking :: Checking(bool oD, double lN)
{
        overDraft = oD;
        loan = lN;
}

void Checking :: printData() const;
{
     cout << "\n" << "Current loan balance for acct. # "<< acctNum << ": $"
          << loan << endl;
}

//================================Set Mutator Function declarations=============================================
void Checking :: setOverDraft(bool oD)
{
     overDraft = oD;
}

void Checking :: setLoan(double lN)
{
     loan = lN;
}

//================================Get Accessor Function declarations=============================================
bool Checking :: getOverDraft() const
{
       if (withdraw > acctBal)
          {
                    loan = withdraw - acctBal;
                    return true
          }
          
          else
          return false;
}

double Checking :: getLoan() const
{
       return loan;
}

SAVINGS.H

#include <string>

#include "Account.h"

#ifndef SAVINGS_CLASS
#define SAVINGS_CLASS
using namespace std;

class Savings: public Account
{
      private:
              int interestRate;
              double interestPaid;
              
      public:
             Savings(int iR = 0, double iP = 0.0);
             
             double withdraw();
             
             
             
             void setRate(int iR);
             void setPaid(double iP);
             
             int getRate() const;
             double getPaid() const;
             
}
#endif

SAVINGS.CPP

#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

#include "Savings.h"

Savings :: Savings(int iR, double iP)
{
        interestRate = iR;
        interestPaid = iP;
}

void Savings :: printData() const;
{
     cout << "\n" << "Current interest rate for acct. # "<< acctNum << ": "
          << interestRate << "\n" << "Current interest paid for acct. # "<< 
             acctNum << ": " << interestPaid << endl;
}

//================================Set Mutator Function declarations=============================================
void Savings :: setRate(int iR)
{
     interestRate = iR;
}

void Savings :: setPaid(double iP)
{
    interestPaid = iP
}

//================================Get Accessor Function declarations=============================================
int Savings :: getRate() const
{
       return interestRate;
}

double Savings :: getPaid() const
{
       return interestPaid;
}

Okay, what I'm currently looking for is just some general comments on what I am doing totally wrong. Just some guidelines. I'm still trying to teach myself inheritance and learn how to implement it into my classes, so a lot of this is probably very wrong. Hopefully once I iron out the big issues, I can start understanding what is being done and get down to the "nitty gritty" of both inheritance and polymorphism.

Thank you! :)

The glaring problem that I see is a lack of any connection at all between Checking/Savings and Account outside of the inheritance statement. You don't interact with any part of Account in either Checking or Savings. In an Object-Oriented system, generally it's okay for Derived objects (Checking/Savings) to be aware of a Base (Account) object, but it's not okay for the Base object to be aware of the Derived objects. Based on that, how do you plan on modifying the account balance of a Savings account if you don't modify Account::acctBal from Savings::Deposit() and/or Savings::Withdraw()?

However, even if you attempted to, you wouldn't be able to unless you used a public interface method from Account. You have specified acctNum and acctBal as private members.

In general, this is good practice, but this is inheritance/polymorph so you need to do it a little differently. Your assignment's instruction "All accounts must have set and get functions for their data members (all data members must be private)." is good practice, but the language is misleading. It really should say "all data members must be protected". Protected allows derived classes access to the members, but still protects them from outside access like private.

I would say check with your instructor on this issue. It may be that he actually does want them private. If that's the case, you'll have to use Account's public functions to perform the necessary actions.

Banking accounts is a good situation for inheritance/polymorphism. The problem is the way this problem is constrained, it is not a good inheritance/polymorphism example, it's incomplete and inefficient. It's more like composition.

Thank you for the great replies!

I was wondering about the private vs. protected issue as well, I'll check with my instructor. Also, I'll be sure to check out that tutorial as well!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.