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;
}
相关推荐
是老余几秒前
Java三大特性:封装、继承、多态【详解】
java·开发语言
小馒头学python2 分钟前
【Python爬虫五十个小案例】爬取豆瓣电影Top250
开发语言·爬虫·python
尘浮生1 小时前
Java项目实战II基于微信小程序的南宁周边乡村游平台(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·微信小程序·小程序·maven
wangjing_05223 小时前
C语言练习.if.else语句.strstr
c语言·开发语言
Tony_long74833 小时前
Python学习——字符串操作方法
开发语言·c#
SoraLuna3 小时前
「Mac玩转仓颉内测版26」基础篇6 - 字符类型详解
开发语言·算法·macos·cangjie
出逃日志4 小时前
JS的DOM操作和事件监听综合练习 (具备三种功能的轮播图案例)
开发语言·前端·javascript
前端青山4 小时前
React事件处理机制详解
开发语言·前端·javascript·react.js
雨中rain4 小时前
贪心算法(2)
算法·贪心算法
black0moonlight5 小时前
ISAAC Gym 7. 使用箭头进行数据可视化
开发语言·python