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;
}
相关推荐
未来可期LJ33 分钟前
【C++ 设计模式】单例模式的两种懒汉式和饿汉式
c++·单例模式·设计模式
Trouvaille ~1 小时前
【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
c++·c++20·编译原理·编译器·类和对象·rvo·nrvo
little redcap1 小时前
第十九次CCF计算机软件能力认证-乔乔和牛牛逛超市
数据结构·c++·算法
机器视觉知识推荐、就业指导2 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt
孤寂大仙v2 小时前
【C++】STL----list常见用法
开发语言·c++·list
咩咩大主教3 小时前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用
Ylucius5 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
是店小二呀5 小时前
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
开发语言·c++·后端
ephemerals__5 小时前
【c++】动态内存管理
开发语言·c++
CVer儿5 小时前
条件编译代码记录
开发语言·c++