C++ Overloading operators and overloading functions

C++ allows a certain [function] in the same scopeFunctionandOperatorsSpecify multiple definitions, respectively calledFunction overloadingandOperator overloading。

An overload declaration is a declaration that has the same name as a function or method previously declared in that scope, but their parameter lists and definitions (implementations) are different.

When you call aOverloaded functionorOverloaded operatorthe compiler decides which definition is most suitable by comparing the parameter types you use with the parameter types in the definition. The process of selecting the most suitable overloaded function or overloaded operator is calledOverload resolution。

Function overloading in C++

In the same scope, you can declare several functions with similar functionality and the same name, but these same-named functions must have different formal parameters (referring to the number, type, or order of parameters). You cannot overload functions based only on different return types.

In the following example, functions with the same nameprint()It is used to output different data types:

Example

#include <iostream> using namespace std; class printData { public: void print(int i) { cout << "The integer is:" << i << endl; } void print(double f) { cout << "The floating-point number is:" << f << endl; } void print(char c[]) { cout << "The string is:" << c << endl; } }; int main(void) { printData pd; // Output integer pd.print(5); // Output floating-point number pd.print(500.263); // Output string char c[] = "Hello C++"; pd.print(c); return 0; }

When the above code is compiled and executed, it produces the following results:

整数为: 5
浮点数为: 500.263
字符串为: Hello C++

Operator overloading in C++

You can redefine or overload most of C++'s built-in operators. In this way, you can use operators with custom types.

Overloaded operators are functions with special names. The function name is composed of the keyword `operator` followed by the operator symbol to be overloaded. Like other functions, an overloaded operator has a return type and a parameter list.

Box operator+(const Box&);

Declare the addition operator to add two Box objects and return the final Box object. Most overloaded operators can be defined as ordinary non-member functions or as class member functions. If we define the above function as a non-member function of the class, then we need to pass two parameters for each operation, as shown below:

Box operator+(const Box&, const Box&);

The following example demonstrates the concept of operator overloading using member functions. Here, objects are passed as arguments, and the object's properties are accessed usingthisthe operator, as shown below:

Example

#include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } // Overload the + operator to add two Box objects Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } private: double length; // Length double breadth; // Width double height; // Height }; // The main function of the program int main( ) { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box Box Box3; // Declare Box3 of type Box double volume = 0.0; // Store the volume in this variable // Box1 details Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // Box2 details Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // Volume of Box1 volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; // Volume of Box2 volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; // Add the two objects to get Box3 Box3 = Box1 + Box2; // Volume of Box3 volume = Box3.getVolume(); cout << "Volume of Box3 : " << volume <<endl; return 0; }

When the above code is compiled and executed, it produces the following results:

Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

Overloadable operators / Non-overloadable operators

The following is a list of operators that can be overloaded:

Binary arithmetic operators + (addition), - (subtraction), * (multiplication), / (division), % (modulo)
Relational Operators == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to)
Logical Operators || (logical OR), && (logical AND), ! (logical NOT)
Unary operator + (positive), - (negative), * (dereference), & (address-of)
Increment and decrement operators ++ (increment), -- (decrement)
Bitwise Operators | (bitwise OR), & (bitwise AND), ~ (bitwise NOT), ^ (bitwise XOR), << (left shift), >> (right shift)
Assignment Operators =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
Memory allocation and release new, delete, new[ ] , delete[]
Other Operators ()(function call),->(member access),,(comma),[](subscript)

The following is a list of operators that cannot be overloaded:

  • .Member access operator
  • .*, ->*Member pointer access operator
  • ::: scope operator
  • sizeof: length operator
  • ?:: conditional operator
  • #: preprocessing symbol

Operator overloading example

The following provides various examples of operator overloading to help you better understand the concept of overloading.

Serial NumberOperators and examples
1Unary Operator Overloading
2Binary operator overloading
3Relational Operator Overloading
4Input/Output Operator Overloading
5Overloading ++ and -- Operators
6Assignment Operator Overloading
7Function call operator () overloading
8Subscript Operator [] Overloading
9Class member access operator -> overloading
other extensions