please help me with the following c++ problem

The questions below refer to the following class declaration:

#include <iostream>
#include <string>

using namespace std;

class Chequebook
{
public:
Chequebook();
Chequebook(float AccountBalance);
void Deposit (float Amount);
void WithDraw (float Amount);
float CurrentBalance() const;
void Adjust();
private:
float Balance;
};

int main()
{
cout << "Enter the Account balance:";
float amount;
cin >> amount;
Chequebook Chequebook1(amount);
cout << “Account balance: R “ << Chequebook1 << endl;
cout << “Enter amount to deposit:”;
cin >> amount;
Chequebook1.Deposit(amount);
cout << “Balance after deposit: R” << Chequebook1 << 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;
}

a. Give implementations of both the default constructor and the second constructor.
b. Implement the Deposit() and WithDraw() member functions. The Deposit() member function should increment the member variable Balance with the amount deposited, and the WithDraw() member function should decrement the member variable Balance with the amount withdrawn.
c. Implement the CurrentBalance() member function. It should return the current balance of the cheque book.
d. The member function Adjust() should increment the member variable Balance by R100. Give an implementation for this member function.
e. Overload the stream insertion operator as a friend function. It should write the balance of the account to the given output stream.
f. 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.

g. Run your program three times (each time with a different version of the overloaded operator ++; comment the other two versions out during each run) with the following input:
400
200
300

Last edited on
Hello sjomelo01,

First off do not use the same subject line for two different posts. It is very confusing.

Again I do not believe that you compiled this program before you posted the code or you would have asked about the errors that you received. All the errors I noticed had nothing to do with the actual problem.

Your use of ( “ ), which came out different on my IDE, should be ( " ). This makes a big difference to the compiler. So form line 25 on you need to use the proper quotes.

It looks like you are just starting into classes so this may help:
http://www.cplusplus.com/doc/tutorial/ Main page look for Classes and
http://www.cplusplus.com/doc/tutorial/classes/

You have been given 7 points (a through g) to complete. Work on each point one at a time.

If you want to show some initiative put the class definition in its own header file and the function definitions in its own ".cpp" file. These days I like to use the header file extension ".hpp" because ".h" could be confused with a C header file.

Point of information. posting a bit of code and what needs to be done or added to the original code makes it look like you want someone to do the work for you. And not posting an actual question along with not showing anything that you have done does not help.

Post a question or just say that you do not know what to do next and ask or direction.

Hope this helps,

Andy
Hi i was wondering if you ever completed this question as i am also having trouble with it.
There is no "having trouble" in programming. There are only exact problems. Once you describe your problem in detail, you might notice a solution too or at least others are able to suggest something.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

using namespace std;

class Chequebook
{
public:
Chequebook();
Chequebook(float AccountBalance);
void Deposit (float Amount);
void WithDraw (float Amount);
float CurrentBalance() const;
void Adjust();
private:
float Balance;
};

Chequebook::Chequebook()
{

}

Chequebook::Chequebook(float AccountBalance)
{
Balance = AccountBalance;
}

void Chequebook::Deposit(float Amount)
{
    Balance = Balance + Amount;
}

void Chequebook::WithDraw(float Amount)
{
    Balance = Balance - Amount;
}

float Chequebook::CurrentBalance() const
{
    return Balance;
}

void Chequebook::Adjust()
{
    Balance = Balance + 100;
}

int main()
{
cout << "Enter the Account balance:";
float amount;
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.CurrentBalance() << endl;
++Chequebook1;
Chequebook1.Adjust;
cout << "Balance after adjusting: R" << Chequebook1.CurrentBalance();
return 0;
}


Hi i an completely new to C++ and to useing this site.
This is the code i have so far.
Firstly i need help in knowing whether or not i did the questions correctly up until question d.
Secondly i don't know how to do question e yet and would like some assistance in understanding what to do here.

Thank you for your patience and understanding.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>

using namespace std;

class Chequebook
{
public:
Chequebook();
Chequebook(float AccountBalance);
void Deposit (float Amount);
void WithDraw (float Amount);
float CurrentBalance() const;
void Adjust();
void operator ++() { //overload the ++operator as member function
    Balance += 100;
}
friend ostream& operator<<(ostream& os, const Chequebook& someValue); 

private:
float Balance;
};

Chequebook::Chequebook()
{

}

Chequebook::Chequebook(float AccountBalance)
{
Balance = AccountBalance;
}

void Chequebook::Deposit(float Amount)
{
    Balance = Balance + Amount;
}

void Chequebook::WithDraw(float Amount)
{
    Balance = Balance - Amount;
}

float Chequebook::CurrentBalance() const
{
    return Balance;
}

void Chequebook::Adjust()
{
    Balance = Balance + 100;
}

ostream& operator<<(ostream& os, const Chequebook& someValue)  
{  
    os << someValue.Balance;
    return os;  
}  


int main()
{
cout << "Enter the Account balance:";
float amount;
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.CurrentBalance() << endl;
++Chequebook1;
cout << "Balance after ++: R" << Chequebook1.CurrentBalance() << endl;
Chequebook1.Adjust();
cout << "Balance after adjusting: R" << Chequebook1.CurrentBalance() << endl;
cout << Chequebook1; //ostream overload step e
return 0;
}
Last edited on
Topic archived. No new replies allowed.