3.28 c++

算数运算符

#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;
}

关系运算符

#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;
}

逻辑运算符

#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;
}

相关推荐

  1. AT_abc348_c [ABC348C] Colorful Beans 题解

    2024-03-29 05:14:07       26 阅读
  2. LeetCode //C - 338. Counting Bits

    2024-03-29 05:14:07       56 阅读
  3. CS32 C++ programming

    2024-03-29 05:14:07       32 阅读
  4. Atcoder ABC338 C - Leftover Recipes

    2024-03-29 05:14:07       60 阅读
  5. 38. C++ 引用的本质

    2024-03-29 05:14:07       47 阅读
  6. STM<span style='color:red;'>32</span> I2<span style='color:red;'>C</span>

    STM32 I2C

    2024-03-29 05:14:07      49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-29 05:14:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 05:14:07       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 05:14:07       82 阅读
  4. Python语言-面向对象

    2024-03-29 05:14:07       91 阅读

热门阅读

  1. NatCat使用说明

    2024-03-29 05:14:07       39 阅读
  2. Yarn的安装和使用

    2024-03-29 05:14:07       42 阅读
  3. VUE3——setup介绍

    2024-03-29 05:14:07       38 阅读
  4. 【Kotlin】List、Set、Map简介

    2024-03-29 05:14:07       38 阅读
  5. js的apply、call、bind

    2024-03-29 05:14:07       39 阅读
  6. python解压RAR文件

    2024-03-29 05:14:07       36 阅读
  7. 大前端-postcss安装使用指南

    2024-03-29 05:14:07       44 阅读
  8. ubuntu18.04找不到网络适配器,无法连接网络

    2024-03-29 05:14:07       41 阅读
  9. CUDA从入门到放弃(七):流( Streams)

    2024-03-29 05:14:07       45 阅读