Csharp/C Sharp/Class Interface/Operator Overloading — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:39, 26 мая 2010
Содержание
- 1 A better way to overload !, | and & for ThreeD. This version automatically enables the && and || operators
- 2 An example of operator overloading
- 3 Demonstrates overloading the addition operator for two class objects
- 4 illustrates operator overloading
- 5 More operator overloading
- 6 Operator Overloading:An Example
- 7 Overload addition for object + object, and for object + int
- 8 overloaded operator + takes two fractions
- 9 Overloaded operator: whether two Fractions are equal
- 10 Overload != operator
- 11 Overload shift operator
- 12 Overload the + for object + object, object + int, and int + object
- 13 Overload true and fase for ThreeD
- 14 Sorting and Searching:Overloading Relational Operators
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.");
}
}
An example of operator overloading
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// An example of operator overloading.
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 binary +.
public static ThreeD operator +(ThreeD op1, ThreeD op2)
{
ThreeD result = new ThreeD();
/* This adds together the coordinates of the two points
and returns the result. */
result.x = op1.x + op2.x; // These are integer additions
result.y = op1.y + op2.y; // and the + retains its original
result.z = op1.z + op2.z; // meaning relative to them.
return result;
}
// Overload binary -.
public static ThreeD operator -(ThreeD op1, ThreeD op2)
{
ThreeD result = new ThreeD();
/* Notice the order of the operands. op1 is the left
operand and op2 is the right. */
result.x = op1.x - op2.x; // these are integer subtractions
result.y = op1.y - op2.y;
result.z = op1.z - op2.z;
return result;
}
// Show X, Y, Z coordinates.
public void show()
{
Console.WriteLine(x + ", " + y + ", " + z);
}
}
public class ThreeDDemo {
public static void Main() {
ThreeD a = new ThreeD(1, 2, 3);
ThreeD b = new ThreeD(10, 10, 10);
ThreeD c = new ThreeD();
Console.Write("Here is a: ");
a.show();
Console.WriteLine();
Console.Write("Here is b: ");
b.show();
Console.WriteLine();
c = a + b; // add a and b together
Console.Write("Result of a + b: ");
c.show();
Console.WriteLine();
c = a + b + c; // add a, b and c together
Console.Write("Result of a + b + c: ");
c.show();
Console.WriteLine();
c = c - a; // subtract a
Console.Write("Result of c - a: ");
c.show();
Console.WriteLine();
c = c - b; // subtract b
Console.Write("Result of c - b: ");
c.show();
Console.WriteLine();
}
}
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 ("\tPoint is at " + point3);
Console.WriteLine ("\tstr = " + 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);
}
}
More operator overloading
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// More operator overloading.
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 binary +.
public static ThreeD operator +(ThreeD op1, ThreeD op2)
{
ThreeD result = new ThreeD();
/* This adds together the coordinates of the two points
and returns the result. */
result.x = op1.x + op2.x;
result.y = op1.y + op2.y;
result.z = op1.z + op2.z;
return result;
}
// Overload binary -.
public static ThreeD operator -(ThreeD op1, ThreeD op2)
{
ThreeD result = new ThreeD();
/* Notice the order of the operands. op1 is the left
operand and op2 is the right. */
result.x = op1.x - op2.x;
result.y = op1.y - op2.y;
result.z = op1.z - op2.z;
return result;
}
// Overload unary -.
public static ThreeD operator -(ThreeD op)
{
ThreeD result = new ThreeD();
result.x = -op.x;
result.y = -op.y;
result.z = -op.z;
return result;
}
// 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 ThreeDDemo1 {
public static void Main() {
ThreeD a = new ThreeD(1, 2, 3);
ThreeD b = new ThreeD(10, 10, 10);
ThreeD c = new ThreeD();
Console.Write("Here is a: ");
a.show();
Console.WriteLine();
Console.Write("Here is b: ");
b.show();
Console.WriteLine();
c = a + b; // add a and b together
Console.Write("Result of a + b: ");
c.show();
Console.WriteLine();
c = a + b + c; // add a, b and c together
Console.Write("Result of a + b + c: ");
c.show();
Console.WriteLine();
c = c - a; // subtract a
Console.Write("Result of c - a: ");
c.show();
Console.WriteLine();
c = c - b; // subtract b
Console.Write("Result of c - b: ");
c.show();
Console.WriteLine();
c = -a; // assign -a to c
Console.Write("Result of -a: ");
c.show();
Console.WriteLine();
a++; // increment a
Console.Write("Result of a++: ");
a.show();
}
}
Operator Overloading:An Example
/*
A Programmer"s Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 25 - Operator Overloading\An Example
// copyright 2000 Eric Gunnerson
using System;
struct RomanNumeral
{
public RomanNumeral(int value)
{
this.value = value;
}
public override string ToString()
{
return(value.ToString());
}
public static RomanNumeral operator -(RomanNumeral roman)
{
return(new RomanNumeral(-roman.value));
}
public static RomanNumeral operator +(
RomanNumeral roman1,
RomanNumeral roman2)
{
return(new RomanNumeral(
roman1.value + roman2.value));
}
public static RomanNumeral operator ++(
RomanNumeral roman)
{
return(new RomanNumeral(roman.value + 1));
}
int value;
}
public class OperatorOverloadingAnExample
{
public static void Main()
{
RomanNumeral roman1 = new RomanNumeral(12);
RomanNumeral roman2 = new RomanNumeral(125);
Console.WriteLine("Increment: {0}", roman1++);
Console.WriteLine("Addition: {0}", roman1 + roman2);
}
}
Overload addition for object + object, and for object + int
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/* Overload addition for object + object, and
for object + int. */
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 binary + for object + object.
public static ThreeD operator +(ThreeD op1, ThreeD op2)
{
ThreeD result = new ThreeD();
/* This adds together the coordinates of the two points
and returns the result. */
result.x = op1.x + op2.x;
result.y = op1.y + op2.y;
result.z = op1.z + op2.z;
return result;
}
// Overload binary + for object + int.
public static ThreeD operator +(ThreeD op1, int op2)
{
ThreeD result = new ThreeD();
result.x = op1.x + op2;
result.y = op1.y + op2;
result.z = op1.z + op2;
return result;
}
// Show X, Y, Z coordinates.
public void show()
{
Console.WriteLine(x + ", " + y + ", " + z);
}
}
public class ThreeDDemo2 {
public static void Main() {
ThreeD a = new ThreeD(1, 2, 3);
ThreeD b = new ThreeD(10, 10, 10);
ThreeD c = new ThreeD();
Console.Write("Here is a: ");
a.show();
Console.WriteLine();
Console.Write("Here is b: ");
b.show();
Console.WriteLine();
c = a + b; // object + object
Console.Write("Result of a + b: ");
c.show();
Console.WriteLine();
c = b + 10; // object + int
Console.Write("Result of b + 10: ");
c.show();
}
}
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());
}
}
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());
}
}
}
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 == (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("\nAssigning f4 to an int...");
int truncated = (int) f4;
Console.WriteLine("When you truncate f4 you get {0}",
truncated);
}
}
Overload shift operator
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Overload < and >.
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 <.
public static bool operator <(ThreeD op1, ThreeD op2)
{
if((op1.x < op2.x) && (op1.y < op2.y) && (op1.z < op2.z))
return true;
else
return false;
}
// Overload >.
public static bool operator >(ThreeD op1, ThreeD op2)
{
if((op1.x > op2.x) && (op1.y > op2.y) && (op1.z > op2.z))
return true;
else
return false;
}
// Show X, Y, Z coordinates.
public void show()
{
Console.WriteLine(x + ", " + y + ", " + z);
}
}
public class ThreeDDemo4 {
public static void Main() {
ThreeD a = new ThreeD(5, 6, 7);
ThreeD b = new ThreeD(10, 10, 10);
ThreeD c = new ThreeD(1, 2, 3);
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 > c) Console.WriteLine("a > c is true");
if(a < c) Console.WriteLine("a < c is true");
if(a > b) Console.WriteLine("a > b is true");
if(a < b) Console.WriteLine("a < b is true");
}
}
Overload the + for object + object, object + int, and int + object
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/* Overload the + for object + object,
object + int, and int + object. */
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 binary + for object + object.
public static ThreeD operator +(ThreeD op1, ThreeD op2)
{
ThreeD result = new ThreeD();
/* This adds together the coordinates of the two points
and returns the result. */
result.x = op1.x + op2.x;
result.y = op1.y + op2.y;
result.z = op1.z + op2.z;
return result;
}
// Overload binary + for object + int.
public static ThreeD operator +(ThreeD op1, int op2)
{
ThreeD result = new ThreeD();
result.x = op1.x + op2;
result.y = op1.y + op2;
result.z = op1.z + op2;
return result;
}
// Overload binary + for int + object.
public static ThreeD operator +(int op1, ThreeD op2)
{
ThreeD result = new ThreeD();
result.x = op2.x + op1;
result.y = op2.y + op1;
result.z = op2.z + op1;
return result;
}
// Show X, Y, Z coordinates.
public void show()
{
Console.WriteLine(x + ", " + y + ", " + z);
}
}
public class ThreeDDemo3 {
public static void Main() {
ThreeD a = new ThreeD(1, 2, 3);
ThreeD b = new ThreeD(10, 10, 10);
ThreeD c = new ThreeD();
Console.Write("Here is a: ");
a.show();
Console.WriteLine();
Console.Write("Here is b: ");
b.show();
Console.WriteLine();
c = a + b; // object + object
Console.Write("Result of a + b: ");
c.show();
Console.WriteLine();
c = b + 10; // object + int
Console.Write("Result of b + 10: ");
c.show();
Console.WriteLine();
c = 15 + b; // int + object
Console.Write("Result of 15 + b: ");
c.show();
}
}
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);
}
}
Sorting and Searching:Overloading Relational Operators
/*
A Programmer"s Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 28 - System.Array and the Collection Classes\Sorting and Searching\Overloading Relational Operators
// copyright 2000 Eric Gunnerson
using System;
public class OverloadingRelationalOperators
{
public static void Main()
{
Employee george = new Employee("George", 1);
Employee fred = new Employee("Fred", 2);
Employee tom = new Employee("Tom", 4);
Employee bob = new Employee("Bob", 3);
Console.WriteLine("George < Fred: {0}", george < fred);
Console.WriteLine("Tom >= Bob: {0}", tom >= bob);
}
}
public class Employee: IComparable
{
public Employee(string name, int id)
{
this.name = name;
this.id = id;
}
int IComparable.rupareTo(object obj)
{
Employee emp2 = (Employee) obj;
if (this.id > emp2.id)
return(1);
if (this.id < emp2.id)
return(-1);
else
return(0);
}
public static bool operator <(
Employee emp1,
Employee emp2)
{
IComparable icomp = (IComparable) emp1;
return(icomp.rupareTo (emp2) < 0);
}
public static bool operator >(
Employee emp1,
Employee emp2)
{
IComparable icomp = (IComparable) emp1;
return(icomp.rupareTo (emp2) > 0);
}
public static bool operator <=(
Employee emp1,
Employee emp2)
{
IComparable icomp = (IComparable) emp1;
return(icomp.rupareTo (emp2) <= 0);
}
public static bool operator >=(
Employee emp1,
Employee emp2)
{
IComparable icomp = (IComparable) emp1;
return(icomp.rupareTo (emp2) >= 0);
}
public override string ToString()
{
return(name + ":" + id);
}
string name;
int id;
}