1、上课写的复数类的实现,再写一遍,写出三种构造函数,算术运算符、关系运算符、逻辑运算符重载尝试实现自增、自减运算符的重载
#include <iostream>
using namespace std;
class Num
{
int rel; //实部
int vir; //虚部
public:
Num():rel(2),vir(1)
{
cout << "Num的无参构造" << endl;
}
Num(int rel):rel(6)
{
cout << "Num有一个参数的构造" << endl;
}
Num(int rel,int vir):rel(rel),vir(vir)
{
cout << "Num有两个参数的构造" << endl;
}
void show()
{
cout << rel << "+" << vir << "i" << endl;
}
Num &operator=(const Num &other)
{
cout << "Num的拷贝赋值函数" << endl;
this->rel = other.rel;
this->vir = other.vir;
return *this;
}
//算数运算符
friend Num operator+(const Num n1,const Num n2);
friend Num operator-(const Num n1,const Num n2);
friend Num operator*(const Num n1,const Num n2);
friend Num operator/(const Num n1,const Num n2);
friend Num operator%(const Num n1,const Num n2);
//关系运算符
friend bool operator>(const Num n1,const Num n2);
friend bool operator>=(const Num n1,const Num n2);
friend bool operator<(const Num n1,const Num n2);
bool operator<=(const Num n2);
bool operator==(const Num n2);
bool operator!=(const Num n2);
//逻辑运算符
friend bool operator&&(const Num n1,const Num n2);
friend bool operator||(const Num n1,const Num n2);
friend bool operator!(const Num n2);
};
Num operator+(const Num n1,const Num n2)
{
Num temp;
temp.rel = n1.rel+n2.rel;
temp.vir = n1.vir+n2.vir;
return temp;
}
Num operator-(const Num n1,const Num n2)
{
Num temp;
temp.rel = n1.rel-n2.rel;
temp.vir = n1.vir-n2.vir;
return temp;
}
Num operator*(const Num n1,const Num n2)
{
Num temp;
temp.rel = n1.rel*n2.rel;
temp.vir = n1.vir*n2.vir;
return temp;
}
Num operator/(const Num n1,const Num n2)
{
Num temp;
temp.rel = n1.rel/n2.rel;
temp.vir = n1.vir/n2.vir;
return temp;
}
Num operator%(const Num n1,const Num n2)
{
Num temp;
temp.rel = n1.rel%n2.rel;
temp.vir = n1.vir%n2.vir;
return temp;
}
bool operator>(const Num n1,const Num n2)
{
if(n1.rel > n2.rel) //如果n1的实部>n2的实部
{
return n1.rel>n2.rel;
}
else if(n1.rel == n2.rel) //如果n1和n2的实部相同,返回虚部比较的结果
{
return n1.vir > n2.vir;
}
return n1.rel>n2.rel;
}
bool operator>=(const Num n1,const Num n2)
{
if(n1.rel > n2.rel)
{
return n1.rel>n2.rel;
}
else if(n1.rel == n2.rel)
{
return n1.vir>=n2.vir;
}
return n1.rel>n2.rel;
}
bool operator<(const Num n1,const Num n2)
{
if(n1.rel < n2.rel)
{
return n1.rel<n2.rel;
}
else if(n1.rel == n2.rel)
{
return n1.vir<n2.vir;
}
return n1.rel<n2.rel;
}
bool Num::operator<=(const Num n2)
{
if(this->rel < n2.rel)
{
return this->rel<n2.rel;
}
else if(this->rel == n2.rel)
{
return this->vir<=n2.vir;
}
return this->rel<n2.rel;
}
bool Num::operator==(const Num n2)
{
if(this->rel == n2.rel)
{
return this->vir==n2.vir;
}
return this->rel==n2.rel;
}
bool Num::operator!=(const Num n2)
{
if(this->rel != n2.rel)
{
return this->rel!=n2.rel;
}
else if(this->rel == n2.rel)
{
return this->vir!=n2.vir;
}
return this->rel!=n2.rel;
}
bool operator&&(const Num n1,const Num n2)
{
return (n1.rel&&n2.rel)&&(n1.vir&&n2.vir);
}
bool operator||(const Num n1,const Num n2)
{
return (n1.rel||n2.rel)||(n1.vir||n2.vir);
}
bool operator!(const Num n2)
{
if(!n2.rel)
{
return 1;
}
return 0;
}
int main()
{
return 0;
}