+ 2

C# Operator-Overloading Lesson Exercise 2

Does not allow correct answer. Am I wrong?

26th May 2019, 2:03 PM
Bo Petrov
Bo Petrov - avatar
6 Respostas
+ 30
You have explanation in the COMMENTS : ➝ No, it's not 'Box'. Don't be confused by an example on the previous page. It was Box there, because the operator returned a Box object. Here it returns a boolean value, so it's 'bool'. public static bool operator > (Box a, Box b) { if (a.Height*a.Width > b.Height*b.Width) return true; else return false; }
26th May 2019, 4:46 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 23
David Carroll 😊Thank you!👍🍻
26th May 2019, 6:33 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 6
Now, consider the next statements using comparison operators: //Comparison Operators var comparisonA = w1 > w2; var comparisonB = A > B; Both statements are comparing the first item with the second item. If the first is greater than the second, return true, otherwise, return false. The return type is determined based on the statement using an arithmetic operator or comparison operator.
26th May 2019, 5:21 PM
David Carroll
David Carroll - avatar
+ 5
Thank you both. My apologies i was not diligent enough in seeking the answer before i asked and for not being precise enough with my question. I will endeavor to do better next time.
26th May 2019, 5:03 PM
Bo Petrov
Bo Petrov - avatar
+ 5
Bo Petrov No worries... Others seem to have been confused with this as well. 😉 I just posted this explanation in the comments for that Quiz question to help others with this... For those confused about why overloading the > comparison operator returns bool instead of Box like the with + arithmetic operator, consider the following rationale: What return type is expected with the statements below using arithmetic operators? int w1 = 3; int h1 = 6; int w2 = 4; int h2 = 5; Box A = new Box(w1, h1); Box B = new Box(w2, h2); //Arithmetic Operators var bigger_number = w1 + w2; var bigger_box = A + B; The first statement expects an integer and the second statement expects a Box. Adding two of the same type would result in the same type. (continued...)
26th May 2019, 5:21 PM
David Carroll
David Carroll - avatar
+ 4
Can you post the question?
26th May 2019, 4:15 PM
Denise Roßberg
Denise Roßberg - avatar