0

I'm trying to print the balance from a checking and savings account. I know you can't return a value using the void function, but in what way can I show the balances for both accounts?

#ifndef ACCOUNT_H
#define ACCOUNT_H

// Account.h
// 4/8/14
// description

class Account {
private:
    double balance;
    double interest_rate; // for example, interest_rate = 6 means 6%
public:
    Account();
    Account(double);
    void deposit(double);
    bool withdraw(double); // returns true if there was enough money, otherwise false
    double query();
    void set_interest_rate(double rate);
    double get_interest_rate();
    void add_interest();
};

#endif


// Bank.cpp
// 4/12/14
// description

#include <iostream>
#include <string>
#include "Bank.h"

using namespace std;

Bank::Bank(): checking(0), savings(0) { }

Bank::Bank(double checking_amount, double savings_amount): checking(checking_amount), savings(savings_amount){;

    checking = Account(checking_amount);
    savings = Account(savings_amount);
}

void Bank::deposit(double amount, string account)
{
    if (account == "S") {
        savings.deposit(amount);
    } if (account == "C") {
        checking.deposit(amount);
    }
}

void Bank::withdraw(double amount, string account)
{
    if (account == "S") {
        savings.withdraw(amount);
    } if (account == "C") {
        checking.withdraw(amount);
    }
}

void Bank::transfer(double amount, string account)
{
    if (account == "S") {
        savings.deposit(amount);
        checking.withdraw(amount);
    } if (account == "C") {
        checking.deposit(amount);
        savings.withdraw(amount);
    }
}

void Bank::print_balances()
{
    cout << savings << endl;
    cout << checking << endl;

}

#ifndef BANK_H
#define BANK_H

// Bank.h
// 4/12/14
// description

#include <string>

#include "Account.h"

using namespace std;

class Bank {
private:
    Account checking;
    Account savings;
public:
    Bank();
    Bank(double savings_amount, double checking_amount);
    void deposit(double amount, string account);
    void withdraw(double amount, string account);
    void transfer(double amount, string account);
    void print_balances();
};

#endif

I'm getting 2 errors under void Bank::print_balances(). It just says:

"no match for 'operator<<' in 'std::cout << ((Bank*)this) ->Bank::savings'"

I was reading a lot about it, but all I learned was since "checking" and "savings" are an account type, it won't work. My previous project similar, and I had "double" types instead so I was able to return a value.

Sorry if the format is wrong. First time posting on this site.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • 6
    If you want to output an `Account`, you have to [overload](http://stackoverflow.com/questions/4421706/operator-overloading) the operator to tell it how. – chris Apr 14 '14 at 15:18
  • 5
    How is the question related to the title? Other than that, you should put a bit more effort into simplifying the question. There is a lot of code there that is clearly unrelated (like the implementation of all other member functions in both `Bank` and `Account`), the `Bank` member definitions are provided *before* the declarations forcing others to scan back and forth in the code... – David Rodríguez - dribeas Apr 14 '14 at 15:19
  • Related question: http://stackoverflow.com/questions/10291385/overloading-operator-for-stdostream – CppChris Apr 14 '14 at 15:21

4 Answers4

3

You need to overload operator<< for your custom class Account in order to be able to cout<< its objects.

class Account {
    ...
public:
    friend ostream& operator<<(ostream &os, const Account &dt);
    ...
};

ostream& operator<<(ostream &os, const Account &dt)
{
    os << dt.balance; // print its balance
    return os;
}

To read on, check out Overloading the I/O operators.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
2

Check this page: Overloading the << Operator for your own class. It illustrates how you can get cout to print your class in exactly the way you want.

Lee White
  • 3,649
  • 8
  • 37
  • 62
2
  1. You do not returning anything in Bank::print_balances() (and you can not, because it is a void function)
  2. What you doing now is passing savings and checking (both of type Account) to std::cout as arguments of operator<<()
  3. The problem is that, by default, std::cout "knows" how to output only primitive types (such as int, float, double etc.) and some C++ standard library types (such as std::string).
  4. So, you have two options:

    • either output primitive types obtained from Account objects via getters:

      class Account {
            ...
            double GetBalance() const { return balance; }
            ...
      };
      
      void Bank::print_balances()
      {
           std::cout << savings.GetBalance();
           ...
      
    • or tell std::cout how to print Account objects, by overloading operator<<() (as described in other answers).

      std::ostream& operator<<(std::ostream& os, const Account& a)
      {
          os << a.GetBalance();
          return os;
      }
      
  5. You would never want to store money amounts in floating point variable! (only if you don't agree to loose some coins every time you making sum or multiply)

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • I'd rather do the 2nd option since Account.h and Bank.h cannot be modified any further. But what you said, does that mean I have to put the ostream code in Account.h within the class brackets? When I put it in my Bank.cpp file (the one with the initial error), it said my checkings and savings accounts were private (and I can't change that). Thank you –  Apr 14 '14 at 15:50
  • @user3532512 Usually we make `operator<<()` as free function. That means you put declaration in `Account.h` and definition in `Account .cpp`. But you can put it virtually anywhere. Still you must have possibility to get a value of `balance` which is `private` member. – Ivan Aksamentov - Drop Apr 14 '14 at 15:58
1

you don't have overloaded "<<" operator in your Account class. compiler doesn't know how and what to cout, you should overload insertion operator of Account class.

Bramsh
  • 389
  • 1
  • 2
  • 14