Overload != operator : Operator Overloading « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Operator OverloadingScreenshots 
Overload != operator
Overload != operator

/*
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
         // 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)
     {
         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 == (Fractiono;
     }

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

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

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

           
       
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.Overloaded operator: whether two Fractions are equalOverloaded operator: whether two Fractions are equal
12.Operator Overloading:An ExampleOperator Overloading:An Example
13.Sorting and Searching:Overloading Relational OperatorsSorting and Searching:Overloading Relational Operators
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.