4 Replies - 509 Views - Last Post: 26 November 2009 - 09:22 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

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

Object value displayed and false overload operator

Posted 26 November 2009 - 11:01 AM

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;  

	public static implicit operator int(ThreeD op1)
   {
		return op1.x * op1.y * op1.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.WriteLine("Here is a: {0}", (int) a); 
	Console.WriteLine("Here is b: {0}", (int) b); 
 
	c = a + b; // add a and b together 
	Console.Write("Result of a + b: {0}", (int) c); 
	
	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(); 
  } 
}


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

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.");


Anyway, the first batch of codes is all about how the values object are displayed and the second batch of codes is about the false overload operator.

Well...Usually when we want to display the value of the object, we usually do this and invoke our constructor by doing this. First, creating an object and along with some values: ThreeD a = new ThreeD(1, 2, 3);, and then the constructor being invoked, values being assigned to the instance variables. In this case, public ThreeD(int i, int j, int k) { x = i; y = j; z = k; }.

But the question is we usually invoke the method of the object class to display the object values and if we want to cast the values of the object, we have to declare another overload operator method, which is something like

public static explicit operator int(ThreeD op1)
{
  return op1.x * op1.y * op1.z;
}


for implicit overload operator we do something like this

public static implicit operator int(ThreeD op1)
{
  return op1.x * op1.y * op1.z;
}


1) But as you can see the Console.WriteLine("Here is a: {0}", (int) a);, it is displaying the object values. But if you take a closer look, this one will invoke the explicit overload operator, but we only have the implicit overload operator and we don't have the explicit overload operator. So how can the object values be displayed in this way? and I just don't understand.

public static bool operator true(ThreeD op) {
	Console.WriteLine("Yoohoo I am in trueee");
	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) {
	Console.WriteLine("Yoohoo I am in faaallseee");
	if((op.x == 0) && (op.y == 0) && (op.z == 0)) 
	  return true; // all coordinates are zero 
	else 
	  return false; 
  }


2) Just as you might expected, the statement if(a) Console.WriteLine("a is true."); will invoke the boolean overload operator, since a is within the if condition expression, by doing this, a is always assumed to be true. I know that if you overload true and false operators, you have to overload both of them. But what is the point of overloading the false overload operator since the overload true operator does all the work. And you could see from the example that if at least one of the coordinate is non-zero, it will return true, otherwise return false. Besides, WHAT INVOKES THE FALSE OVERLOAD OPERATOR since the expression like this if(a) will always invoke the true overload operator? I cannot find any related answer to my question about what will invoke the false overload operator.

This post has been edited by kenryuakuma: 26 November 2009 - 11:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Object value displayed and false overload operator

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Object value displayed and false overload operator

Posted 26 November 2009 - 12:20 PM

1) Implicit operator will be called if you don't have an explicit one. And Implicit operator should never throw an exception and never lose information. An explicit operator can do both (and you see it when you try to convert double to int implicitly). It is dependant on you, the coder, to make sure that implicit operators always work.

2) The complier optimizes your code, and sometimes it will reverse the check from if (a) to if (!a) internally if it determines that the code would be faster, smaller, or whatever you have optimization set at.

This holds true for all the pair operators (like == and !=, < and >=, etc.). This is why you must overload them in pairs.
Was This Post Helpful? 0
  • +
  • -

#3 kenryuakuma  Icon User is offline

  • D.I.C Regular

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

Re: Object value displayed and false overload operator

Posted 26 November 2009 - 03:59 PM

First of all, HAPPY THANKSGIVING Momerath! Turkey time!

Anyway, will the program generate error messages if there is no overload operator method of implicit or explicit if we do something like this Console.WriteLine("Here is a: {0}", (int) a);?

Besides, since the false overload operator is hardly called, I am just wondering what statements should be included within the body of the false overload operator method?
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

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

Re: Object value displayed and false overload operator

Posted 26 November 2009 - 06:37 PM

View Postkenryuakuma, on 26 Nov, 2009 - 04:59 PM, said:

First of all, HAPPY THANKSGIVING Momerath! Turkey time!

Anyway, will the program generate error messages if there is no overload operator method of implicit or explicit if we do something like this Console.WriteLine("Here is a: {0}", (int) a);?

Besides, since the false overload operator is hardly called, I am just wondering what statements should be included within the body of the false overload operator method?


If you try and cast it like that and there is no overloaded operator to do that you would more than likely get a compile time error. If you didn't you would surely get a run time exception.
Was This Post Helpful? 0
  • +
  • -

#5 kenryuakuma  Icon User is offline

  • D.I.C Regular

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

Re: Object value displayed and false overload operator

Posted 26 November 2009 - 09:22 PM

Cool...Now it is all clear up. Again, I owe you a thank SixOfEleven.

BTW...Happy Thanksgiving! I have also read your gaming tutorial, although not understand what they are really about...But the pictures speak the words. Really fantastic work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1