64,654
社区成员
发帖
与我相关
我的任务
分享
//#include"iostream.h"
#include <iostream>
using namespace std;
class OpOverClass
{
friend ostream& operator <<(ostream& osObject, const OpOverClass& right)
{
osObject << right.a <<" , " << right.b;
return osObject;
}
friend istream& operator>>(istream& isObject, OpOverClass& right)
{
isObject>> right.a >> right.b;
return isObject;
}
friend bool operator>(const OpOverClass& left,const OpOverClass& right)
{
return (left.a>right.a && left.b>right.b);
}
public:
~OpOverClass() {};
OpOverClass() {};
OpOverClass(int , int);
void printf() const;
OpOverClass operator+(const OpOverClass&) const;
bool operator==(const OpOverClass&) const;
bool operator!=(const OpOverClass&) const;
private:
int a;
int b;
};
//**************************************************************************
// friend Function
//**************************************************************************
//**************************************************************************
OpOverClass::OpOverClass(int tempa,int tempb)
{
a = tempa;
b = tempb;
}
void OpOverClass::printf() const
{
cout <<"a = " <<a <<" , b = " <<b <<endl;
}
OpOverClass OpOverClass::operator+(const OpOverClass& otherObject) const
{
OpOverClass temp;
temp.a = a + otherObject.a;
temp.b = b + otherObject.b;
return temp;
}
bool OpOverClass::operator==(const OpOverClass& right) const
{
return (a==right.a && b==right.b);
}
bool OpOverClass::operator!=(const OpOverClass& right) const
{
return (a!=right.a || b!=right.b);
}
//**************************************************************************
// Function main()
//**************************************************************************
void main()
{
cout <<"Welcome to operator!\n\n" <<flush;
OpOverClass x(1,2);
OpOverClass y(3,4);
OpOverClass z(0,0);
z = x+y;
x.printf();
y.printf();
z.printf();
if(x==y)
{
cout <<"YES!" <<endl;
}
else
{
cout <<"NO!" <<endl;
}
cout<<x<<"\t"<<y<<"\t"<<z;
}