Get the App
SLTechnology News&Howtos  ›  Development  › 

How to quickly understand operator overloading in C +

Shulou Source: shulou.com Published: 2022-06-03 01:27:58 09月19日 Update

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

Tags: Operators operations functions operators methods functions members types normal same global content meaning more users examples learning working deeper special Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Shulou Information Xiaomi macOS