Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pre & Post Incrementing

Status
Not open for further replies.

RollerMan

Technical User
Jan 27, 2002
6
US
Could someone look at my code and see why it is not outputting what I would like for it to? I am having the same problem with post as I am with pre. I thought perhaps maybe someone could show me the light here on why I am having a problem. I have looked at several books any suggestions.

Main()
//pre
z = Fraction( 5, 2);
cout << &quot;\tFraction Z is: &quot; << z << endl;
//this should be 5/2
cout << &quot;\t++Z is: &quot; << ++z << &quot;, now Z is: &quot; <<z<< endl;
//this should be 7/2 and then 7/2
cout << &quot;\t--Z is: &quot; << --z << &quot;, now Z is: &quot; <<z<< endl;
//this should be 5/2 and then 5/2

Although it reads out as

//Fraction Z is: 5/2
// ++Z is: 7/2, now Z is: 5/2
// --Z is: 5/2, now Z is: 7/2

Here is the code for it:

Fraction Fraction::eek:perator ++ ()
{
numerator += denominator;
return *this;
}

Fraction Fraction::eek:perator --()
{
numerator -= denominator;
return *this;
}

Fraction Fraction::eek:perator ++ (int)
{
Fraction temp=*this;
numerator += denominator;
return temp;
}

Fraction Fraction::eek:perator --(int)
{
Fraction temp=*this;
numerator -= denominator;
return temp;
}


The header file:

under public
Fraction operator ++();
Fraction operator --();
Fraction operator ++(int);
Fraction operator --(int);

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top