overloaded operator + takes two fractions : 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 
overloaded operator + takes two fractions
overloaded operator + takes two fractions

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

     // return a string representation of the fraction
     public override string ToString()
     {
         String s = numerator.ToString() "/" +
             denominator.ToString();
         return s;
     }
 }


 public class TesterOverrideToString
 {
     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());
     }
 }

           
       
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: whether two Fractions are equalOverloaded operator: whether two Fractions are equal
11.Overload != operatorOverload != operator
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.