 /*
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;
}

// overload the constructor to create a
// fraction from a whole number
public Fraction(int wholeNumber)
{
Console.WriteLine("In constructor taking a whole number");
numerator = wholeNumber;
denominator = 1;
}

// convert ints to Fractions implicitly
public static implicit operator Fraction(int theInt)
{
Console.WriteLine("Implicitly converting int to Fraction");
return new Fraction(theInt);
}

// convert Fractions to ints explicitly
public static explicit operator int(Fraction theFraction)
{
Console.WriteLine("Explicitly converting Fraction to int");
return theFraction.numerator /
theFraction.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
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)
{
bool equality = lhs==rhs;
return !(equality);
}

// 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 TesterOverrideThree
{
static void Main()
{
Fraction f1 = new Fraction(3,4);
Fraction f2 = new Fraction(2,4);
Fraction f3 = f1 + f2;

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

Console.WriteLine("\nAssigning f4 to an int...");
int truncated = (int) f4;
Console.WriteLine("When you truncate f4 you get {0}",
truncated);
}
}

Related examples in the same category

 1 An example of operator overloading 2 More operator overloading 3 Overload addition for object + object, and for object + int 4 Overload the + for object + object, object + int, and int + object 5 Overload shift operator 6 Overload true and fase for ThreeD 7 A better way to overload !, | and & for ThreeD. This version automatically enables the && and || operators 8 illustrates operator overloading 9 Demonstrates overloading the addition operator for two class objects 10 overloaded operator + takes two fractions 11 Overloaded operator: whether two Fractions are equal 12 Operator Overloading:An Example 13 Sorting and Searching:Overloading Relational Operators 