?
Solved

Derived class problem

Posted on 2005-04-22
20
Medium Priority
?
393 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:Helix
19 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13846204
Hi Helix,
Exactly what is your question?

David Maisonave :-)
Cheers!
0
 

Author Comment

by:Helix
ID: 13846218
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13846219
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;

}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 13846242
BTW, the output then is

 Account number 1234 has a balance of 50
test
0
 

Author Comment

by:Helix
ID: 13846244
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..
0
 

Author Comment

by:Helix
ID: 13846367
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 13846390
You mean like

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

   withdraw(charge);

}

?
0
 

Author Comment

by:Helix
ID: 13846411
Unofruntalty that doesn't work
0
 
LVL 86

Expert Comment

by:jkr
ID: 13846441
Hmm, it does compile - however I don't know what purpose you're intending, so...
0
 

Author Comment

by:Helix
ID: 13846458
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.
0
 

Author Comment

by:Helix
ID: 13846867
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..
0
 
LVL 86

Expert Comment

by:jkr
ID: 13846878
What value are you trying to pass and what's your code not? (my crystal ball is on repair)
0
 

Author Comment

by:Helix
ID: 13846889
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 13846909
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?
0
 

Author Comment

by:Helix
ID: 13846944
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
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 13847720
Yes, that's what I've been using (along with the above changes):

#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, 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;

   withdraw(charge);

}

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

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

    return 0;
}


0
 

Author Comment

by:Helix
ID: 13849077
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;
}
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 13853177
PLease post your code after the changes suggested above

Amit
0
 
LVL 14

Expert Comment

by:wayside
ID: 15714117
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question