A custom extractor (>>) : custom extractor « Operator Overloading « C++ Tutorial






#include <iostream> 
using namespace std; 
 
class MyClass { 
  int x, y, z; 
public: 
  MyClass(int a, int b, int c) { x = a; y = b; z = c; } 
  friend ostream &operator<<(ostream &stream, MyClass obj); 
  friend istream &operator>>(istream &stream, MyClass &obj); 
} ; 
 
ostream &operator<<(ostream &stream, MyClass obj) 
{ 
  stream << obj.x << ", "; 
  stream << obj.y << ", "; 
  stream << obj.z << "\n"; 
  return stream; // return the stream 
} 
 
istream &operator>>(istream &stream, MyClass &obj) 
{ 
  cout << "Enter X,Y,Z values: "; 
  stream >> obj.x >> obj.y >> obj.z; 
  return stream; 
} 
 
int main() 
{ 
  MyClass a(1, 2, 3); 
 
  cout << a; 
 
  cin >> a; 
  cout << a; 
 
  return 0; 
}
1, 2, 3
Enter X,Y,Z values: 1
2
3
1, 2, 3








10.19.custom extractor
10.19.1.A custom extractor (>>)