#include <iostream>
using namespace std;
class Math
{
friend const Math operator-(const Math &L, const Math &R);
friend bool operator>(const Math &L, const Math &R);
friend Math &operator-=(Math &L, const Math &R);
private:
int a;
int b;
public:
Math(){}
Math(int a, int b):a(a),b(b)
{}
//算术运算符
const Math operator+(const Math &R) const
{
Math temp;
temp.a = a + R.a;
temp.b = b + R.b;
return temp;
}
const Math operator*(const Math &R) const
{
Math temp;
temp.a = a * R.a;
temp.b = b * R.b;
return temp;
}
const Math operator/(const Math &R) const
{
Math temp;
temp.a = a / R.a;
temp.b = b / R.b;
return temp;
}
const Math operator%(const Math &R) const
{
Math temp;
temp.a = a % R.a;
temp.b = b % R.b;
return temp;
}
//关系运算符
bool operator<(const Math &R) const
{
if(a < R.a && b < R.b)
{
return true;
}else {
return false;
}
}
bool operator==(const Math &R) const
{
if(a == R.a && b == R.b)
{
return true;
}else {
return false;
}
}
bool operator<=(const Math &R) const
{
if(a <= R.a && b <= R.b)
{
return true;
}else {
return false;
}
}
bool operator>=(const Math &R) const
{
if(a >= R.a && b >= R.b)
{
return true;
}else {
return false;
}
}
//赋值运算符
Math &operator+=(const Math &R)
{
a += R.a;
b += R.b;
return *this;
}
Math &operator*=(const Math &R)
{
a *= R.a;
b *= R.b;
return *this;
}
Math &operator/=(const Math &R)
{
a /= R.a;
b /= R.b;
return *this;
}
Math &operator%=(const Math &R)
{
a %= R.a;
b %= R.b;
return *this;
}
void show()
{
cout << "a = " << a << " b = " << b << endl;
}
};
const Math operator-(const Math &L, const Math &R)
{
Math temp;
temp.a = L.a - R.a;
temp.b = L.b - R.a;
return temp;
}
bool operator>(const Math &L, const Math &R)
{
if(L.a > R.a && L.b > R.b)
{
return true;
}else {
return false;
}
}
Math &operator-=(Math &L, const Math &R)
{
L.a -= R.a;
L.b -= R.b;
return L;
}
int main()
{
//算术运算符
Math m1(99,20);
Math m2(10,12);
Math m3 = m1 + m2;
Math m4 = m1 - m2;
Math m5 = m1 * m2;
Math m6 = m1 / m2;
Math m7 = m1 % m2;
m3.show();
m4.show();
m5.show();
m6.show();
m7.show();
cout << "---------------------------------------" << endl;
//关系运算符
if(m1 < m2){
cout << "m1 < m2" << endl;
}else if(m1 > m2){
cout << "m1 > m2" << endl;
}else if(m1 == m2){
cout << "m1 == m2" << endl;
}
//赋值运算符
cout << "---------------------------------------" << endl;
m1 += m2;
m1.show();
return 0;
}
c++day4
最新推荐文章于 2024-10-10 10:58:41 发布