Link to home
Start Free TrialLog in
Avatar of Helix
Helix

asked on

Derived class problem

Hey everyone, I'm trying to complete a question out of my C++ texbook but having some problems. Basically, i have a BankAccount class. I then have to write a derived class for a cheque account which has an additional attribute of an overdraft charge.Any withdrawal that will leave the balance with a negative value will have a charge made. This charge is subtracted from the balance (i.e. balance becomes more negative)  each time a withdrawal results in a negative balance.

The ChequeAccount will have function to get the overdraft charge, and the constructor will take 3 parameters:  one to initialise accNum, one to initialise balance and the third to initialise the overdraft charge. i will lateralter BankAccount::withdraw to accommodate the extra charge.

But so far im having enough problems implementing the derived class and sorting out its member functions, can anyone give me some help please?

#include <iostream>
using namespace std;

class BankAccount{

 public:
      BankAccount( int num, double amount);
      BankAccount();

      void deposit(double amount);
      void withdraw( double amount);

      int getAccNum() const;
      double getBalance() const;  
      
    void printDetails()const;
   
protected:
   double balance;
   int accNum;
};


BankAccount::BankAccount (int num, double amount)
{
     balance = amount;
     accNum = num;
}




BankAccount::BankAccount()
{  
     balance = 0;
     accNum = 100;
}  


void BankAccount::deposit (double amount)
{
   balance+= amount;
}

void BankAccount::withdraw (double amount)
{
      if (balance -=amount < 0)
      cout << "test" << endl;
}

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


int BankAccount::getAccNum() const
{
      return accNum;
}


void BankAccount::printDetails() const
{
   cout<<" Account number "<< accNum <<" has a balance of " << balance << endl;
}
   

class ChequeAccount: public BankAccount{
public:
      ChequeAccount(int num, int amount, int charge);
      double GetOverdraft();
      int getCharge();
private:
      double overdraft;
};


double ChequeAccount::GetOverdraft()
{
      return overdraft;
}

ChequeAccount::ChequeAccount(int num, double amount, double charge)
{
      balance = amount;
    accNum = num;
      overdraft = charge;

}

int ChequeAccount::getCharge()
{
      return overdraft;
}
int main()
{
      ChequeAccount chq  (1234, 50, 5);

      chq.printDetails();
      
      chq.withdraw(55);

      return 0;
}

Thanks to anyone who may be able to help

David
Avatar of Axter
Axter
Flag of United States of America image

Hi Helix,
Exactly what is your question?

David Maisonave :-)
Cheers!
Avatar of Helix
Helix

ASKER

Basically, the code doesn't work, i cant seem to get my statements in Main to work. Once ive got those working I can then proceed to calculate the extra charge.
The constructor's parameter list sdid not match - use

class ChequeAccount: public BankAccount{
public:
    ChequeAccount(int num, double amount, double charge);
    double GetOverdraft();
    int getCharge();
private:
    double overdraft;
};


double ChequeAccount::GetOverdraft()
{
    return overdraft;
}

ChequeAccount::ChequeAccount(int num, double amount, double charge)
{
    balance = amount;
   accNum = num;
    overdraft = charge;

}
BTW, the output then is

 Account number 1234 has a balance of 50
test
Avatar of Helix

ASKER

Jesus, cant beleive I missed that one. Must be my tiredness, heh.

now moving on to calculate the balance after the extra charge has been made..
Avatar of Helix

ASKER

Is there a way i can tranfer the value for the overdraft cost from the constructor the withdraw function? I would simply add it in with chq.withdraw but unfortunatly im not allowed to modify that
You mean like

ChequeAccount::ChequeAccount(int num, double amount, double charge)
{
    balance = amount;
   accNum = num;
    overdraft = charge;

   withdraw(charge);

}

?
Avatar of Helix

ASKER

Unofruntalty that doesn't work
Hmm, it does compile - however I don't know what purpose you're intending, so...
Avatar of Helix

ASKER

Strange, i didn't get it to compile, could you copy/paste the code please?

Basicly, what im trying to do is when the balance gets below 0, an additional charge will have to be made because its going into the overdraft.
Avatar of Helix

ASKER

I still got some problems with tranfering the value for the overdraft cost from the constructor to the withdraw function if anyone fancys a crack at it..
What value are you trying to pass and what's your code not? (my crystal ball is on repair)
Avatar of Helix

ASKER

Well in main i have:

ChequeAccount chq  (1234, 50, 5);

which is Account number > amount > charge. So im setting charge to 5 which i want to pass to withdraw
Um, that's what

ChequeAccount::ChequeAccount(int num, double amount, double charge)
{
   balance = amount;
  accNum = num;
   overdraft = charge;

  withdraw(charge);

}

does - what code are you using for your construcor?
Avatar of Helix

ASKER

My constructor is the same there, I have implemented the withdraw(charge) but got compiling errors:

C:\Documents and Settings\David\My Documents\C++\Exercise 6\3\3.cpp(46) : error C2511: 'withdraw' : overloaded member function 'void (double,double)' not found in 'BankAccount'
        C:\Documents and Settings\David\My Documents\C++\Exercise 6\3\3.cpp(4) : see declaration of 'BankAccount'

Full code is in my first post
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Helix

ASKER

I still cant get it to pass charge into withdraw, i need it there so i can do something like:


void BankAccount::withdraw (double amount)
{
    if (balance -= amount < 0)
    balance -= amount+charge;
}
PLease post your code after the changes suggested above

Amit
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jkr

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

wayside
EE Cleanup Volunteer