User Tools

Site Tools


c_-_c_operator_overloading:user_defined_extractor_operator

C - C++ Operator Overloading - User Defined Extractor Operator

#include <iostream> 
using namespace std; 
 
class MyClass 
{ 
  int x1, y1, z1; 
 
public: 
  MyClass(int a1, int b1, int c1) { x1 = a1; y1 = b1; z1= c1; } 
  friend ostream &operator<<(ostream &stream, MyClass obj); 
  friend istream &operator>>(istream &stream, MyClass &obj); 
}; 
 
 
ostream &operator<<(ostream &stream, MyClass obj) 
{ 
  stream << obj.x1 << ", "; 
  stream << obj.y1 << ", "; 
  stream << obj.z1 << "\n"; 
  return stream; // return the stream 
} 
 
 
istream &operator>>(istream &stream, MyClass &obj) 
{ 
  cout << "Enter X,Y,Z values: "; 
  stream >> obj.x1 >> obj.y1 >> obj.z1; 
  return stream; 
} 
 
 
int main() 
{ 
  MyClass a(1, 2, 3); 
 
  cout << a; 
 
  cin >> a; 
  cout << a; 
 
  return 0; 
}
c_-_c_operator_overloading/user_defined_extractor_operator.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki