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;
}