15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

operator overloading ❘ 167<br />

whatever type you get when you use this operator. Adding two vectors results in a vector, therefore, the<br />

return type is also a Vector . For this particular override of the addition operator, the return type is<br />

the same as the containing class, but that is not necessarily the case, as you will see later in this example. The<br />

two parameters are the things you are operating on. For binary operators (those that take two parameters),<br />

such as the addition <strong>and</strong> subtraction operators, the fi rst parameter is the value on the left of the operator,<br />

<strong>and</strong> the second parameter is the value on the right.<br />

Note that it is convention to name your left-h<strong>and</strong> parameters lhs (for left-h<strong>and</strong> side)<br />

<strong>and</strong> your right-h<strong>and</strong> parameters rhs (for right-h<strong>and</strong> side).<br />

<strong>C#</strong> requires that all operator overloads be declared as public <strong>and</strong> static , which means that they are<br />

associated with their class or struct, not with a particular instance. Because of this, the body of the operator<br />

overload has no access to non - static class members <strong>and</strong> has no access to the this identifi er. This is fi ne<br />

because the parameters provide all the input data the operator needs to know to perform its task.<br />

Now that you underst<strong>and</strong> the syntax for the addition operator declaration, you can look at what happens<br />

inside the operator:<br />

{<br />

}<br />

Vector result = new Vector(lhs);<br />

result.x += rhs.x;<br />

result.y += rhs.y;<br />

result.z += rhs.z;<br />

return result;<br />

This part of the code is exactly the same as if you were declaring a method, <strong>and</strong> you should easily be able<br />

to convince yourself that this really will return a vector containing the sum of lhs <strong>and</strong> rhs as defi ned. You<br />

simply add the members x , y , <strong>and</strong> z together individually.<br />

Now all you need to do is write some simple code to test the Vector struct. Here it is:<br />

static void Main()<br />

{<br />

Vector vect1, vect2, vect3;<br />

}<br />

vect1 = new Vector(3.0, 3.0, 1.0);<br />

vect2 = new Vector(2.0, - 4.0, - 4.0);<br />

vect3 = vect1 + vect2;<br />

Console.WriteLine( “ vect1 = “ + vect1.ToString());<br />

Console.WriteLine( “ vect2 = “ + vect2.ToString());<br />

Console.WriteLine( “ vect3 = “ + vect3.ToString());<br />

Saving this code as Vectors.cs <strong>and</strong> compiling <strong>and</strong> running it returns this result:<br />

vect1 = ( 3, 3, 1 )<br />

vect2 = ( 2, - 4, - 4 )<br />

vect3 = ( 5, - 1, - 3 )<br />

adding More overloads<br />

In addition to adding vectors, you can multiply <strong>and</strong> subtract them <strong>and</strong> compare their values. In this section,<br />

you develop the Vector example further by adding a few more operator overloads. You will not develop the<br />

complete set that you ’ d probably need for a fully functional Vector type, but just enough to demonstrate<br />

some other aspects of operator overloading. First, you ’ ll overload the multiplication operator to support<br />

multiplying vectors by a scalar <strong>and</strong> multiplying vectors by another vector.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!