views:

103

answers:

3

I'm trying to create my first hierarchy with the following classes (Account, CheckingAccount, and SavingsAccount) and can't figure out how to link the classes together.
Also, should the balance value be public in the header?
Currently it is not, and it shows "error in this context" every time it's mentioned in this main code.

[stackoverflow questions] Is using pastebin instead of including the code with the question okay? Is there a faster way to indent by 4? Oh well.

header:

class Account 
{
public:
   Account(double);
   void creditBalance(double);
   void debitBalance(double);
   double getBalance() const;
protected:
   double balance;      
};

class SavingsAccount : public Account
{
public:
   SavingsAccount(double, double);
   double calculateInterest();

private:
   double interest = 10;

};

class CheckingAccount : public Account
{
public:
   CheckingAccount(double, double);
   void feeCreditBalance(double);
   void feeDebitBalance(double);

private:
   double fee = 10;

};

CPP file

#include "12.10.h"
#include <iostream>
using namespace std;

Account::Account(double initBal)
{
   if(initBal < 0)
      initBal = 0;
   balance = initBal;
   cerr << "Initial balance was invalid.";
}

void Account::creditBalance(double plus)
{
   if(plus > 0)
      balance += plus;
   else
      cout << "Cannot credit negative.";
}

void Account::debitBalance(double minus)
{
   if(minus <= balance)
      balance -= minus;
   else
      cout << "Debit amount exceeded account balance.";
}

double Account::getBalance() const
{
   return balance;
}

SavingsAccount::SavingsAccount(double initBal,double intrst):Account(initBal)
{
   if(initBal < 0){
      initBal = 0;
      cerr << "Initial balance was invalid.";
   }
   balance = initBal;


   if(intrst<0)
      intrst=0;
   interest = intrst;
}

double SavingsAccount::calculateInterest()
{
   if(interest>=0)
      balance=balance+(balance*(0.01*interest));
   return balance;
}

CheckingAccount::CheckingAccount(double initBal, double phi) : Account(initBal)
{
   if(initBal < 0)
      initBal = 0;
   balance = initBal;
   cerr << "Initial balance was invalid.";

   if(phi < 0)
      phi = 0;
   fee = phi;
}

void CheckingAccount::feeCreditBalance(double plus)
{
   if(plus > 0){
      balance += plus;
      balance -= fee;
   }
   else
      cout << "Cannot credit negative.";
}

void CheckingAccount::feeDebitBalance(double minus)
{
   if(minus <= balance){
      balance -= minus;
      balance -= fee;
   }
   else
      cout << "Debit amount exceeded account balance.";
}

I have updated the content of my code, there are 6 errors that I can't find. I have a feeling it has to do with the altered feeDebit and feeCreditBalance.
Are they supposed to keep the same name as the original debit and creditBalance ones?
What is the syntax for the redefining of these functions?

A: 

balance should not be public. You have already creditBalance() and debitBalance() functions, so no need to make it protected either.

To avoid the error use your getBalance() function in main() instead, thats why you made it.

The constructor for your derived class could look like:

SavingsAccount::SavingsAccount(double initBal,double intrst):
    Account(initBal)
    {
       if(intrst<0)
           intrst=0;
       interest = intrst;
    }
drhirsch
oh, I see. The name of the double for the original needs to be provided to match the position of initBal.
R. Daneel Olivaw
I tried the same idea with others, didn't work:void CheckingAccount::creditBalance(double plus) : creditBalance(plus)
R. Daneel Olivaw
I changed the credit and debit variations' names, to make them different, then tried this. Worked. Down to 6 mysterious errors.
R. Daneel Olivaw
A: 

[One of] the benefits of polymorphism is that it allows limiting duplication of code.
As currently written, your program doesn't take advantage of this. For example the Constructors for the SavingsAccount and CheckingAccount could invoke the constructor of Account, their base class. (Look in the C++ manual the proper syntax for this)


Unrelated, there is a logic error in the constructor(s). The snippet:

   if(initBal < 0)
      initBal = 0;
   balance = initBal;
   cerr << "Initial balance was invalid.";

will result in sytematically printing the "invalid" message. You probably want to group several statements (make a compound statement, i.e. with braces) for the if case. Something like:

   if(initBal < 0)
   {
      initBal = 0;
      cerr << "Initial balance was invalid.";
   }
   balance = initBal;
mjv
To do that do I add after them " : public Account(double)"?
R. Daneel Olivaw
Just realized that I changed that after I pasted, good eye though.
R. Daneel Olivaw
A: 

There are three modes you can use when defining accessibility within a class:

  • Public means the member is accessible by anyone.
  • Protected means the member is accessible within the class and any subclasses.
  • Private means the member is only accessible within the class itself.

There's also the possibility of messing with friend classes, but I wouldn't bother with them for this.

If your subclasses need to access the value of balance, there are two possible paths. They could use accessors and mutators to handle the value indirectly, or the access rights to balance need to allow subclasses direct access to the variable.

As written, your code is trying to access a private attribute (Account::balance) from different classes (CheckingAccount/SavingsAccount). Even if the classes in question are parent and child, they're still separate classes so far as access rights are concerned.

goldPseudo
it sounds like I should make balance protected. it seems to have worked.
R. Daneel Olivaw