I have an assignment that I am almost finished with but I am trying to figure out why my code won't compile. I believe the issue to be minor but I can't seem to figure out the bug. The problem call for me to build an inheritance heirarchy that a bank may use. The base class is 'Account' and the derived classes 'SavingsAccount' and 'CheckingAccount' inherit from this account.

Here is my Header for and source for class Account

#ifndef Account_H
#define Account_H

using namespace std;

class Account
{
      private: 
      double balance;
      
      public:
      Account(double =0.0);
      
        
      void credit(double add);
      void debit(double sub);  
      double getbalance(); 
      
      
};            
#endif
#include <iostream>
#include "Account.h"

using namespace std;

Account::Account(double amount)
{
  if (amount <=0.0)
  {
    balance =0.0;
    cout<<"Initial balance must be greater than 0"<<endl;
   }  
   else balance = amount;                    
   
}

void Account::credit(double add)  
{
   balance +=add;
}

void Account::debit(double sub)                                              
{
 if (sub > balance)
 {
   bool r = false;
  
while (r!=false)
{                             
  balance-=sub;
}
}

double Account::getbalance()
{
  return balance;
}

Here is my header and source for class SavingsAccount

#ifndef SavingsAccount_H
#define SavingsAccount_H

using namespace std;

class SavingsAccount : public Account
{
     public:
     SavingsAccount(double amount,double rate);
     
     double CalculateInterest();
     
     private:
     double interestRate;
     
};             
     
#endif
#include <iostream>
#include "SavingsAccount.h"

using namespace std;

SavingsAccount::Account(double amount,double rate)
{
 :Account(amount)
 
 {
    interestRate = rate;
 }
 
SavingsAccount::CalculateInterest()
{
   double interest;
   
   return interest = balance * rate/12;                                 
}

Here is my header and source for CheckingAccount

#ifndef CheckingAccount_H
#define CheckingAccount_H

using namespace std;

class CheckingAccount : public Account
{
      private:
    double transactionFee;
    
    public:
     CheckingAccount(double amount, double fee);
     
      void double credit(double add);
      void double debit(double sub);  
      
};                    
#endif
#include <iostream>
#include "CheckingAccount.h"

using namespace std;

CheckingAccount::CheckingAccount(double amount,double fee)
{
   :Account(amount)
   
   {
     transactionFee = fee;
   }  
   
CheckingAccount::credit(double add)  
{
   Account::credit(add-fee)                                                                                 
}

CheckingAccount::debit(double sub)
{
   Account::debit(sub+fee)                            
}

I keep getting compile errors on account in the source code. I can't figure out how to correctly implement the getbalance() function and getting it to compile. Also having other compile errors but this may be the cause for them as well. Any help would be appreciated.

1-in header files you typed

using namespace std;

this need to be used only if you add

#include <iostream>

or any other class that include std namespace
so delete this line as it is not necessary(in headers)
2-some implemented functions doesnot have return type so write it eg:

(CheckingAccount::debit(double sub))

3-this function has 2 errors

void Account::debit(double sub)
{ 
  if (sub > balance) 
  {   
    bool r = false; 
    while (r!=false)//logical error this means you will never sub because r is false
    {                               
      balance-=sub;
    }
  }
}//missing

Ok thanks. I made the recommended changes but I am still having some issues. Not sure if something is wrong with my Bloodshed IDE or not. Do you recommend going with visual C++ instead?

can you show me the fixed code to tell you if all issues are fixed

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.