#include <iostream>
using namespace std;
//定义一个复数类
class Complex
{
private:
int real;
int vir;
public:
Complex() {}
Complex(int r,int v):real(r),vir(v) {}
void show()
{
if(vir >= 0)
{
cout << real << " + " << vir << "i" << endl;
}
else
{
cout << real << vir << "i" << endl;
}
}
//成员函数实现减法运算法重载
const Complex operator- (const Complex &R) const
{
Complex c;
c.real = this->real - R.real;
c.vir = this->vir - R.vir;
return c;
}
//成员函数实现加法运算符重载
const Complex operator+ (const Complex &R) const
{
Complex c;
c.real = this->real + R.real;
c.vir = this->vir + R.vir;
return c;
}
//成员函数实现关系运算符重载
bool operator> (const Complex &R) const
{
return this->real > R.real && this->vir > R.vir;
}
//成员函数实现中括号运算符
int & operator[] (int index)
{
if(index == 0)
{
return real;
}
else if(index == 1)
{
return vir;
}
}
//成员函数实现+=运算符重载
Complex & operator+= (const Complex &R)
{
this->real += R.real;
this->vir += R.vir;
return *this;
}
//成员函数实现负号运算符重载
Complex operator-()
{
Complex c;
c.real = -this->real;
c.vir = -this->vir;
return c;
}
//成员函数实现前置自增运算符重载
Complex &operator++()
{
++this->real;
++this->vir;
return *this;
}
};
int main()
{
cout << "Hello World!" << endl;
return 0;
}
国庆第四天
最新推荐文章于 2024-08-19 18:41:12 发布