C++ | Operator Overloading | Question 9
#include<iostream> using namespace std; class Point { private: int x, y; public: Point() : x(0), y(0) { } Point& operator()(int dx, int dy); void show() {cout << "x = " << x << ", y = " << y; } }; Point& Point::operator()(int dx, int dy) { x = dx; y = dy; return *this; } int main() { Point pt; pt(3, 2); pt.show(); return 0; } |
chevron_right
filter_none
(A) x = 3, y = 2
(B) Compiler Error
(C) x = 2, y = 3
Answer: (A)
Explanation: This a simple example of function call operator overloading.
The function call operator, when overloaded, does not modify how functions are called. Rather, it modifies how the operator is to be interpreted when applied to objects of a given type.
If you overload a function call operator for a class its declaration will have the following form:
return_type operator()(parameter_list)
Recommended Posts:
- C++ | Operator Overloading | Question 6
- C++ | Operator Overloading | Question 4
- C++ | Operator Overloading | Question 5
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 2
- C++ | Operator Overloading | Question 7
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 3
- C++ Program to concatenate two strings using Operator Overloading
- C++ program to compare two Strings using Operator Overloading
- C++ | Function Overloading and Default Arguments | Question 4
- C++ | Function Overloading and Default Arguments | Question 3
- C++ | Function Overloading and Default Arguments | Question 5
- C++ | Function Overloading and Default Arguments | Question 2



