We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,047 Members — Technology Publication meets Social Media

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:

``````//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;

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

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

{
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;
}
``````
3
Contributors
2
Replies
6 Hours
Discussion Span
8 Months Ago
Last Updated
3
Views
loshan.moonsamy
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0
Skill Endorsements: 0

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Skill Endorsements: 37

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.

np complete
Posting Whiz
385 posts since Sep 2010
Reputation Points: 18