/* 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 == (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; Console.WriteLine("adding f3 + 5..."); Fraction f4 = f3 + 5; Console.WriteLine("f3 + 5 = f4: {0}", f4.ToString()); Console.WriteLine(" Assigning f4 to an int..."); int truncated = (int) f4; Console.WriteLine("When you truncate f4 you get {0}", truncated); } }
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
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()); } } }
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()); } }
Demonstrates overloading the addition operator for two class objects
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Plus.cs -- demonstrates overloading the addition operator for two // class objects. // // Compile this program with the following command line: // C:>csc Plus.cs // namespace nsOverload { using System; public class PlusclsMain { static public void Main () { clsPoint point1 = new clsPoint (12, 28, "This is part"); clsPoint point2 = new clsPoint (42, 64, " of a string"); clsPoint point3 = point1 + point2; Console.WriteLine ("Results for point3:"); Console.WriteLine (" Point is at " + point3); Console.WriteLine (" str = " + point3.str); } } class clsPoint { public clsPoint () { } public clsPoint (int x, int y, string str) { m_cx = x; m_cy = y; this.str = str; } private int m_cx = 0; private int m_cy = 0; public int cx { get {return (m_cx);} set {m_cx = value;} } public int cy { get {return (m_cy);} set {m_cy = value;} } public string str = ""; static public clsPoint operator +(clsPoint pt1, clsPoint pt2) { clsPoint point = new clsPoint(); point.cx = pt1.cx + pt2.cx; point.cy = pt1.cy + pt2.cy; point.str = pt1.str + pt2.str; return (point); } public override string ToString() { return ("(" + m_cx + "," + m_cy + ")"); } } }
illustrates operator overloading
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example7_12.cs illustrates operator overloading */ using System; // declare the Rectangle class class Rectangle { // declare the fields public int width; public int height; // define constructor public Rectangle(int width, int height) { this.width = width; this.height = height; } // override the ToString() method public override string ToString() { return "width = " + width + ", height = " + height; } // overload the == operator public static bool operator ==(Rectangle lhs, Rectangle rhs) { Console.WriteLine("In operator =="); if (lhs.width == rhs.width && lhs.height == rhs.height) { return true; } else { return false; } } // overload the != operator public static bool operator !=(Rectangle lhs, Rectangle rhs) { Console.WriteLine("In operator !="); return !(lhs==rhs); } // override the Equals() method public override bool Equals(object obj) { Console.WriteLine("In Equals()"); if (!(obj is Rectangle)) { return false; } else { return this == (Rectangle) obj; } } // overload the + operator public static Rectangle operator +(Rectangle lhs, Rectangle rhs) { Console.WriteLine("In operator +"); return new Rectangle(lhs.width + rhs.width, lhs.height + rhs.height); } } public class Example7_12 { public static void Main() { // create Rectangle objects Rectangle myRectangle = new Rectangle(1, 4); Console.WriteLine("myRectangle: " + myRectangle); Rectangle myRectangle2 = new Rectangle(1, 4); Console.WriteLine("myRectangle2: " + myRectangle2); if (myRectangle == myRectangle2) { Console.WriteLine("myRectangle is equal to myRectangle2"); } else { Console.WriteLine("myRectangle is not equal to myRectangle2"); } Rectangle myRectangle3 = myRectangle + myRectangle2; Console.WriteLine("myRectangle3: " + myRectangle3); } }
A better way to overload !, | and & for ThreeD. This version automatically enables the && and || operators
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /* A better way to overload !, |, and & for ThreeD. This version automatically enables the && and || operators. */ using System; // A three-dimensional coordinate class. class ThreeD { int x, y, z; // 3-D coordinates public ThreeD() { x = y = z = 0; } public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } // Overload | for short-circuit evaluation. public static ThreeD operator |(ThreeD op1, ThreeD op2) { if( ((op1.x != 0) || (op1.y != 0) || (op1.z != 0)) | ((op2.x != 0) || (op2.y != 0) || (op2.z != 0)) ) return new ThreeD(1, 1, 1); else return new ThreeD(0, 0, 0); } // Overload & for short-circuit evaluation. public static ThreeD operator &(ThreeD op1, ThreeD op2) { if( ((op1.x != 0) && (op1.y != 0) && (op1.z != 0)) & ((op2.x != 0) && (op2.y != 0) && (op2.z != 0)) ) return new ThreeD(1, 1, 1); else return new ThreeD(0, 0, 0); } // Overload !. public static bool operator !(ThreeD op) { if(op) return false; else return true; } // Overload true. public static bool operator true(ThreeD op) { if((op.x != 0) || (op.y != 0) || (op.z != 0)) return true; // at least one coordinate is non-zero else return false; } // Overload false. public static bool operator false(ThreeD op) { if((op.x == 0) && (op.y == 0) && (op.z == 0)) return true; // all coordinates are zero else return false; } // Show X, Y, Z coordinates. public void show() { Console.WriteLine(x + ", " + y + ", " + z); } } public class TrueFalseDemo1 { public static void Main() { ThreeD a = new ThreeD(5, 6, 7); ThreeD b = new ThreeD(10, 10, 10); ThreeD c = new ThreeD(0, 0, 0); Console.Write("Here is a: "); a.show(); Console.Write("Here is b: "); b.show(); Console.Write("Here is c: "); c.show(); Console.WriteLine(); if(a) Console.WriteLine("a is true."); if(b) Console.WriteLine("b is true."); if(c) Console.WriteLine("c is true."); if(!a) Console.WriteLine("a is false."); if(!b) Console.WriteLine("b is false."); if(!c) Console.WriteLine("c is false."); Console.WriteLine(); Console.WriteLine("Use & and |"); if(a & b) Console.WriteLine("a & b is true."); else Console.WriteLine("a & b is false."); if(a & c) Console.WriteLine("a & c is true."); else Console.WriteLine("a & c is false."); if(a | b) Console.WriteLine("a | b is true."); else Console.WriteLine("a | b is false."); if(a | c) Console.WriteLine("a | c is true."); else Console.WriteLine("a | c is false."); Console.WriteLine(); // now use short-circuit ops Console.WriteLine("Use short-circuit && and ||"); if(a && b) Console.WriteLine("a && b is true."); else Console.WriteLine("a && b is false."); if(a && c) Console.WriteLine("a && c is true."); else Console.WriteLine("a && c is false."); if(a || b) Console.WriteLine("a || b is true."); else Console.WriteLine("a || b is false."); if(a || c) Console.WriteLine("a || c is true."); else Console.WriteLine("a || c is false."); } }
Overload true and fase for ThreeD
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Overload true and fase for ThreeD. using System; // A three-dimensional coordinate class. class ThreeD { int x, y, z; // 3-D coordinates public ThreeD() { x = y = z = 0; } public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } // Overload true. public static bool operator true(ThreeD op) { if((op.x != 0) || (op.y != 0) || (op.z != 0)) return true; // at least one coordinate is non-zero else return false; } // Overload false. public static bool operator false(ThreeD op) { if((op.x == 0) && (op.y == 0) && (op.z == 0)) return true; // all coordinates are zero else return false; } // Overload unary --. public static ThreeD operator --(ThreeD op) { // for ++, modify argument op.x--; op.y--; op.z--; return op; } // Show X, Y, Z coordinates. public void show() { Console.WriteLine(x + ", " + y + ", " + z); } } public class TrueFalseDemo { public static void Main() { ThreeD a = new ThreeD(5, 6, 7); ThreeD b = new ThreeD(10, 10, 10); ThreeD c = new ThreeD(0, 0, 0); Console.Write("Here is a: "); a.show(); Console.Write("Here is b: "); b.show(); Console.Write("Here is c: "); c.show(); Console.WriteLine(); if(a) Console.WriteLine("a is true."); else Console.WriteLine("a is false."); if(b) Console.WriteLine("b is true."); else Console.WriteLine("b is false."); if(c) Console.WriteLine("c is true."); else Console.WriteLine("c is false."); Console.WriteLine(); Console.WriteLine("Control a loop using a ThreeD object."); do { b.show(); b--; } while(b); } }