Search This Blog

Monday, 12 November 2012

Overloading Binary Operators

#include<iostream.h>
#include<conio.h>
class MARK{
private:
int mark;
public:
MARK(){}
MARK(int x){mark=x;}
void getMark()
{
cout<<"Get Mark";
cin>>mark;
}
void putMak()
{
cout<<"Mark="<<Mark<<"\n";
}
MARK operator+(MARK t)
{
MARK temp;
temp.mark=mark+t.mark;
return temp;
}
int operator-(MARK t)
{
int res;
res=mark-t.mark;
return res;
}
MARK operator*(MARK t)
{
MARK temp;
temp.mark=mark*t.mark;
return temp;
}
MARK operator/(MARK t)
{
MARK temp;
temp.mark=this->mark/t.mark;
return temp;
}
int operator<(MARK t)
{
if(this->mark<t.mark)
return 1;
else
return 0;
}
int operator>(MARK t)
{
if(this->mark>t.mark)
return 1;
else
return 0;
}
int operator==(MARK t)
{
if(this->mark==t.mark)
return 1;
else
return 0;
}
};
void main()
{
MARK p(5),q(3),r;
r=p+q;
r.putMark();
int res=p-q;
cout<<"Rsult="<<res;
r=p*q;
r.putMark();
r=p/q;
r.putMark();
if(p<q)
cout<<"Obj p is less than q";
else
cout<<"not less than q";
getch();
}

Output:
Mark=8
Rsult=2Mark=15
Mark=1
not less than qMark=8
Rsult=2Mark=15
Mark=1
not less than q



No comments:

Post a Comment