3.28 c++

算数运算符

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);
    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);
     
    Num operator+(const Num n1);
    Num operator-(const Num n1);
    Num operator*(const Num n1);
    Num operator/(const Num n1);
    Num operator%(const Num n1);
    void show();
};

Num Num::operator+(const Num n1 )
{       
    Num temp;            
    temp.rel=this->rel+n1.rel;
    temp.vir=this->vir+n1.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 Num::operator*(const Num n1)
{
    Num temp;
    temp.rel=this->rel*n1.rel;
    temp.vir=this->vir*n1.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 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;
}
//减法
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;
}
void Num::show()
{
    cout << rel << "+" << vir << "i" << endl;
}
int main()
{
    Num n1;
    Num n2(1,4);
    Num n3;
    n3 = n1+n2;
    n3.show();
    return 0;
}

关系运算符

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 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 n1);
    bool operator==(const Num n1);
    bool operator!=(const Num n1);
};
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 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 Num::operator==(const Num n1)
{
    if(this->rel==n1.rel)  
    {
        return this->vir==n1.vir;
    }
    return this->rel==n1.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;
}
int main()
{
    
    return 0;
}

逻辑运算符

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 bool operator&&(const Num n1,const Num n2);
    friend bool operator||(const Num n1,const Num n2);

};
bool operator&&(const Num n1,const Num n2)
{
    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.vir||n2.vir;
    }
    return n1.rel||n2.rel;
}
int main()
{
    cout << "Hello World!" << endl;
    return 0;
}
相关推荐
paterWang2 小时前
基于 Python 和 OpenCV 的酒店客房入侵检测系统设计与实现
开发语言·python·opencv
东方佑2 小时前
使用Python和OpenCV实现图像像素压缩与解压
开发语言·python·opencv
mit6.8242 小时前
[实现Rpc] 通信类抽象层 | function | using | 解耦合设计思想
c++·网络协议·rpc
我真不会起名字啊2 小时前
“深入浅出”系列之杂谈篇:(3)Qt5和Qt6该学哪个?
开发语言·qt
laimaxgg2 小时前
Qt常用控件之单选按钮QRadioButton
开发语言·c++·qt·ui·qt5
水瓶丫头站住3 小时前
Qt的QStackedWidget样式设置
开发语言·qt
尼尔森系4 小时前
排序与算法:希尔排序
c语言·算法·排序算法
小钊(求职中)4 小时前
Java开发实习面试笔试题(含答案)
java·开发语言·spring boot·spring·面试·tomcat·maven
AC使者4 小时前
A. C05.L08.贪心算法入门
算法·贪心算法
冠位观测者4 小时前
【Leetcode 每日一题】624. 数组列表中的最大距离
数据结构·算法·leetcode