Write a program to overload function call operator.
#include<iostream> using namespace std; class A { private: int x,y; public: void print(); A(); A(int x,int y); void operator ()(int,int); }; void A :: operator ()(int a,int b) { x = a; y = b; } A :: A() { cout << "default constructor" << endl; } A :: A(int a,int b):x(a),y(b) { cout << "parameterized constructor" << endl; } void A :: print() { cout << "x = " << x << " y = " << y << endl; } int main() { A obj1,obj2; obj1(12,35);//opbj1.operator()(12,35) obj2(34,46); obj1.print(); obj2.print(); return 0; }
Output
default constructor default constructor x = 12 y = 35 x = 34 y = 46