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.

Dani AI

Generated

A few small, separate errors are causing most of the compile noise. was right to flag the header issues: remove using namespace std; from headers and keep iostream/includes in the .cpp files. The other blockers are: constructors defined with the wrong name or with the initializer list in the wrong place, malformed function declarations (for example void double ...), the strange while in debit, missing closing braces, and derived classes touching the Account private balance directly instead of using an accessor.

Fixes to apply (in order):

  • Remove using namespace std; from all headers.
  • Give the derived constructors the correct name and use the initializer list after the signature. Example:
    SavingsAccount::SavingsAccount(double amount, double rate)
      : Account(amount), interestRate(rate)
    {
    }
  • Make the base getter a const member and use it from derived classes instead of accessing balance:
    double getBalance() const;
  • Replace the incorrect debit loop with a simple check and subtract, or print/throw when insufficient funds:
    void Account::debit(double amount)
    {
      if (amount > getBalance())
          std::cout << "Debit amount exceeded account balance\n";
      else
          balance -= amount;
    }
  • Fix CheckingAccount declarations to match the base signatures and use transactionFee:
    void credit(double amount);
    void debit(double amount);

    and implement them by calling the base methods with transactionFee applied.

Other recommendations: declare credit/debit virtual in Account if you intend overrides, and make the destructor virtual. Compile with warnings on (for example g++ -Wall -Wextra -std=c++11 *.cpp) and fix one error at a time — the first few will cascade into many secondary messages. For constructor syntax and access-control rules see the C++ reference on and .

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.