Skip to main content

Exercises 20.9 Exercises

1.

Write an == operator that determines if two EvenNumber objects are equal. We will define them as being equal if they have the same value (seems logical enough). The operator is declared in the class, you need to implement it after the class declaration. (Don’t forget EvenNumber:: before the function name operator==.)

2.

Write an + operator that adds two EvenNumbers. The operator is declared in the class, you need to implement it after the class declaration.

3.

A Person has a firstName and lastName. Implement operator< (outside the class) to compare people by their first and last names according to this logic:
  • If the current person’s last name is < the other person’s last name, return true.
  • If the last names are equal, compare the first names. If the current person’s first name is < the other person’s first name, return true.
  • In all other cases, return false.

4.

Write a prefix ++ operator that changes an EvenNumber to the next even number (adds two to its value). The operator is declared in the class, you need to implement it after the class declaration.
Hint 1.
This is the easy case, just change the value of the current object and then return it.
Hint 2.
*this is β€œthe current object”

5.

Write a postfix ++ operator that changes an EvenNumber to the next even number (adds two to its value). The operator is declared in the class, you need to implement it after the class declaration.
Remember that the (int) is not really a parameter to use... it is just a hint to the compiler that this is the postfix ++.
Hint.
This is the complex version. You need to make a copy, modify the current object, and return the copy.
You have attempted of activities on this page.