C + + operator overloading

preface           Recently, I learned about operator overloading in c + +. Because there are po...

preface

          Recently, I learned about operator overloading in c + +. Because there are points to pay attention to overloading for different operators and different overloading methods, I will sort out the overloading methods (member functions and friend functions) and overloading contents (ordinary operators, taking + + operators as an example)

1. Overload mode:

        1) Operator overloading of member functions requires that the first parameter must be a user-defined object. The return type is a custom object. Therefore, it is usually used for operations between objects of the same type.

      2) The operator overloading of friend functions is relatively flexible and can realize the operation between different data types. However, when defining a function, pay attention to the left operands of the operator corresponding to the first formal parameter and the right operands of the operator corresponding to the second formal parameter. The order cannot be reversed.

2. + + operator overloading

      The difference between i + + and + + i operator overloaded functions is that i + + overloaded functions have one more int type parameter

3. Stream insertion operator overload   “<<”     And stream extraction operator overloading   “>>”

friend ostream & operator<<(ostream &out, Complex& c1);
friend istream& operator>>(istream& in, Complex& c1);

      1)     Stream operator overloading can only be implemented using friend functions.

      2) The first parameter of the stream insertion and stream extraction overloaded functions is fixed as ostream and istream objects respectively:

      3) The return types of stream insertion and stream extraction overloaded functions are ostream and istream.

      4) The overloaded function uses the ostream (istream) object passed in to call the output (input) stream

Here is a code example

1, Two overload modes 1. Member function implementation
class Complex { private: double real; double imag; public: Complex() { real = 0; imag = 0; } Complex(double r, double m) { real = r; imag = m; } double getReal(); double getImag(); void display(); //The member function defines the operation of adding two Complex objects Complex operator+(Complex& c1); };

  Note:
     1. The return value must be a Compare object
     2. The left side of + is required to be a Compare object, so it can realize the operation of (1 + 2i) + 2, but it can not realize the operation of 2 + (1 + 2i), because the left side here is int, and this problem can be solved through friends.

2. Using friend functions
class Complex { private: double real; double imag; public: Complex() { real = 0; imag = 0; } Complex(double r, double m) { real = r; imag = m; } double getReal(); double getImag(); void display(); //The friend function defines the operation of adding two Complex objects friend Complex operator+(Complex& c2, Complex& c3); //The following is to realize the operation of Complex object and int data //1. Complex object + int because the first parameter is a complex object, it can also be set as a member function and only int is passed in friend Complex operator+(Complex& c1, int n); //Complex operator+(int n); // This is equivalent to this.operator+(n) adding n to the current object //2. Because the first parameter of int+Complex object is int, member functions cannot be used friend Complex operator+(int n, Complex& c1); };

Note:
        1. The data type of the first parameter corresponds to the operand type on the left of + and the type of the second parameter corresponds to the operand type on the right.
        2. The return value may not be a Complex object

2, Operator overloading

1. Code example

      Overloaded + + operator implementation. When the number of seconds reaches 60, the number of seconds is cleared and the fraction is added by one

1)Time1.h

#pragma once #include "Date.h" class Time1 { int hour; //Time int minute; //branch int sec; //second public: void set_time(); void show_time(); Time1(int, int, int); Time1 operator++(); //Add + + i before Time1 operator++(int); //i + + is added after addition. In order to distinguish the former addition, an int type parameter is added };

2)Time1.cpp

#include <iostream> #include "Time1.h" #include "Date.h" #include "student.h" using namespace std; Time1::Time1(int h,int m,int s):hour(h), minute(m), sec(s){} //++i operator overload Time1 Time1::operator++() { if (++sec >= 60) { //First put 59 + 1 and then judge whether it is greater than 60 sec = 0; minute += 1; } return *this; } //i + + operator overloading. Note that multiple int parameters are distinguished from + + i overloaded functions Time1 Time1::operator++(int) { int t = ++sec; //Judge the value of seconds plus one if (t >= 60) { sec = 0; minute += 1; } return *this; } //time object initialization void Time1::set_time() { //Note that the qualifier of the class function is written after the function type cout << "Please enter hour, minute and second:" << endl; cin >> hour; cin >> minute; cin >> sec; } // time object display void Time1::show_time() { cout << hour<<"-"; cout << minute << "-"; cout << sec; }
3, Overloaded stream insertion operator and stream extraction operator

class Complex { private: double real; double imag; public: Complex() { real = 0; imag = 0; } Complex(double r, double m) { real = r; imag = m; } double getReal(); double getImag(); void display(); //Overloaded stream insert operator output<< friend ostream & operator<<(ostream &out, Complex& c1); //Overloaded stream extraction operator input > > friend istream& operator>>(istream& in, Complex& c1); }; ostream &operator<<(ostream & out, Complex& c1) { //Note that the ostream object out is used here to call the output stream out << c1.real << "+" << c1.imag << "i"; return out; //Returning to the current out object, you can call the overloaded function continuously } istream& operator>>(istream& in, Complex& c1) { cout << "Real part:"; //Note that the istream object in is used here to call the input stream in >> c1.real; cout << "Imaginary part:"; in >> c1.imag; return in;//Returning to the current in object, you can call the overloaded function continuously } int main(){ Complex c1,c2,c3; cin >> c1; // The cin object here is equivalent to the first formal parameter in operator > > (out, C1) cout << c1; //Next, the overloaded function is called twice, because the overloaded function returns the ostream (istream) object, //Information can be continuously inserted into the output stream (input stream) cin >> c1>>c2; cout << c1 <<"\t"<< c2; }

    be careful:  

        1,   Overloading of stream operators can only be implemented in the form of friend functions. Because defining a member function implementation requires that the first parameter be a custom object. When actually called, the first parameter is the ostream (istream) object.

      2. The overloaded function returns an ostream (istream) object, so you can continuously insert information into the input and output streams

summary

1. Overload mode:

        1) Operator overloading of member functions requires that the first parameter must be a user-defined object. The return type is also a custom object in the. Therefore, it is usually used for operations between objects of the same type.

      2) The operator overloading of friend functions is relatively flexible and can realize the operation between different data types. However, when defining a function, pay attention to the left operands of the operator corresponding to the first formal parameter and the right operands of the operator corresponding to the second formal parameter. The order cannot be reversed.

2. + + operator overloading

      The difference between i + + and + + i operator overloaded functions is that i + + overloaded functions have one more int type parameter

3. Stream insertion operator overload   “<<”     And stream extraction operator overloading   “>>”

friend ostream & operator<<(ostream &out, Complex& c1);
friend istream& operator>>(istream& in, Complex& c1);

      1)     Stream operator overloading can only be implemented using friend functions.

      2) The first parameter of the stream insertion and stream extraction overloaded functions is fixed as ostream and istream objects respectively:

      3) The return types of overloaded functions of stream insertion and stream extraction are ostream and istream objects.

      4) The overloaded function uses the ostream (istream) object passed in to call the output (input) stream

30 October 2021, 23:25 | Views: 6781

Add new comment

For adding a comment, please log in
or create account

0 comments