0

coders! I have been accomplishing my c++ homework, but got stuck with relating a method to a class called BankAccount. Main idea is to create a class called BankAccount with its own methods like deposit() or addInterest() with functions that match their name and constructors. I have declared all of my methods and wrote their code, however when it came to the methods called "void BankAccount::addInterest()" and "void BankAccout::deposit()" I get error messages like "incompitable". I will share my code here:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class BankAccount
{

public:
    double getBalance();
    void deposit(double);
    void witdraw(double);
    void addInterest();
    BankAccount();
    BankAccount(double, double, int, string);
    double displayAccountSummary();
    
    
private:
    double interestRate;
    int accountNumber;
    double deposit;
    double accountWithdraw;
    double balance;
    string ownerName;



    
};
int main()
{
    BankAccount myAccount(1000.50, 0.05, 1111, "John Williamas");
    myAccount.deposit(500);
    myAccount.witdraw(200);
    myAccount.addInterest();
    myAccount.displayAccountSummary();
    return 0;
}

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

void BankAccount::addInterest(double rate)
{
    balance = balance * (1 + rate);
}

BankAccount::BankAccount()
{
    balance = 0;
    interestRate = 0;
    accountNumber = 0;
    ownerName = "";
}

BankAccount::BankAccount(double money, double rate, int accountnumber, string name)
{
    balance = money;
    interestRate = rate;
    accountNumber=accountnumber;
    ownerName = name;
}

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

void BankAccount::witdraw(double wizdraw)
{
    if (wizdraw < 0||wizdraw>balance)
    {
        cout << "Witdraw amount can not be less than 0 or greater than balance. Try again\n ";
        cin >> wizdraw;
    }
    balance = balance - wizdraw;
}



double BankAccount::displayAccountSummary()
{
    return balance;
    return interestRate;
    return accountNumber;
    cout<< ownerName;
}
  • 3
    Include the full compiler error in your question. Also: [why is using namespace std considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Stefan Riedel Dec 02 '21 at 15:07
  • 1
    Which particular line? (Also why does `displayAccountSummary` have three return statements - only the first will happen here) – doctorlove Dec 02 '21 at 15:07
  • 2
    Btw `void addInterest()` is not compatible with `void BankAccount::addInterest(double rate)` - most of the time, the compiler error is telling you exactly what's wrong (if it's not MSVC...), just read it carefully – Stefan Riedel Dec 02 '21 at 15:08
  • And just a side note: according to most coding conventions I've seen (for C++) you would write constructors before any "regular" method. – Stefan Riedel Dec 02 '21 at 15:14
  • Most compilers in addition to giving you a more complete message than is posted will tell you the exact line that the error occurs on. There are several errors: [https://ideone.com/rj2BDR](https://ideone.com/rj2BDR) one of them is you made deposit a function and also a variable. You need to rename one of these. – drescherjm Dec 02 '21 at 15:15

2 Answers2

1

You have naming conflicts in your code. For example, your void deposit(double) member function and double deposit member variable have the same names.

The fixed version is here and I have improved it a lot:

#include <iostream>
#include <iomanip>
#include <string>
#include <tuple>


class BankAccount
{

public:
    BankAccount( );
    BankAccount( const std::string ownerName, const double balance, const double interestRate, const int accountNumber );

    double getBalance( ) const;
    std::tuple< double, double, int > getAccountSummary( ) const;
    void displayAccountSummary( ) const;

    void deposit( const double depositAmount );
    bool withdraw( const double withdrawAmount );
    void addInterest( const double rate );
    
private:
    std::string m_ownerName;
    double m_balance;
    double m_interestRate;
    int m_accountNumber;
    double m_deposit;
    double m_accountWithdraw;
};


int main( )
{
    BankAccount myAccount( "John Williams", 1000.50, 0.05, 1111 );

    std::cout << "Enter the deposit amount: ";
    double depositAmount { };
    std::cin >> depositAmount;
    myAccount.deposit( depositAmount );

    bool wasSuccessful { false };
    do
    {
        std::cout << "Enter the withdraw amount: ";
        double withdrawAmount { };
        std::cin >> withdrawAmount;
        wasSuccessful = myAccount.withdraw( withdrawAmount );
        if ( !wasSuccessful )
        {
            std::cout << "Withdraw amount can not be less than 0 or greater than balance. Try again\n ";
        }

    } while ( !wasSuccessful );

    std::cout << "Enter the interest rate: ";
    double rate { };
    std::cin >> rate;
    myAccount.addInterest( rate );

    // use structured bindings like below to store the returned values
    const auto [ balance, interestRate, accountNumber ] = myAccount.getAccountSummary( );

    std::cout << '\n';
    myAccount.displayAccountSummary( );

    return 0;
}

BankAccount::BankAccount( )
: m_ownerName( "NO-NAME" ), m_balance( 0 ), m_interestRate( 0 ), m_accountNumber( 0 )
{
}

BankAccount::BankAccount( const std::string ownerName, const double balance, const double interestRate, const int accountNumber )
: m_ownerName( ownerName ), m_balance( balance ), m_interestRate( interestRate ), m_accountNumber( accountNumber )
{
}

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

// return multiple values using a tuple and not by writing 3 return statements!!!
std::tuple< double, double, int > BankAccount::getAccountSummary( ) const
{
    return std::tuple< double, double, int >( std::move( m_balance ),
                                              std::move( m_interestRate ),
                                              std::move( m_accountNumber ) );
}

void BankAccount::displayAccountSummary( ) const
{
    std::cout << "Owner: " << m_ownerName << '\n';
    std::cout << "Balance: " << m_balance << '\n';
    std::cout << "Interest rate: " << m_interestRate << '\n';
    std::cout << "Account number: " << m_accountNumber << '\n';
}

void BankAccount::deposit( const double depositAmount )
{
    m_balance += depositAmount;
}

bool BankAccount::withdraw( const double withdrawAmount )
{
    if ( withdrawAmount < 0 || withdrawAmount > m_balance )
    {
        return false;
    }

    m_balance -= withdrawAmount;

    return true;
}

void BankAccount::addInterest( const double rate )
{
    m_balance *= ( 1.0 + rate );
}

A sample input/output:

Enter the deposit amount: 500
Enter the withdraw amount: 200
Enter the interest rate: 0.05

Owner: John Williams
Balance: 1365.53
Interest rate: 0.05
Account number: 1111

Now complete the code and the rest of its features on your own.

digito_evo
  • 3,216
  • 2
  • 14
  • 42
-2

The method above is feasible, you can also add this->, like this->deposit to avoid naming conflict.

  • 1
    There are some circumstances where it matters (specifically, templates where `this` it type-dependent) but for a simple class like this it doesn't matter. – MSalters Dec 02 '21 at 16:42