News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

complete progarm for Overload << and >> in c++

Started by dhilipkumar, Nov 05, 2008, 06:47 PM

Previous topic - Next topic

dhilipkumar

complete progarm for Overload << and >> in c++


#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;
  }
  void getXY(int &i, int &j) {
     i=x;
     j=y;
  }
  MyClass operator<<(int i);
  MyClass operator>>(int i);
};

// Overload <<.
MyClass MyClass::operator<<(int i)
{
  MyClass temp;

  temp.x = x << i;
  temp.y = y << i;

  return temp;
}

// Overload >>.
MyClass MyClass::operator>>(int i)
{
  MyClass temp;

  temp.x = x >> i;
  temp.y = y >> i;

  return temp;
}

int main()
{
  MyClass object1(4, 4), object2;
  int x, y;

  object2 = object1 << 2;  // ob << int
  object2.getXY(x, y);
  cout << "(object1<<2) X: " << x << ", Y: " << y << endl;

  object2 = object1 >> 2; // ob >> int
  object2.getXY(x, y);
  cout << "(object1>>2) X: " << x << ", Y: " << y << endl;

  return 0;
}