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;
}
相关推荐
MessiGo14 分钟前
Javascript 编程基础(5)面向对象 | 5.1、构造函数实例化对象
开发语言·javascript·原型模式
大霞上仙17 分钟前
nonlocal 与global关键字
开发语言·python
曦月逸霜19 分钟前
第34次CCF-CSP认证真题解析(目标300分做法)
数据结构·c++·算法
galaxy_strive22 分钟前
绘制饼图详细过程
开发语言·c++·qt
黑客老李1 小时前
JavaSec | SpringAOP 链学习分析
java·运维·服务器·开发语言·学习·apache·memcached
开开心心就好1 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
特立独行的猫a1 小时前
Nuxt.js 中的路由配置详解
开发语言·前端·javascript·路由·nuxt·nuxtjs
海的诗篇_1 小时前
移除元素-JavaScript【算法学习day.04】
javascript·学习·算法
勤奋的知更鸟2 小时前
Java编程之原型模式
java·开发语言·原型模式