C++ Programming homework help

scoobydrvr

Junior Member
Joined
Apr 1, 2009
Messages
225
Reaction score
6
Points
0
Location
Central Ok.
Visit site
Lame, I know, but I'm taking a summer class and i've got an assignment that has me stumped.

I've got to overload the += and -= operator so that I can increase the value of a variable within an object by a value. For example:

SavingsAccount saver1;
saver1 += 234;

This needs to increment a data member called savingBalance belonging to the class SavingsAccount.

Wikipedia and all other sources I've tried have failed me. I'm hoping there are some professionals here that can offer me a hand... please?
 

scoobydrvr

Junior Member
Joined
Apr 1, 2009
Messages
225
Reaction score
6
Points
0
Location
Central Ok.
Visit site
thanks... believe me, the only reason i'm taking it is because I have to. It doesn't help that the one guy I know in the class has his phone off and my textbook is useless...
 

iRoNyX

Junior Member
Joined
Jun 13, 2009
Messages
27
Reaction score
2
Points
0
Location
Canada
Visit site
thanks... believe me, the only reason i'm taking it is because I have to. It doesn't help that the one guy I know in the class has his phone off and my textbook is useless...
A seasoned programmer/nerd here, this will be a good start for you to read:

C++ Operator Overloading Guidelines

Basically you'll want a function within your object similar to the following:

MyClass & MyClass:: operator+=(int addedValue) { // no space between :: and operator, had to do it to avoid smiley
// Do the member variable addition work here.
MyClass.yourVariable = MyClass.yourVariable + addedValue;
// or if you can "see" the variable globally
yourVariable = yourVariable + addedValue;
return *this;
}

Not gonna do the homework for you, but hope that helps!

-Dave
 

scoobydrvr

Junior Member
Joined
Apr 1, 2009
Messages
225
Reaction score
6
Points
0
Location
Central Ok.
Visit site
That helps immensely, but now I'm getting a syntax error "missing ' ; ' before ' . ' " on the line

SavingsAccount.savingBalance = SavingsAccount.savingBalace + addedValue;

It's producing two of those errors for this one line and I'm totally at a loss for this one...
 

scoobydrvr

Junior Member
Joined
Apr 1, 2009
Messages
225
Reaction score
6
Points
0
Location
Central Ok.
Visit site
fixed it.
Header:
class SavingsAccount
{
public:
...
SavingsAccount& operator +=(float newAdd)
...
}

.cpp:

SavingsAccount& SavingsAccount:: operator +=(newAdd)
{
savingBalance = savingBalance + newAdd;
return *this;
}


thanks dave for your help getting me started!
 

scoobydrvr

Junior Member
Joined
Apr 1, 2009
Messages
225
Reaction score
6
Points
0
Location
Central Ok.
Visit site
fixed it.
Header:
class SavingsAccount
{
public:
...
SavingsAccount& operator +=(float newAdd)
...
}

.cpp:

SavingsAccount& SavingsAccount:: operator +=(newAdd)
{
savingBalance = savingBalance + newAdd;
return *this;
}


thanks dave for your help getting me started!
 

oso2k

Member
Joined
Nov 26, 2007
Messages
570
Reaction score
12
Points
18
Location
Moorpark, CA
Visit site
That helps immensely, but now I'm getting a syntax error "missing ' ; ' before ' . ' " on the line

SavingsAccount.savingBalance = SavingsAccount.savingBalace + addedValue;

It's producing two of those errors for this one line and I'm totally at a loss for this one...

I see a typo on the RHS.

If its an instance member variable, then you shouldn't be using the class name:
Code:
savingsBalance = savingsBalance + addedValue;
or
Code:
this.savingsBalance = this.savingsBalance + addedValue;

However, if the savingBalance variable is a static class member variable, then you have the appropriate syntax:
Code:
SavingsAccount.savingsBalance = SavingsAccount.savingsBalance + addedValue;

In ironyx's example above, you were supposed to choose one or the other. Instance variables (and objects) can be created multiple times in a program (say like a database or web site). Static class member variables can only be created once and are only accessed via the class (SavingsAccount.) (if the program can only access savings account).
 
Top