3.28C++

复数类的实现,写出三种构造函数,算术运算符、关系运算符、逻辑运算符重载尝试实现自增、自减运算符的重载

cpp 复制代码
#include <iostream>
using namespace std;
class Num
{
    int rel;   //实部
    int vir;   //虚部
public:
    Num():rel(2),vir(1){}
    Num(int rel,int vir):rel(rel),vir(vir){}
    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);
    Num operator-(Num &other);
    friend Num operator*(const Num n1,const Num n2);
    Num operator/(const Num n1);
    friend Num operator%(const Num n1,const Num n2);
    friend bool operator>(const Num n1,const Num n2);
    bool operator<(const Num n1);
    friend bool operator>=(const Num n1,const Num n2);
    bool operator==(const Num n1);
    friend bool operator<=(const Num n1,const Num n2);

    friend bool operator&&(const Num n1,const Num n2);
    bool operator||(const Num n1);

    bool operator!();

    friend Num operator++(Num &n1);
    Num operator++(int);
    friend Num operator--(Num &n1);
    Num operator--(int);

    void show();
};
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 Num::operator-(Num &other)
{
    Num temp;
    temp.rel = this->rel-other.rel;
    temp.vir = this->vir-other.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 Num::operator/(const Num n1)
{
    Num temp;
    temp.rel = this->rel/n1.rel;
    temp.vir = this->vir/n1.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)
    {
        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 n1)
{
    if(this->rel<n1.rel)
    {
        return this->rel<n1.rel;
    }
    else if(this->rel==n1.rel)
    {
        return this->vir<n1.vir;
    }
    return this->rel<n1.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.vir>n2.vir;
}
bool Num::operator==(const Num n1)
{
    return (this->rel==n1.rel)&&(this->vir==n1.vir);
}
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.vir<n2.vir;
}

bool operator&&(const Num n1,const Num n2)
{
    return (n1.rel&&n2.rel)||(n1.vir&&n2.vir);
}
bool Num::operator||(const Num n1)
{
    return (this->rel||n1.rel)&&(this->vir||n1.vir);
}

bool Num::operator!()
{
    return !(this->rel||this->vir);
}


Num operator++(Num &n1)
{
    ++(n1.rel);
    ++(n1.vir);
    return n1;
}
Num Num::operator++(int)
{
    Num temp;
    temp.rel=this->rel++;
    temp.vir=this->vir++;
    return temp;
}
Num operator--(Num &n1)
{
    --(n1.rel);
    --(n1.vir);
    return n1;
}
Num Num::operator--(int)
{
    Num temp;
    temp.rel=this->rel--;
    temp.vir=this->vir--;
    return temp;
}
void Num::show()
{
    cout << rel << " + " << vir << "i" << endl;
}
int main()
{
    Num n1;
    Num n2(1,4);
    Num n3;
    n3 = n1+n2;
    n3.show();
    Num n4;
    n4=n1-n2;
    n4.show();
    n4++;
    n4.show();
    --n4;
    n4.show();
    cout<<(n4>=n3)<<endl;
    Num n5;
    n5--;
    n5.show();
    ++n5;
    n5.show();
    cout<<(n2&&n3)<<endl;
    cout<<(n2||n5)<<endl;
    return 0;
}
相关推荐
凌云行者18 分钟前
OpenGL入门005——使用Shader类管理着色器
c++·cmake·opengl
凌云行者21 分钟前
OpenGL入门006——着色器在纹理混合中的应用
c++·cmake·opengl
~yY…s<#>1 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
可均可可1 小时前
C++之OpenCV入门到提高004:Mat 对象的使用
c++·opencv·mat·imread·imwrite
白子寰2 小时前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++
小芒果_012 小时前
P11229 [CSP-J 2024] 小木棍
c++·算法·信息学奥赛
gkdpjj2 小时前
C++优选算法十 哈希表
c++·算法·散列表
王俊山IT2 小时前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习
-Even-2 小时前
【第六章】分支语句和逻辑运算符
c++·c++ primer plus
我是谁??3 小时前
C/C++使用AddressSanitizer检测内存错误
c语言·c++