22.1 Explaining operator overloading


In both Java and C#, the binary * operator has always been used to obtain the product of two numerical values, which are the two operands. The code below shows a class which represents a fraction with two int fields “ numerator and denominator . I have added a constructor which takes in two int s to initialize these two fields:

 1: class Fraction{  2:   public int numerator;  3:   public int denominator;  4:  5:   // constructor  6:   public Fraction (int numerator, int denominator){  7:     this.numerator = numerator;  8:     this.denominator = denominator;  9:   } 10: } 

When you want to perform a fraction multiplication, you can write a static method, called Multiply , which takes in two Fractions and which returns a resultant new Fraction object with the correct values stored in numerator and denominator . We can assume that the Multiply method below is inside the Fraction class.

 30: public static Fraction Multiply(Fraction f1, Fraction f2){ 31:   int newNumerator = f1.numerator * f2.numerator; 32:   int newDenominator = f1.denominator * f2.denominator; 33:   Fraction result = 34:             new Fraction (newNumerator, newDenominator); 35:   return result; 36: } 

We can also write a Main method to check if our Multiply method works.

 40: public static void Main(){ 41:   Fraction f1 = new Fraction(1,2); 42:   Fraction f2 = new Fraction(3,4); 43:   Fraction r = Fraction.Multiply(f1,f2); 44:   Console.WriteLine(r.numerator + "/" + r.denominator); 45: } 

Output:

 c:\expt>test 3/8 

Everything works fine, but wouldn't it have been neater (and more intuitive) if you could perform the multiplication using the * operator?

This is definitely more intuitive:

 Fraction r = f1*f2; 

than this:

 Fraction r = Fraction.Multiply(f1,f2); 

You can do that if you overload the * operator. When you do this, you are trying to define different results when you apply the operator to different types of operands. In this case, what you have to do is to write a method containing the multiplication logic like this:

 public static Fraction operator * (Fraction f1, Fraction f2){   // logic for Multiplication } 

The whole program looks like this.

 1: using System;  2:  3: class Fraction{  4:   public int numerator;  5:   public int denominator;  6:  7:  public static Fraction  8:  operator * (Fraction f1, Fraction f2){  9: 10:  int newNumerator  =  f1.numerator * f2.numerator;  11:  int newDenominator  =  f1. denominator * f2.denominator;  12:  Fraction result  = 13:  new Fraction (newNumerator,newDenominator);  14:  return result;  15:  }  16: 17:   public Fraction (int numerator, int denominator){ 18:     this.numerator = numerator; 19:     this.denominator = denominator; 20:   } 21: 22:   public static void Main(){ 23:     Fraction f1 = new Fraction(1,2); 24:     Fraction f2 = new Fraction(3,4); 25:  Fraction r  =  f1*f2;  26:     Console.WriteLine(r.numerator + "/" + r.denominator); 27:   } 28: } 

That's operator overloading! You have overloaded the * operator so that when you supply two operands of type Fraction , it executes a custom-made method, and returns a result.

Lines 7 “ 15 define the overloading method for the * operator. * can now take in two Fraction operands and return a Fraction object.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net