Overloaded Operator

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1734 Views - Last Post: 23 November 2009 - 05:17 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Overloaded Operator

Posted 20 November 2009 - 08:48 PM

using System;

class Complex
{
 private int x;
 private int y;
 public Complex()
 {
 }
 public Complex(int i, int j)
 {
  x = i;
  y = j;
 }

 public void ShowXY()
 {
  Console.WriteLine(\"{0} {1}\",x,y);
 }

 public static Complex operator +(Complex c1,Complex c2)
 {
  Complex temp = new Complex();
  temp.x = c1.x+c2.x;
  temp.y = c1.y+c2.y;
  return temp;
 }
}


class MyClient
{
 public static void Main()
 {
  Complex c1 = new Complex(10,20);
  c1.ShowXY(); // displays 10 & 20
  Complex c2 = new Complex(20,30);
  c2.ShowXY(); // displays 20 & 30
  Complex c3 = new Complex();
  c3 = c1 + c2;
  c3.ShowXY(); // dislplays 30 & 50
 }
}
 }
}


How anybody explain to me how this work. I know I have been keeping asking questions, which seems a bit annoying to someone. But I really want to learn.

Well what bugs me is when it comes to the parameter of the overloaded method, and I am just wondering:

1) What value it takes as a parameter to work with? Is it an object or something else?
2) What method calls send the arguments to the parameter of the overloaded object?
3) And when the overloaded method returns the value, where or to which the value is being retured?
4) Does it have anything to do with the constructors?

What I know is only the parameter of the overloaded method consists of the operands you might wanna work with. And hope I am not being felt annoying.

Is This A Good Question/Topic? 0
  • +

Replies To: Overloaded Operator

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Overloaded Operator

Posted 20 November 2009 - 10:01 PM

What you have there is operator overloading. There truly isn't a method call like you know it. Operator overloading is the ability to use operators: +, -. *. /, etc with classes that you create. You have there a class called Complex, which I assume is for complex numbers from math. Complex numbers have two parts, the real part and the imaginary part. It would be very nice if you could just add two complex numbers together using the + sign like you would for integers or strings.

What the program you posted does is overload the + operator so that you can take two instances of the Complex class and add them together using the + sign.

This is the code that says that you want to create an overload of the + operator:

 public static Complex operator +(Complex c1,Complex c2)
 {
  Complex temp = new Complex();
  temp.x = c1.x+c2.x;
  temp.y = c1.y+c2.y;
  return temp;
 }



The keyword operator says that you are not creating a method but instead you are creating an operator using the operator so you can write mathematical statements with the operator like in the line:

c3 = c1 + c2;



You could also overload the - operator so you could do"

c3 = c1 - c2;



What you would do is create an overload for the - operator using the same syntax but with a - instead of a + and you would implement the logic of subtracting two complex numbers, like this.

 public static Complex operator -(Complex c1,Complex c2)
 {
  Complex temp = new Complex();
  temp.x = c1.x-c2.x;
  temp.y = c1.y-c2.y;
  return temp;
 }


Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Overloaded Operator

Posted 20 November 2009 - 10:29 PM

If you are still lost you might want to try this tutorial here on DIC on operator overloading with C#.

http://www.dreaminco...topic121873.htm
Was This Post Helpful? 0
  • +
  • -

#4 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Overloaded Operator

Posted 20 November 2009 - 10:34 PM

I know but the question is the relationship between the object and the operator.

1) Is the variable or identifier within the parameter can be anything that we want? Or does it have to be related to certain things such as object? To be more clear, here is something I make up.

public static int operator +(user-defined type operand)

The question is we could use any variable for the operand or they have to be specific or relative to certain objects or static instance variable?

2) Do we always have to create or instantiate an object in order to do the calculation? such as this line of code
Complex temp = new Complex();?

3)At the end, there is a return statement, so where the value or result is being returned to?
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Overloaded Operator

Posted 21 November 2009 - 12:39 AM

1) Yes, it can be anything you want. You could overload the + operator like this if you wish:
public static int operator +(String a, FileStream B)

2) In the method, if it returns an object, yes, you should create a new object (though you don't have to do so). But, if you don't, your method could have unintended side effects that will be a major pain to discover.

One thing to remember before you decide that overloading an operator is "does this make logical sense to do this", or "When someone knows that types, will they know what it means to use this operator on them?" For complex types, yes, people will know that using + means add the complex types. For my example above, no one would have a clue what it means to add a string to a filestream.
Was This Post Helpful? 0
  • +
  • -

#6 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Overloaded Operator

Posted 21 November 2009 - 09:24 AM

Further questions added on, so if I have the code like this:

public static Complex operator +(Complex c1,Complex c2)
{
  Complex temp = new Complex();
  temp.x = c1.x+c2.x;
  temp.y = c1.y+c2.y;
  return temp;
}


1) where or how is the temp being returned? Since operator overloading doesn't need a method call?

2) In addition to the question above, do the parameters or the two operands c1 and c2 have anything to do with the objects created in main such as

Complex c1 = new Complex(10,20);
Complex c2 = new Complex(20,30);


If so, what's the relationship? Or are they related to the constructors or the arguments of the objects sent to the constructor? It is so hard to understand how this works. Because according to the principle of operator overloading, you could make the + operator to do subtraction or any other possible operation, not just addition. The thing is how the whole code work? And when does it execute in the program?





For the instance constructor, what you do is something like:

class access {
	int i, y;
 
	public access(int n, int x) {
		 i = n;
		 y = x;
   }
///...............

class MyClient {

	public static void Main() {
			 Complex c1 = new Complex(10,20);
//..............



As soon as the instance has been instantiated in this case new complex, the two numbers 10, 20 are sent to the class access constructor and then the parameters, which are n and x respectively will receive the arguments and then assign n and x to i and y.

Since static methods do not require any object qualification, so I assume, like SixOfEleven said, it doesn't have the method call. So here is what I am confused again, where does or do the operand or operands come from, or is it the syntax that requires an operand or operands to exist? Does it have any connection with the constructor or object like what I just mentioned:

As soon as the instance has been instantiated in this case new complex, the two numbers 10, 20 are sent to the class access constructor and then the parameters, which are n and x respectively will receive the arguments and then assign n and x to i and y.

Besides, it seems its operation and properties don't change even after having it overloaded, it just does the same thing like what it does for predefined type except that after it has been overloaded, you could add two object together.

I thought it was like after it is overloaded, the operator will be completely changed. For instance, I have it overloaded to have the functionality of subtraction. Instead of adding two numbers together, it would do the subtraction.

Something like this:
3+3 = 6 // This is addition.
3+3 = 0 //After having it overloaded to have the function of subtraction. But I don't think this is possible. But initially I thought that could be accomplished.

This post has been edited by kenryuakuma: 21 November 2009 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#7 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Overloaded Operator

Posted 21 November 2009 - 09:53 AM

View Postkenryuakuma, on 21 Nov, 2009 - 10:24 AM, said:

1) where or how is the temp being returned? Since operator overloading doesn't need a method call?


When you use the + operator like this:

c3 = c1 + c2;



When the program is run it sees the c1 + c2 and since you have an overload of the + operator the program know that it can add these two values using the + operator just like it could add two integers.

Quote

2) In addition to the question above, do the parameters or the two operands c1 and c2 have anything to do with the objects created in main such as

Complex c1 = new Complex(10,20);
Complex c2 = new Complex(20,30);


If so, what's the relationship? Or are they related to the constructors or the arguments of the objects sent to the constructor? It is so hard to understand how this works. Because according to the principle of operator overloading, you could make the + operator to do subtraction or any other possible operation, not just addition. The thing is how the whole code work? And when does it execute in the program?


Okay. Since c1 and c2 are both instances of the class Complex when the line c3 = c1 + c2 is reached it sees the + sign between the two objects of type Complex. It then knows, because you have designed an overload of the + sign, that it can add the two complex numbers together using the overload of the operator that you created. It is executed when your program gets to that point in your program.

Yes, you could make the overload of the + operator do anything. You could make it subtract, divide, multiply, whatever you want. The thing is when you design your class and you use operator over loading it is a good idea to have it make sense to the person using your class. For example, if you created the overload for the + operator and you made it subtract the person using your class would not understand why you would do that.


Quote

Since static methods do not require any object qualification, so I assume, like SixOfEleven said, it doesn't have the method call. So here is what I am confused again, where does or do the operand or operands come from, or is it the syntax that requires an operand or operands to exist? Does it have any connection with the constructor or object like what I just mentioned:


Static methods, in a class, require the name of the class to access them, unless the code is inside of the class. For example, if you have the following class:

class MyClass
{
	int number;

	public MyClass()
	{
	}

	public static void DoSomething()
	{
	}
}



To access the DoSomething method out side of the class you need to use the name of the class, MyClass, instead of an object of type MyClass.

MyClass myObject  = new MyClass();

myObject.DoSomething(); // This is wrong. You can't access it this way!
MyClass.DoSomething(); // This is how you access static methods




Quote

Besides, it seems its operation and properties don't change even after having it overloaded, it just does the same thing like what it does for predefined type except that after it has been overloaded, you could add two object together.


That is the point of it. Instead of having a method in the class like this and calling it in your program you can just add the two objects together. It is always a good idea to make your overload make sense though.

public static Complex Add(Complex num1, Complex num2)
{
	Complex result = new Complex();

	result.X = num1.X + num2.X;
	result.Y = num1.Y + num2.Y;

	return result;
}

// In your program
c3 = Complex.Add(c1, c2);




Quote

I thought it was like after it is overloaded, the operator will be completely changed. For instance, I have it overloaded to have the functionality of subtraction. Instead of adding two numbers together, it would do the subtraction.

Something like this:
3+3 = 6 // This is addition.
3+3 = 0 //After having it overloaded to have the function of subtraction. But I don't think this is possible. But initially I thought that could be accomplished.


You can do a lot of things in programming that you never should do. You have to write good code that will react in the right way. Having your code do something that is unexpected is never a good idea, like having an operator overload for + do subtraction. And, as Momerath said earlier:

Quote

One thing to remember before you decide that overloading an operator is "does this make logical sense to do this", or "When someone knows that types, will they know what it means to use this operator on them?"

Was This Post Helpful? 0
  • +
  • -

#8 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Overloaded Operator

Posted 21 November 2009 - 11:30 AM

Your explanation is really good and has cleared things up a bit. And now I am half way through. I understand half of it now.

But again the question is about the operands within the parameter of the overload operator method.. So to rephrase what you wrote, what invokes the overload operator method is the c3 = c1 + c2 right? So are c1 and c2 the objects passed to the parameter within the overload operator method as the two operands to be operated on?

c3 = c1 + c2;

public static Complex Add(Complex num1, Complex num2)

I know that's bad code, but would you please give me an example of + operator used as the - operator for subtraction. I would like to see how flexible the overload operator can be.
Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Overloaded Operator

Posted 21 November 2009 - 12:00 PM

Easy enough
public static Complex operator +(Complex c1, Complex c2) {
	Complex c3 = new Complex();
	c3.real = c1.real - c2.real;
	c3.imaginary = c1.imaginary - c2.imaginary;

	return c3;
}


That defines the + operater as subtraction for complex types. Don't do this, it's bad (though a good obfuscator will do these kind of substitutions to confuse people trying to decompile the code).
Was This Post Helpful? 0
  • +
  • -

#10 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Overloaded Operator

Posted 21 November 2009 - 03:55 PM

I know my question is gonna be long-winded, which it is indeed. Anyway.

I still have questions about the operands that are being operated, since this has not been answered yet.

1) Is the operands Complex c1, Complex c2 within the parameter take the two objects c1 and c2 as arguments? If that's true, then my assumption is correct and if not, it will just confuse me more.

2) Ok reuse these codes for our discussion again:

Complex c1 = new Complex(10,20);
Complex c2 = new Complex(20,30);[/inline]

[code]public static Complex operator +(Complex c1,Complex c2)
{
  Complex temp = new Complex();
  temp.x = c1.x+c2.x;
  temp.y = c1.y+c2.y;
  return temp;
}


so when we add the c1 + c2 together and assign to c3, which is c3 = c1 + c2; the expression c1 + c2 invokes the overload operator method and then c1 and c2 are passed to the overload operator method to be used as the operands to be operated on. But as soon as the program reaches the return temp;. Then the result of the addition of c1 and c2 is returned. So c3 = 30 + 50.

3) Now c3 is 80, so when we assign something like
c3 = c1 + c2 + c3, don't we have to create another operator overloading method? Because the overload operator+ method only have two operands in the parameter list. As far as I know, c1 + c2 + c3 will invokes the overload operator+ when the program reaches there, so it has three objects there, the overload operator methods need to have three operands.

4) Besides, by writing the code below how could the operator change its functionality? It just simply returns the value of the result of the calculation of two objects.

public static Complex operator +(Complex c1, Complex c2) {
	Complex c3 = new Complex();
	c3.real = c1.real - c2.real;
	c3.imaginary = c1.imaginary - c2.imaginary;

	return c3;
}


-See. If the c1.real = 10 and then the c2.real = 10, and the c1.imaginary = 20, and then c2.imaginary = 15, and after the calculation, the c3 contains, for real is 0, and then for imaginary 5, when it is returned, it only contains 0 and 5. If let's say, we have

result 3 = result 1 + result 2; it would just be like result 3 = 0 + 5, which is 5. How can the + operator becomes a subtraction operator?

This post has been edited by kenryuakuma: 21 November 2009 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#11 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Overloaded Operator

Posted 21 November 2009 - 04:36 PM

1) Yes

2) No, c3 is a complex type, so the result is (30,50i)

3) No, you can think of it as (c1 + c2) + c3. First it solves the (c1+c2) which returns a complex type, then it solves the result of the first + c3.

4) If you were given the problem (10, 20i) + (10, 15i) what would you expect the result to be? Unless you are crazy, you would get (20, 35i). But if you use our code, you would get (0, 5i) which is the result of subtracting the two complex numbers, not adding. Someone reading your code would have a hard time figuring out what went wrong with their addition.
Was This Post Helpful? 0
  • +
  • -

#12 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Overloaded Operator

Posted 21 November 2009 - 05:51 PM

I just don't really understand what the i next to the second number is such as (10, 20i) + (10, 15i)?

how come the complex type would return something like (30, 50!)?
Was This Post Helpful? 0
  • +
  • -

#13 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Overloaded Operator

Posted 21 November 2009 - 06:01 PM

View Postkenryuakuma, on 21 Nov, 2009 - 06:51 PM, said:

I just don't really understand what the i next to the second number is such as (10, 20i) + (10, 15i)?

how come the complex type would return something like (30, 50!)?


Complex number s are written in to parts. The first part is the real value and the second part is the imaginary value. In math when you write complex numbers they are written as follows:

10 + 20i



The i signifies that it is the imaginary part of the number.

The Complex number class would not return the i when it returns the number. What you would want to do, to allow the person using your code to write a complex number, is create an override of the ToString method that would write out the complex number as above.
Was This Post Helpful? 0
  • +
  • -

#14 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Overloaded Operator

Posted 21 November 2009 - 06:20 PM

1) Is the (10, 20i) + (10, 15i) expressions with the icon ! the math stuff, and it doesn't have anything to do with programming right?

About the question 4, I think I didn't express myself really clearly...Sorry about that Momerath. If possible, I would prefer either you or SixOfEleven to be my teacher...Anyway, putting the joke aside. :D

Yes...You are right about that, that would be crazy if the user use your program to calculate some stuff and then when they do the addition like 3 + 3, and the result should be 6, but then they get 0, that is something nobody wanna do.

2) But the question I would like to ask is how can adding a few statements like these would make the + operator become a subtraction(-) operator :

public static Complex operator +(Complex c1, Complex c2) {
	Complex c3 = new Complex();
	c3.real = c1.real - c2.real;
	c3.imaginary = c1.imaginary - c2.imaginary;

	return c3;
}


As you could see from the code that all we do is just calculation and return the result to the objects, it doesn't do anything to change the addition(+) into a subtraction(-) operator?

To be brief or accurate, how could those statements above make the +(addition) into +(the + operator become the subtraction operator)? This is what I don't understand.

This post has been edited by kenryuakuma: 21 November 2009 - 06:20 PM

Was This Post Helpful? 0
  • +
  • -

#15 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Overloaded Operator

Posted 21 November 2009 - 06:32 PM

1) Yes, the i has nothing to do with the way the code works. It is purely a real world thing. I would love to be a teacher to teach others. That is why I'm on DIC so much. :)

2) The reason is that the code you wrote does the opposite of what you expect it to do. It would like be writing a method called Add and doing subtraction in it rather than doing addition. C# doesn't care how you write the code to implement the operator overloading. It just sees that there are two objects of the Complex class and says: "Hey, there is an overload for the + operator to add these two objects together. I will go to that code and execute it."

Your job as a programmer is not just to write code. Your job as a programmer is to write code that will behave the way the person using your code expects it to. Just because you can do something in code doesn't mean that it is a good idea. For example it is possible to write code that would wipe out a hard drive and the person running the code wouldn't know that it would happen. That would be malware and could get you into serious trouble. You should also write code that behaves in an appropriate manner and will work the way others using code expect it to.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2