An article to take you a preliminary understanding of C + + overload mechanism!

Time:2021-4-20

C + + function and operator overloading

Operator overloading is a very important part of C + +. We already know how to overload functions

The so-called overload is to redefine a defined function, symbol, etc.

C + + adds the ability to overload operators on the basis of C, while Java does not allow to modify the meaning of operators, but it should allow to modify functions.

function overloading

First, let’s look at how to overload functions

#include <iostream>

using namespace std;
void swap(int &a, int &b)
{
    a = a + b;
    b = a - b;
    cout  << " swap in func " <<  endl ; // test
}
int main()
{
    int a = 1, b = 2;
    swap(a, b);
    cout << a << " " << b << endl;
    // Output 2 1
    return 0;
}

If you don’t think it’s a custom function, add itcout << "swap in func" << endl;The output is like this:

swap in func
3 1

Explain the exchange in the user-defined function. This is the redefinition of functions

overload operators

If there are two date classes that store month and day separately, how can we subtract these two classes and put them into another class to get a date difference?

First of all, the definition of this class is as follows

class Date {
public:
    int month;
    int day;
};
Date d1, d2;

Then it can’t be directd1 - d2Well, there are so many exceptions, so we need to redefine the minus sign. In fact, the structure can also be overloaded.

class Date {
public:
    int month;
    int day;
    Date operator-(Date &b)
    {
        Date R;
        R.month = month - b.month;
        R.day = day - b.day;
        return R;
    }
};

Of course, this operator is quite simple. If you do this, the date difference will lead to a negative number, and the month has not been determined, so you have to improve it. It’s better to call it a subtraction procedure

Let’s take a small example to add up the scores of two students. In fact, we use a structure, one by one. However, I use it for practicing classes, sharing and private

#include <iostream>
#include <stdlib.h>
using namespace std;
class stuScore {
private:
    int chinese;
    int math;
    int english;
public:

    void setScore()
    {
        cin >> chinese >> math >> english;
    }
    stuScore operator+(stuScore ss)
    {
        stuScore rb;
        rb.chinese = chinese + ss.chinese;
        rb.math = math + ss.math;
        rb.english = english + ss.english;
        return rb;
    }
    void outScore()
    {
        cout << chinese << " " << math << " " << english << endl;
    }
};
stuScore s1, s2, ans;
int main()
{
    s1.setScore();
    s2.setScore();
    ans = s1 + s2;
    ans.outScore();
    return EXIT_SUCCESS;
}

https://www.runoob.com/cplusp…

Recommended Today

Envoy announced alpha version of native support for windows

Author: sunjay Bhatia Since 2016, porting envoy to the windows platform has been an important part of the projectOne of the goalsToday, we are excited to announce the alpha version of envoy’s windows native support. The contributor community has been working hard to bring the rich features of envoy to windows, which is another step […]