How to quickly understand operator overloading in C +
This article focuses on "how to quickly understand operator overloading in C +". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to quickly understand operator overloading in C +.
In C +, we can make operators work for user-defined classes. This means that C + + can provide operators with a special meaning of the data type, which is called operator overloading. For example, we can reload the operator'+'in a class such as String so that we can concatenate two strings using only +. Other example classes where arithmetic operators may be overloaded are plural, decimal, large integers, and so on.
A simple and complete example
# include using namespace std; class Complex {private: int real, imag; public: Complex (int r = 0, int I = 0) {real = r; imag = I;} Complex operator + (Complex const & obj) {Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res;} void print () {cout