Problem: The statement ++Chequebook1; should increment the member variable Balance of
Chequebook1 by R100. Give three different implementations for the overloaded operator ++
to accomplish this:
• using the member function Adjust()
• implementing the overloaded operator ++ as a friend function
• implementing the overloaded operator ++ as a member function

ive been struggling with this one for days, i havent came up with a correct solution but heres my code:
please help me :(

//Question2
#include <iostream>
#include <string>
using namespace std;

  class Chequebook
  {
    public:
        //constructors
        Chequebook();
        Chequebook(float amountBalance);

        //accessors/mutators
        void deposit(float amount);
        void withDraw(float amount);
        float currentBalance() const;
        void adjust();

        //operators
        friend ostream& operator <<(ostream& outs, const Chequebook& amount);
        friend Chequebook operator++(Chequebook& balance);

    private:
        float balance;
  };

  int main()
  {
    float amount;
    cout << "Enter the Account balance: ";
    cin >> amount;
    Chequebook Chequebook1(amount);



    cout << "Account balance: R " << Chequebook1.currentBalance() << endl;
    cout << "Enter amount to deposit:";
    cin >> amount;
    Chequebook1.deposit(amount);



    cout << "Balance after deposit: R" << Chequebook1.currentBalance() << endl;
    cout << "Enter amount to withdraw:";
    cin >> amount;
    Chequebook1.withDraw(amount);



    cout << "Balance after withdrawal: R" << Chequebook1 << endl;
    ++Chequebook1;
    cout << "Balance after adjusting: R" << Chequebook1;
    return 0;
  }

  //default constructor
  Chequebook::Chequebook()
  {
    balance = 0;
  }

  //overloaded constructor
  Chequebook::Chequebook(float accountBalance)
  {
    balance = accountBalance;
  }

  //deposit member function
  void Chequebook::deposit(float amount)
  {
    balance += amount;
  }

  //withdraw member function
  void Chequebook::withDraw(float amount)
  {
    balance -=amount;
  }

  float Chequebook::currentBalance() const
  {
     return balance;
  }

  void Chequebook::adjust()
  {
     balance += 100;
  }


  //stream insertion operator
  ostream &operator <<(ostream& outs, const Chequebook& amount)
  {
    outs << "Balance: " << amount;
    return outs;
  }


  // function definition
  Chequebook operator++(Chequebook& balance)
  {
     ++balance;
     return balance;
  }

Recommended Answers

All 2 Replies

Problem #1: you claim you're struggling with this code.
Problem #2: you give no indication what the struggle is
Problem #3: we are not psychic so have no idea what the struggle is
Solution#1: read the post at the top of the forum titled "Read This Before Posting A Question"
Solution#2: post again

After skimming over your code, i think cout << "Balance after withdrawal: R" << Chequebook1 << endl; should be cout << "Balance after withdrawal: R" << Chequebook1.currentBalance() << endl;
Which is in line 50. Same with line 52.

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.