C++ overloaded operators and overloaded functions

C++ allows multiple definitions of a function and operator in the same scope , called function overloading and operator overloading , respectively .

An overloaded declaration is a declaration with the same name as a function or method that has been previously declared in that scope, but with a different parameter list and definition (implementation).

When you call an overloaded function or overloaded operator , the compiler determines the most appropriate definition by comparing the parameter types you are using with the parameter types in the definition. The process of choosing the most appropriate overloaded function or overloaded operator is called overload resolution .

Function overloading in C++

In the same scope, you can declare several functions with the same name with similar functions, but the formal parameters (referring to the number, type or order of parameters) of these functions with the same name must be different. You can't overload a function just by the difference in return type.

In the following example, the function print() of the same name is used to output different data types:

example

#include <iostream>
using namespace std;

class printData
{
public:
	void print(int i)
	{
		cout << "整数为: " << i << endl;
	}

	void print(double  f)
	{
		cout << "Floating point numbers are: " << f << endl;
	}

	void print(string c)
	{
		cout << "The string is: " << c << endl;
	}
};

int main(void)
{
	printData pd;

	// output integer
	pd.print(5);
	// output float
	pd.print(500.263);
	// output string
	pd.print("Hello C++");

	return 0;
}

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

Integer is: 5
Floating point is: 500.263
The string is: Hello C++

Operator overloading in C++

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

An overloaded operator is a function with a special name consisting of the keyword operator followed by the operator symbol to be overloaded. Like other functions, overloaded operators have 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 follows:

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

The following example demonstrates the concept of operator overloading using member functions. Here, the object is passed as a parameter, and the properties of the object are accessed using the this operator as follows:

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 = hey;
	}
	// overloaded + operator for adding 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
};
// main function of the program
intmain()
{
	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);

	// The volume of Box1
	volume = Box1.getVolume();
	cout << "Volume of Box1 : " << volume << endl;

	// The volume of Box2
	volume = Box2.getVolume();
	cout << "Volume of Box2 : " << volume << endl;

	// Add the two objects to get Box3
	Box3 = Box1 + Box2;

	// The 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 operator/non-overloadable operator

The following is a list of overloadable operators:

Binary Arithmetic Operators + (addition), - (subtraction), * (multiplication), / (division), % (modulo)
relational operator == (equal to), != (not equal to), < (less than), > (greater than >, <= (less than or equal), >= (greater than or equal)
Logical Operators || (logical or), && (logical and), ! (logical not)
unary operator + (positive), - (negative), * (pointer), & (address)
increment/decrement operator ++ (self-increment), -- (self-decrement)
bitwise operators | (bitwise OR), & (bitwise AND), ~ (bitwise negation), ^ (bitwise exclusive OR), << (left shift), >> (right shift)
assignment operator =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
Space application and release new, delete, new[ ] , delete[]
other operators () (function call), -> (member access), , (comma), [] (subscript)

The following is a list of non-overloaded operators:

  • . : member access operator
  • .* , ->* : member pointer access operators
  • :: : domain operator
  • sizeof : length operator
  • ?: : conditional operator
  • # : preprocessor symbol

operator overloading example

Examples of various operator overloading are provided below to help you better understand the concept of overloading.

序号 运算符和实例
1 一元运算符重载
2 二元运算符重载
3 关系运算符重载
4 输入/输出运算符重载
5 ++ 和 -- 运算符重载
6 赋值运算符重载
7 函数调用运算符 () 重载
8 下标运算符 [] 重载
9 类成员访问运算符 -> 重载

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325684117&siteId=291194637