Overloading operator+

I cant put operator+ in the class so I'm lost.

1
2
3
4
5
6
7
8
9
10
Rational operator+( const Rational& A, const Rational B )          
{
   int Num;
   int Den;
  
   Num = A.Numerator()*B.Denominator()+A.Denominator()*B.Numerator();
   Den = A.Denominator()*B.Denominator();

   return Rational C(Num, Den);
}


I'm also having trouble with my assignment operator
1
2
3
4
5
6
7
8
Rational& Rational::operator=(const Rational& C )
{
   Numerator_ = C.Numerator_;
   Denominator_ = C.Denominator_;

   return *this;
  
}
Last edited on
I don't see anything wrong with this code (other than maybe intending const Rational& B on line 1 of the + bit).

What errors are you getting?
Line 9 -- remove the C.

 
return Rational( Num, Den );


You could make operator+ for Rational a friend function so you can access Numerator_ and Denominator_ directly without going through the accessor methods. Just a thought.

operator= looks right to me.
Topic archived. No new replies allowed.