Overloaded operator: whether two Fractions are equal : Operator Overloading « Class Interface « C# / C Sharp






Overloaded operator: whether two Fractions are equal

Overloaded operator: whether two Fractions are equal
/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/

 using System;

 class Fraction
 {
     private int numerator;
     private int denominator;

     // create a fraction by passing in the numerator
     // and denominator
     public Fraction(int numerator, int denominator)
     {
         this.numerator=numerator;
         this.denominator=denominator;
     }

     // overloaded operator+ takes two fractions
     // and returns their sum
     public static Fraction operator+(Fraction lhs, Fraction rhs)
     {
         // like fractions (shared denominator) can be added
         // by adding thier numerators
         if (lhs.denominator == rhs.denominator)
         {
             return new Fraction(lhs.numerator+rhs.numerator,
                 lhs.denominator);
         }

         // simplistic solution for unlike fractions
         // 1/2 + 3/4 == (1*4) + (3*2) / (2*4) == 10/8
         // this method does not reduce.
         int firstProduct = lhs.numerator * rhs.denominator;
         int secondProduct = rhs.numerator * lhs.denominator;
         return new Fraction(
             firstProduct + secondProduct,
             lhs.denominator * rhs.denominator
             );
     }

     // test whether two Fractions are equal
     public static bool operator==(Fraction lhs, Fraction rhs)
     {
         if (lhs.denominator == rhs.denominator &&
             lhs.numerator == rhs.numerator)
         {
             return true;
         }
         // code here to handle unlike fractions
         return false;
     }
     // delegates to operator ==
     public static bool operator !=(Fraction lhs, Fraction rhs)
     {
         return !(lhs==rhs);
     }
     // tests for same types, then delegates
     public override bool Equals(object o)
     {
         if (! (o is Fraction) )
         {
             return false;
         }
         return this == (Fraction) o;
     }
     // return a string representation of the fraction
     public override string ToString()
     {
         String s = numerator.ToString() + "/" +
             denominator.ToString();
         return s;
     }
 }


 public class TesterOperatorOverride
 {
     static void Main()
     {
         Fraction f1 = new Fraction(3,4);
         Console.WriteLine("f1: {0}", f1.ToString());

         Fraction f2 = new Fraction(2,4);
         Console.WriteLine("f2: {0}", f2.ToString());

         Fraction f3 = f1 + f2;
         Console.WriteLine("f1 + f2 = f3: {0}", f3.ToString());

         Fraction f4 = new Fraction(5,4);
         if (f4 == f3)
         {
             Console.WriteLine("f4: {0} == F3: {1}",
                 f4.ToString(),
                 f3.ToString());
         }

         if (f4 != f2)
         {
             Console.WriteLine("f4: {0} != F2: {1}",
                 f4.ToString(),
                 f2.ToString());
         }


         if (f4.Equals(f3))
         {
             Console.WriteLine("{0}.Equals({1})",
                 f4.ToString(),
                 f3.ToString());
         }
     }
 }
           
       








Related examples in the same category

1.An example of operator overloadingAn example of operator overloading
2.More operator overloadingMore operator overloading
3.Overload addition for object + object, and for object + intOverload addition for object + object, and 
   for object + int
4.Overload the + for object + object, object + int, and int + objectOverload the + for object + object, 
   object + int, and int + object
5.Overload shift operatorOverload shift operator
6.Overload true and fase for ThreeDOverload true and fase for ThreeD
7.A better way to overload !, | and & for ThreeD. This version automatically enables the && and || operatorsA better way to overload !, | and & for ThreeD. 
   This version automatically enables the && and || operators
8.illustrates operator overloadingillustrates operator overloading
9.Demonstrates overloading the addition operator for two class objectsDemonstrates overloading the addition operator for two class objects
10.overloaded operator + takes two fractionsoverloaded operator + takes two fractions
11.Overload != operatorOverload != operator
12.Operator Overloading:An ExampleOperator Overloading:An Example
13.Sorting and Searching:Overloading Relational OperatorsSorting and Searching:Overloading Relational Operators