Handling Operations on C Built-In Types


Handling Operations on C# Built-In Types

For any given class and operator, an operator method can, itself, be overloaded. One of the most common reasons for this is to allow operations between a class type and other types of data, such as a built-in type. For example, once again consider the ThreeD class. To this point, you have seen how to overload the + so that it adds the coordinates of one ThreeD object to another. However, this is not the only way in which you might want to define addition for ThreeD. For example, it might be useful to add an integer value to each coordinate of a ThreeD object. Such an operation could be used to translate axes. To perform such an operation, you will need to overload + a second time, as shown here:

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

Notice that the second parameter is of type int. Thus, the preceding method allows an integer value to be added to each field of a ThreeD object. This is permissible because, as explained earlier, when overloading a binary operator, one of the operands must be of the same type as the class for which the operator is being overloaded. However, the other operand can be of any other type.

Here is a version of ThreeD that has two overloaded + methods:

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

The output from this program is shown here:

 Here is a: 1, 2, 3 Here is b: 10, 10, 10 Result of a + b: 11, 12, 13 Result of b + 10: 20, 20, 20

As the output confirms, when the + is applied to two objects, their coordinates are added together. When the + is applied to an object and an integer, the coordinates are increased by the integer value.

While the overloading of + just shown certainly adds a useful capability to the ThreeD class, it does not quite finish the job. Here is why. The operator+(ThreeD, int) method allows statements like this:

 ob1 = ob2 + 10;

It does not, unfortunately, allow ones like this:

 ob1 = 10 + ob2;

The reason is that the integer argument is the second argument, which is the right-hand operand. The trouble is that the preceding statement puts the integer argument on the left. To allow both forms of statements, you will need to overload the + yet another time. This version must have its first parameter as type int and its second parameter as type ThreeD. One version of the operator+( ) method handles object + integer, and the other handles integer + object. Overloading the + (or any other binary operator) this way allows a built-in type to occur on the left or right side of the operator. Here is a version of ThreeD that overloads the + operator as just described:

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

The output from this program is shown here:

 Here is a: 1, 2, 3 Here is b: 10, 10, 10 Result of a + b: 11, 12, 13 Result of b + 10: 20, 20, 20 Result of 15 + b: 25, 25, 25




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net