Problems
Below is a faithful English translation of the entire text.
Suppose we have a structure Data consisting of two integers x and y.
And suppose we declare two variables a and b of type Data.
struct Data{
int x, y;
};
Data a, b;
Now, all three of the following lines result in compile errors. Why?
if(a < b) { ... } // compile error
if(a == b) { ... } // compile error
Data c = a + b; // compile error
From the computer’s point of view, it does not know:
what condition should make two structures equal (
==),what rule should be used to compare their sizes (
<),or how two structures should be added together (
+).
For example, in the expression a < b, the computer has no idea whether it should compare x, or y, or something else.
Therefore, when we want to use such operators, the programmer must explicitly and kindly tell the computer how the operation should be performed.
This is called operator overloading.
This is not a concept that exists only for structures, but it is most commonly used with structures.
Below is an example of defining the comparison operator < using this template.
(In this example, the comparison criterion is the value of x.)
Let’s understand this code.
The < operator asks:
“Is the value on the left smaller than the value on the right?”
For example, a < b asks:
“Is a smaller than b?”
The answer to this question is either true or false.
Therefore, the return type must be bool, which is why bool appears at the beginning.
The rest of the syntax is something that inevitably requires a bit of memorization.
At first, just follow along and type it out. Gradually, you will memorize it naturally—this is code you will use countless times in the future.
Here is the code that defines the < operator inside the Data structure.
struct Data{
int x, y;
bool operator < ( const Data &Right ) const{
return x < Right.x;
}
};
The variable name does not have to be Right.
It is chosen simply to make the meaning clearer.
As mentioned earlier, the < operator compares “this object and the object to its right.”
Therefore, naming the parameter Right makes it easier to understand.
In this code, the comparison is based on the value of x.
The statement
return x < Right.x;
means that we compare the current object’s x value with the x value of the object on the right.
After defining < like this, the expression if (a < b) will no longer cause a compile error.
The computer now knows how to handle <.
Defining ==
Applying the same idea, how would we define ==?
The == operator also asks a question:
“Are this object and the object on the right equal?”
Again, the result is true or false, so the return type is bool.
bool operator == ( const Data &Right ) const{
return x == Right.x && y == Right.y;
}
This code considers two structures equal only if both x and y are equal.
Defining +
Finally, how do we define the + operator?
Suppose that when adding two Data objects, we want to add x values together and y values together.
In other words:
(3, 5) + (1, 4) = (3 + 1, 5 + 4) = (4, 9)
In this case, the result is not a bool.
The result is (4, 9), which is of type Data.
Therefore, we write Data instead of bool at the beginning.
Data operator + ( const Data &Right ) const{
return (Data){ x + Right.x , y + Right.y };
}
Practice Problem
Define a structure Rect (rectangle) consisting of two integers width and height.
Then declare four variables a, b, c, and d of type Rect.
struct Rect{
int width, height;
} a, b, c, d;
Inside the main function, read the width and height of a, b, c, and d.
int main(){
scanf("%d %d", &a.width, &a.height);
scanf("%d %d", &b.width, &b.height);
scanf("%d %d", &c.width, &c.height);
scanf("%d %d", &d.width, &d.height);
}
From this point on, you must complete the code yourself.
You will compare the areas of the rectangles (a + b) and (c + d).
When two rectangles are added, their widths are added together and their heights are added together.
That is, if a rectangle is represented as (width, height), anda = (4, 8) and b = (1, 2), then:
a + b = (5, 10)
Output:
Left Smallif the area of(a + b)is smaller,Right Smallif the area of(c + d)is smaller,Sameif the two areas are equal.
In other words, the comparison criterion is area.
Be sure, for the sake of improving your skills, to solve this by defining the +, <, and == operators inside the Rect structure.
The area of a rectangle is, of course, width * height.
The overall structure of a model solution would look like this:
if(a + b == c + d) // print "Same"
else if(a + b < c + d) // print "Left Small"
else // print "Right Small"Input
The width and height of a, b, c, and d are input over 4 lines.
These are all natural numbers less than or equal to 10,000.
Output
Compare as requested in the problem and output the result.
Example #1
3 6
2 3
1 1
2 14
Same
a + b = (5, 9)
c + d = (3, 15)
The areas of the two rectangles are equal to 45.
Example #2
1 2
3 4
1 3
2 4
Right Small
a + b = (4, 6)
c + d = (3, 7)
The area of c + d is smaller.
Example #3
2 5
4 1
2 3
3 5
Left Small
a + b = (6, 6)
c + d = (5, 8)
The area of a + b is smaller.