简体   繁体   English

使用运算符重载对两个数字进行加法和减法

[英]addition and subtraction of two numbers using operator overloading

#include<iostream>

using namespace std;

class add
{
private: int a,b;
public: add(int x=0)
        {
            a=x;
        }
        add operator+(add const &c)         // sub operator-(sub const &c)
        {                                   //{
            add sum;                        //      sub diff;
            sum.a=a+c.a;                    //      diff.a=a-c.a;
            return sum;                     //      return diff
        }                                   //}
        void print()
        {
            cout<<"sum: "<<a;
        }
};

int  main()
{
add a1(10),a2(5);                           //sub s1(10),s2(5);
add a3=a1+a2;                               // sub s3=s1-s2;
a3.print();                                 // s3.print();
return 0;
}

Here I've written seperately but what to do if I need to do both in a single code?在这里,我已经单独编写了,但是如果我需要在一个代码中同时执行这两个操作,该怎么办? I want a C++ code to perform them simultaneously我想要一个 C++ 代码同时执行它们

You can define any reasonable combination of:您可以定义以下任何合理组合:

Foo operator+(arg);
Foo operator-(arg);
Foo operator*(arg);
Foo operator/(arg);

And the arg can be another Foo or some other type entirely.并且 arg 可以完全是另一个 Foo 或其他类型。 For instance:例如:

#include <iostream>

using namespace std;

class Operators {
public:
    Operators() = default;
    Operators(int v) : value(v) {}

    Operators operator+(const Operators &other) {
        return Operators{value + other.value};
    }

    Operators operator+(const int byValue) {
        return Operators{value + byValue};
    }

    Operators operator-(const Operators &other) {
        return Operators{value - other.value};
    }

    Operators operator-(const int byValue) {
        return Operators{value - byValue};
    }

    Operators operator*(const Operators &other) {
        return Operators{value * other.value};
    }

    Operators operator/(const Operators &other) {
        return Operators{value / other.value};
    }

    int value = 0;
};

int main(int, char **) {
    Operators first{10};
    Operators second{20};

    Operators result1 = first + second;
    Operators result2 = first * second;
    Operators result3 = first * 3;
    Operators result4 = second / 2;

    cout << "first + second == " << result1.value << endl;
    cout << "first * second == " << result2.value << endl;
    cout << "first * 3      == " << result3.value << endl;
    cout << "first / 2      == " << result4.value << endl;
}

first + second == 30 first * second == 200 first * 3 == 30 first / 2 == 10第一个 + 第二个 == 30 个第一个 * 第二个 == 200 个第一个 * 3 == 30 个第一个 / 2 == 10

You'll see I overwrote operators that take two Operators objects, but I also wrote a few that take an integer argument, too.你会看到我重写了带有两个 Operators 对象的运算符,但我也写了一些带有 integer 参数的运算符。

I compiled and ran that with:我编译并运行它:

g++ --std=c++17 Whatever.cpp -o Whatever && Whatever

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM