Write a program to an overload extraction operator.
#include<iostream> using namespace std; class A { private: int x,y; public: A(){} A(int a,int b):x(a),y(b){} friend A& operator >> (istream &,A&); void print() { cout << "x = " << x << " y = " << y << endl; } }; A& operator >> (istream& in,A& ob) { int a,b; cout << "Enter value for x and y = "; in >> a >> b; ob.x = a,ob.y = b; } int main() { A obj1(12,34),obj2(72,42); cin >> obj1;//cin.operator >>(A&); cin >> obj2; obj1.print(); obj2.print(); return 0; }
Output
Enter value for x and y = 10 20 Enter value for x and y = 30 40 x = 10 y = 20 x = 30 y = 40