C++——day4

思维导图:

2>成员函数版本实现算术运算符的重载

全局函数版本实现算术运算符的重载
 

#include <iostream>

using namespace std;
class Sor
{
private:
    int a;
    int b;
public:
    Sor()
    {}
    Sor(int a,int b):a(a),b(b)
    {}
    const Sor operator+(const Sor &other)const
    {
        Sor temp;
        temp.a=this->a+other.a;
        temp.b=this->b+other.b;
        return temp;
    }
    const Sor operator-(const Sor &other)const
    {
        Sor temp;
        temp.a=this->a-other.a;
        temp.b=this->b-other.b;
        return temp;
    }
    const Sor operator%(const Sor &other)const
    {
        Sor temp;
        temp.a=this->a%other.a;
        temp.b=this->b%other.b;
        return temp;
    }
    friend const Sor operator*(const Sor &L,const Sor &R);
    friend const Sor operator/(const Sor &L,const Sor &R);
    void show()
    {
        cout << "a = " << a << "  b = " << b << endl;
    }
};
const Sor operator*(const Sor &L,const Sor &R)
{
    Sor temp;
    temp.a=L.a*R.a;
    temp.b=L.b*R.b;
    return temp;
}
const Sor operator/(const Sor &L,const Sor &R)
{
    Sor temp;
    temp.a=L.a/R.a;
    temp.b=L.b/R.b;
    return temp;
}
int main()
{
    Sor s1(2,3);
    Sor s2(4,5);
    Sor s3;
    s3=s1+s2;
    s3.show();
    s3=s2-s1;
    s3.show();
    s3=s1*s2;
    s3.show();
    s3=s2/s1;
    s3.show();
    s3=s1%s2;
    s3.show();
    return 0;
}

相关推荐

  1. MSc CDA Take-Home

    2024-03-15 08:54:06       60 阅读
  2. CDA一级备考策略分享

    2024-03-15 08:54:06       32 阅读
  3. CDA-LevelⅡ【考题整理-带答案】

    2024-03-15 08:54:06       50 阅读
  4. Spring Data访问Elasticsearch----CDI集成

    2024-03-15 08:54:06       36 阅读
  5. CDA Level Ⅰ 2023认证考试大纲

    2024-03-15 08:54:06       78 阅读
  6. web server apache tomcat11-33-CDI

    2024-03-15 08:54:06       33 阅读
  7. Spring Data访问 MongoDB(十六)----CDI集成

    2024-03-15 08:54:06       41 阅读

最近更新

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

    2024-03-15 08:54:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 08:54:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 08:54:06       82 阅读
  4. Python语言-面向对象

    2024-03-15 08:54:06       91 阅读

热门阅读

  1. NAT笔记

    2024-03-15 08:54:06       40 阅读
  2. springboot 自动装载原理

    2024-03-15 08:54:06       42 阅读
  3. C-线程池

    2024-03-15 08:54:06       39 阅读
  4. react03

    react03

    2024-03-15 08:54:06      41 阅读
  5. 力扣大厂热门面试算法题 27-29

    2024-03-15 08:54:06       41 阅读
  6. 【MySQL 系列】MySQL 引擎篇

    2024-03-15 08:54:06       48 阅读
  7. c# 新增一条数据

    2024-03-15 08:54:06       42 阅读
  8. Retelling|Facebook1

    2024-03-15 08:54:06       40 阅读