Use a friend inserter for objects of type MyClass. : Inserter « Overload « C++






Use a friend inserter for objects of type MyClass.

Use a friend inserter for objects of type MyClass.

#include <iostream>
using namespace std;

class MyClass {
  int x, y;
public:
  MyClass() { 
     x = 0; 
     y = 0; 
  }
  MyClass(int i, int j) { 
     x = i; 
     y = j; 
  }
  friend ostream &operator<<(ostream &stream, MyClass ob);
};

ostream &operator<<(ostream &stream, MyClass ob)
{
  stream << ob.x << ", " << ob.y << '\n';
  return stream;
}

int main()
{
  MyClass a(1, 1), b(10, 23);

  cout << a << b;

  return 0;
}



           
       








Related examples in the same category

1.Create a non-friend inserter.Create a non-friend inserter.
2.This program draws right trianglesThis program draws right triangles
3.PhoneNumber inserter functionPhoneNumber inserter function
4.An inserter can be used to output data in any formAn inserter can be used to output data in any form
5.Use friend function to operator: <<, >>Use friend function to operator: <<, >>
6.Output account info to a file using an inserter.Output account info to a file using an inserter.