Learning C ++ (V) _ operator overloading

Operator overloading

First we look at the definition of overloaded operators:

Operator is overloaded with special function name, the function name is followed by the key operator and the operator to override symbols. As with other functions, overloaded operators have a return type and an argument list.

Supported in C ++ overload operators are:

It does not support overloaded operators:


Operator overloading rules

Operator overloading is a member function of a general syntax of the form:
Function type operator operator (parameter list)
{
function body;
}
operator overloading general syntax class friend function as:
Friend function type operator operator (shape reference table)
{
function body;
}
where the function type is a type of calculation results; operator keyword is defined operator overloading function; operator is overloaded operator name.

When a member of the class of operator overloading function, the number of function parameters is less than a number of original operation;

When the overload class friend function, the number of parameters and the same number of the original operands.

The reason is that when overloaded member function of the class, if an object using the member functions overloaded, its own data can be accessed directly, do not need to be passed in the parameter table, the less is the object of the operands itself.

The reload function is friend, friend function data for operating an object, it must be carried out by the name of the object, and therefore to use the parameters to be passed, there will be the number of operands Variety.

The main advantage of operator overloading is allowed to change within the system used in the operation of the operator, to adapt to a user-defined type similar operation.

Here we were to +, -, +, -, << operator operator overloading results were observed program.


Example 1: Overloaded +, -

1.使用成员函数实现运算符+,-的重载
#include<iostream>

using namespace std;

class Point {
public:
    Point(int x = 0, int y = 0):x(x),y(y) {};
    Point operator+(Point p) {
        return Point(this->x + p.x, this->y + p.y);
    }
    Point operator-(Point p) {
        return Point(this->x - p.x, this->y - p.y);
    }
    void display() {
        cout << "(" << x << "," << y << ")";
    }

private:
    int x,y;
};

int main()
{
    Point p1(1, 1);
    Point p2(2, 2);
    //1.将友元函数注释,调用Point的成员函数实现 p1 + p2
    Point p3 = p1 + p2;

    cout << "p1:";//输出p1的值
    p1.display();
    cout << " + ";
    cout << "p2:";//输出p2的值
    p2.display();
    cout << " = ";
    cout << "p3:";//输出p3的值
    p3.display();
    cout << endl;

    Point p4 = p1 - p2;

    cout << "p1:";//输出p1的值
    p1.display();
    cout << " - ";
    cout << "p2:";//输出p2的值
    p2.display();
    cout << " = ";
    cout << "p4:";//输出p4的值
    p4.display();
    cout << endl;

    return 0;
}

Program output:

According to the results we can see that the program is running successfully achieved p1 + p2 and p1-p2.


2.使用友元函数实现运算符+,-的重载
#include<iostream>

using namespace std;

class Point {
    friend Point operator+(Point p1, Point p2); //友元函数
    friend Point operator-(Point p1, Point p2);
public:
    Point(int x = 0, int y = 0):x(x),y(y) {};
    void display() {
        cout << "(" << x << "," << y << ")";
    }

private:
    int x,y;
};

//重载运算符实现
Point operator+(Point p1, Point p2) {
    return Point(p1.x + p2.x, p1.y + p2.y);
}

Point operator-(Point p1, Point p2) {
    return Point(p1.x - p2.x, p1.y - p2.y);
}

int main()
{
    Point p1(1, 1);
    Point p2(2, 2);
    //2.将成语函数注释,调用Point的友元函数实现 p1 + p2
    Point p3 = p1 + p2;
    
    cout << "p1:";//输出p1的值
    p1.display();
    cout << " + ";
    cout << "p2:";//输出p2的值
    p2.display();
    cout << " = ";
    cout << "p3:";//输出p3的值
    p3.display();
    cout << endl;

    Point p4 = p1 - p2;

    cout << "p1:";//输出p1的值
    p1.display();
    cout << " - ";
    cout << "p2:";//输出p2的值
    p2.display();
    cout << " = ";
    cout << "p4:";//输出p4的值
    p4.display();
    cout << endl;

    system("pause");

    return 0;
}

operation result:


Example 2: Overload +, -

1.使用成员函数重载++
#include<iostream>

using namespace std;

class Point {
    
public:
    Point(int x = 0, int y = 0):x(x),y(y) {};
    Point & operator++() {    //对运算符++(前置)进行重载
        this->x++;
        this->y++;
        return *this;
    }
    Point operator++(int) {  //对运算符++(后置)进行重载
        Point p = *this;
        this->x++;
        this->y++;
        return p;
    }
    void display() {   //输出点
        cout << "(" << x << "," << y << ")" << endl << endl;
    }

private:
    int x,y;
};

int main()
{
    Point p(1, 1);
    cout << "p :";
    p.display();
        
        //p++
    cout << "------ p++ ------" << endl; 

    Point p1 = p++;
    cout << "p1:";
    p1.display();

    cout << "p :";
    p.display();

    cout << "------ p++ ------" << endl << endl;

    cout << "p :";
    p.display();
        
        //++p
    cout << "------ ++p ------" << endl;

    Point p2 = ++p;
    cout << "p2:";
    p2.display();

    cout << "p :";
    p.display();

    cout << "------ ++p ------" << endl << endl;
    
    system("pause");

    return 0;
}

operation result:


2.使用成员函数重载--
#include<iostream>

using namespace std;

class Point {
    
public:
    Point(int x = 0, int y = 0):x(x),y(y) {};
    Point& operator--() {     //对运算符--(前置)进行重载
        this->x--;
        this->y--;
        return *this;
    }
    Point operator--(int) {    //对运算符--(后置)进行重载
        Point p = *this;
        this->x--;
        this->y--;
        return p;
    }
    void display() {
        cout << "(" << x << "," << y << ")" << endl << endl;
    }

private:
    int x,y;
};

int main()
{
    Point p(1, 1);
    cout << "p :";
    p.display();
        
        //p--
    cout << "------ (p--) ------" << endl;

    Point p1 = p--;
    cout << "p1:";
    p1.display();

    cout << "p :";
    p.display();
        
    cout << "------ (p--) ------" << endl << endl;

    cout << "p :";
    p.display();

    //--p
        cout << "------ (--p) ------" << endl;

    Point p2 = --p;
    cout << "p2:";
    p2.display();

    cout << "p :";
    p.display();

    cout << "------ (--p) ------" << endl << endl;
    
    system("pause");

    return 0;
}

operation result:


3.使用友元函数重载++
#include<iostream>

using namespace std;

class Point {
    friend Point& operator++(Point& p);     //友元函数
    friend Point operator++(Point& p, int);
public:
    Point(int x = 0, int y = 0) :x(x), y(y) {};
    void display() {
        cout << "(" << x << "," << y << ")" << endl << endl;
    }

private:
    int x, y;
};

Point& operator++(Point &p) {
    p.x++;
    p.y++;
    return p;
}

Point operator++(Point &p, int) {
    Point temp = p;
    p.x++;
    p.y++;
    return temp;
}

int main()
{
    Point p(1, 1);
    cout << "p :";
    p.display();
        
        //p++   
    cout << "------ p++ ------" << endl;

    Point p1 = p++;
    cout << "p1:";
    p1.display();

    cout << "p :";
    p.display();

    cout << "------ p++ ------" << endl << endl;

    cout << "p :";
    p.display();
        
        //++p
    cout << "------ ++p ------" << endl;

    Point p2 = ++p;
    cout << "p2:";
    p2.display();

    cout << "p :";
    p.display();

    cout << "------ ++p ------" << endl << endl;

    system("pause");

    return 0;
}

operation result:


4.使用友元函数重载--
#include<iostream>

using namespace std;

class Point {
    friend Point& operator--(Point& p);   //友元函数
    friend Point operator--(Point& p, int);
public:
    Point(int x = 0, int y = 0) :x(x), y(y) {};
    void display() {
        cout << "(" << x << "," << y << ")" << endl << endl;
    }

private:
    int x, y;
};

Point& operator--(Point& p) {
    p.x--;
    p.y--;
    return p;
}

Point operator--(Point& p, int) {
    Point temp = p;
    p.x--;
    p.y--;
    return temp;
}

int main()
{
    Point p(1, 1);
    cout << "p :";
    p.display();
        
        //p--
    cout << "------ (p--) ------" << endl;

    Point p1 = p--;
    cout << "p1:";
    p1.display();

    cout << "p :";
    p.display();

    cout << "------ (p--) ------" << endl << endl;

    cout << "p :";
    p.display();
        
        //--p
    cout << "------ (--p) ------" << endl;

    Point p2 = --p;
    cout << "p2:";
    p2.display();

    cout << "p :";
    p.display();

    cout << "------ (--p) ------" << endl << endl;

    system("pause");

    return 0;
}

operation result:


Example 3: overloaded << operator implementation of the output of Point objects

#include<iostream>

using namespace std;

class Point {
    friend ostream& operator<<(ostream & out, Point& p);    //友元函数
public:
    Point(int x = 0, int y = 0) :x(x), y(y) {};
private:
    int x, y;
};

ostream& operator<<(ostream & out,Point &p) {
    out << "(" << p.x << "," << p.y << ")";
    return out;
}

int main()
{
    Point p(1, 1);
    cout << "p:" << p;    //使用重载后的<<输出p

    system("pause");

    return 0;
}

operation result:

According to the results we can see that the program run by the output value of p <<.

At this point we think: Can I use << member function overloading it?

*使用成员函数重载<<运算符
#include<iostream>

using namespace std;

class Point {
public:
    Point(int x = 0, int y = 0) :x(x), y(y) {};
    void operator<<(ostream& out) {        //使用成员函数进行<<运算符重载
        out << "(" << this->x << "," << this->y << ")";
    }
private:
    int x, y;
};

int main()
{
    Point p(1, 1);
    p << cout;

    system("pause");

    return 0;
}

Program can run normally.

But there will be a problem that can only be written when the program output p

p << cout

form.

Because the first argument is this pointer, the second argument is that we pass in the out, but it is entirely inconsistent with the cout std habits, our variable should be printed in the cout of the right, such as cout < <d << endl.

Such overloading and normal function is no different, where it lost its purpose overloaded function.

So this way, we can not put the output of operator overloading member functions written, write member functions to achieve the function, can realize the function but meaningless reload itself.

So we should write overloaded functions outside the class, and then use the corresponding class friend function.

The above is my summary of C ++ operator overloading knowledge points.

If wrong, please correct me.

Guess you like

Origin www.cnblogs.com/meituanqishoualin/p/11747306.html