Skip to content

Instantly share code, notes, and snippets.

@somratcste
Created September 30, 2013 21:20
Show Gist options
  • Save somratcste/6770447 to your computer and use it in GitHub Desktop.
Save somratcste/6770447 to your computer and use it in GitHub Desktop.
How can we overload the Binary Operator this such type of program are described here.
#include <iostream>
using namespace std;
class coord
{
int x,y;
public :
coord(){x=0;y=0;}
coord(int i,int j){x=i;y=j;}
void get_xy(int &i,int &j){i=x;j=y;}
coord operator +(coord ob2);
coord operator -(coord ob2);
coord operator *(coord ob2);
coord operator /(coord ob2);
coord operator =(coord ob2);
};
coord coord::operator +(coord ob2)
{
coord temp;
temp.x=x+ob2.x;
temp.y=y+ob2.y;
return temp;
}
coord coord::operator -(coord ob2)
{
coord temp;
temp.x=x-ob2.x;
temp.y=y-ob2.y;
return temp;
}
coord coord::operator *(coord ob2)
{
coord temp;
temp.x=x*ob2.x;
temp.y=y*ob2.y;
return temp;
}coord coord::operator /(coord ob2)
{
coord temp;
temp.x=x/ob2.x;
temp.y=y/ob2.y;
return temp;
}
coord coord::operator =(coord ob2)
{
coord temp;
x=ob2.x;
y=ob2.y;
return *this;
}
int main()
{
coord o1(10,10),o2(5,3),o3;
int x,y;
o3=o1+o2;
o3.get_xy(x,y);
cout<<x<<" "<<y;
cout<<endl;
o3=o1-o2;
o3.get_xy(x,y);
cout<<x<<" "<<y;
cout<<endl;
o3=o1*o2;
o3.get_xy(x,y);
cout<<x<<" "<<y;
cout<<endl;
o3=o1/o2;
o3.get_xy(x,y);
cout<<x<<" "<<y;
cout<<endl;
o3=o1-o2;
o3.get_xy(x,y);
cout<<x<<" "<<y;
cout<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment