210k views
2 votes
Implement comparison operator overload for comparing the salary of Point object.

A) Overload the operator == to output boolean. Implement arithmetic operator overload for Point object.
B) Overload the operator +, which can add x and y coordinate of two Point Objects, and return a new temporary Point object.
c) Overload the operator +, which can add an integer to x and y coordinate of Point Objects, and return a new temporary Point object.
D) Overload the operator += to update x and y coordinate of Point, which can add x and y of a Point to x and y coordinate of another Point.
E) Overload the operator += to update x and y coordinate of Point, which can add an integer to x and y coordinate of Point.

User Zimmryan
by
8.3k points

1 Answer

6 votes

Final answer:

To compare the salary of a Point object, you can overload the == operator. To perform arithmetic operations on Point objects, you can overload the + operator. To update the coordinates of a Point object, you can overload the += operator.

Step-by-step explanation:

Comparison Operator Overload

To compare the salary of a Point object using the == operator, you can define the operator inside the Point class. The implementation should check if the salaries of the two Point objects are equal and return a boolean value accordingly. Here is an example:

bool operator==(const Point& p1, const Point& p2) {
return p1.salary == p2.salary;
}

Arithmetic Operator Overload

To overload the + operator, you can define it inside the Point class. The implementation should add the x and y coordinates of two Point objects and return a new Point object with the updated coordinates. Here is an example:

Point operator+(const Point& p1, const Point& p2) {
int newX = p1.x + p2.x;
int newY = p1.y + p2.y;
return Point(newX, newY);
}

To overload the + operator to add an integer to the x and y coordinates of a Point object, you can define a separate function outside the Point class. The implementation should add the integer to both coordinates and return a new Point object. Here is an example:

Point operator+(const Point& p, int value) {
int newX = p.x + value;
int newY = p.y + value;
return Point(newX, newY);
}

Operator += Overload

To overload the += operator to update the x and y coordinates of a Point object, you can define it inside the Point class. The implementation should add the x and y coordinates of another Point object to the current Point object's coordinates. Here is an example:

Point& operator+=(const Point& p) {
x += p.x;
y += p.y;
return *this;
}

To overload the += operator to add an integer to the x and y coordinates of a Point object, you can define a separate function outside the Point class. The implementation should add the integer to both coordinates of the Point object. Here is an example:

Point& operator+=(int value) {
x += value;
y += value;
return *this;
}

User Pankaj Khurana
by
7.9k points