Operator overloading based on C + +

Posted by The Jackel on Sat, 06 Jun 2020 05:44:40 +0200

The concept of operator overloading

⚫ Expressions in C + + are composed of operators and operands according to rules. For example, arithmetic operators include plus "+", minus "-", multiply "*", divide "/" and modulus "%". Without special processing, these arithmetic operators can only be used to operate on constants or variables of basic data types, but not between objects.
⚫ Operator overloading is to give multiple meanings to existing operators, so that the same operator can produce different behaviors when it is used for different types of data. The purpose of operator overloading is to enable operators in C + + to operate on objects as well.
⚫ Operators used for class operations are usually overloaded. There are two operators, the system provides the default overloaded version: assignment operator = and address operator &.

 

 

2. Rules for overloaded operators

  • 1) The meaning of the operator after overloading should conform to the original usage. For example, to overload the "+" operator, the completed function should be similar to adding. It is not appropriate to subtract in the overloaded "+" operator.
  • 2) Operator overloading cannot change the original semantics of operators, including the priority and associativity of operators.
  • 3) Operator overloading cannot change the number and syntax of operator operands.
  • 4) Cannot create a new operator, that is, overloaded operators cannot exceed the range of overloaded operators allowed by C + + language.
  • 5) When overloading operators' () '[]' - > or assignment operators' = ', they can only be overloaded as member functions, not as global functions.
  • 6) Operator overloading cannot change the meaning of the operator used for basic data type objects.

3. Overload of assignment operator

  • The assignment operator "=" in C + + requires that the types of the left and right operands match, or at least the assignments are compatible. Sometimes it is hoped that the type of operands on both sides of "=" can be established even if they are not assignment compatible, which requires overloading "=". C + + specifies that "=" can only be overloaded as a member function. If there are two objects s1 and s2 of class CL, the assignment statement between them is usually in the following form: s1=s2; when the member function is defined in class CL and the assignment operator is overloaded, the above assignment statement will be interpreted as the form of function call: s1.operator=(s2);

4. Shallow copy and deep copy

  • Similar objects can be assigned to each other through the assignment operator "=". If it is not overloaded, the function of "=" is to assign the values of the objects on the right side of the assignment number one by one to the objects on the left side. This is equivalent to a copy of the value, called a "shallow copy.".
  • After overloading the assignment operator, the function of the assignment statement is to copy the contents pointed by the pointer member variable in one object to the place pointed by the pointer member variable in another object. Such a copy is called "deep copy".

5. Overload flow insertion operator and flow extraction operator

  • In C + +, the left shift operator "<" can be used for output together with cout, so it is often called "flow insertion operator". Shift right operators ">" and cin - used for input, are generally referred to as stream extraction operators. They are all provided in the C + + class library. In the header file provided by the class library, we have overloaded "<" and ">", which can be used to output and input data of C + + basic data types as stream insertion operator and stream extraction operator respectively. Cout is an object of ostream class and cin is an object of istream class. They are all declared in the header file iostream. Therefore, if you use "cout <" and "cin > > to output / input basic data type data, you need to use the" include "command to include the header file iostream into this program file.
  • Must overload friend of class
#include<iostream>
#include<typeinfo>
using namespace std;
class Point  
{  
private:  
    int x; 
public:  
    Point(int x1){x=x1;}  
    int get();
    Point operator++();
    Point operator++(int x);
    Point operator--();  
    Point operator--(int x);
    void operator+(const Point &p);
    void operator-(const Point &p);
    Point& operator=(const Point &p);
    operator double();
    friend void operator<<(ostream & stream,Point obj);
}; 
int Point::get(){
    return this->x;
} 
//Overloaded operator(++obj)
Point Point::operator++(){
    x++;
    return *this;
}
//Overloaded operator( obj++)
Point Point::operator++(int x){
    Point temp = *this;
    this->x++;
    return temp;
}
//Overloaded operator(--obj)
Point Point::operator--(){
    this->x--;
    return *this;
}
//Overloaded operator( obj--)
Point Point::operator--(int x){
    Point temp = *this;
    this->x--;
    return temp;
}
//overload operators+(a+b)
void Point::operator+(const Point &p){
    this->x=this->x+p.x;
}
//overload operators-(a-b)
void Point::operator-(const Point &p){
    this->x=this->x-p.x;
}
//Duplicate operator overload=(a=b),Assignment operators must be overloaded as member functions
Point& Point::operator=(const Point &p){
    this->x=p.x;
    return *this;
}
//Overloaded type conversion operator()a
Point::operator double(){
     cout << "Overloaded type conversion operator" << endl;
     return this->x;
}
//overload operators (cout <<)
void operator<<(ostream & stream,Point obj){
    stream<< obj.x <<endl;
}

int main(){
    Point point(10);
    operator<<(cout, point.operator++());//11
    Point point2(10);
    operator<<(cout, point2.operator++(0));//10

    Point point3(10);
    operator<<(cout, point3.operator--());//9
    Point point4(10);
    operator<<(cout, point4.operator--(0));//10

    Point point5(1);
    Point point6(1);
    point5.operator+(point6);
    operator<<(cout, point5);//2

    point5.operator-(point6);
    operator<<(cout, point5);//1

    Point point7(1);
    Point point8(5);
    operator<<(cout, point7.operator=(point8));//5

    Point point9(20);
    cout << typeid(point9.get()).name()<< endl;//i
    cout << point9.operator double()<< endl;
    cout << typeid(point9.operator double()).name();//d
    return 0;
}

Topics: C++