c 运算符重载前置++
Hello, folks! In this article, we will understand a very interesting yet magical power provided by C++ – Operator Overloading.
大家好! 在本文中,我们将了解C ++提供的一种非常有趣但不可思议的功能- 运算符重载 。
Operator overloading provides the ability to a mathematical operator to perform other manipulation operations other than its original operation.
运算符重载为数学运算符提供了执行除原始运算以外的其他运算操作的能力 。
For example, the '+' operator
is used for arithmetic addition purpose, but with the help of operator overloading, we can use ‘+’ operator to concatenate two strings at runtime efficiently.
例如, '+' operator
用于算术加法,但借助运算符重载,我们可以使用“ +”运算符在运行时有效地连接两个字符串。
Thus, C++ has the ability to make operators work into different dimensions to perform various operators other than the designated operation of its own.
因此,C ++能够使运算符工作于不同的维度,以执行除自身指定的运算符以外的各种运算符。
Syntax:
句法:
Class_name operator_keyword operator_symbol(Class_name object)
We will get to know more about the syntax and usage further in this article.
我们将在本文中进一步了解语法和用法。
Example:
例:
#include
#include
#include
using namespace std;
class Concatenate
{
char inp_str[100];
public:
void enter_string()
{
cout<<"Input the string: ";
cin>>inp_str;
}
void show_string()
{
cout<
In the above example, we have overloaded ‘+’ operator to perform string concatenation. We have created an object of type Class and further used strcpy() function
to copy the input string and strcat() function
to associate it with the class object.
在上面的示例中,我们重载了“ +”运算符以执行字符串连接。 我们创建了一个Class类型的对象,并进一步使用strcpy() function
复制输入字符串,并使用strcat() function
将其与类对象关联。
Output:
输出:
Input the string: Journal
Input the string: Dev
The concatenated string is:
JournalDev
There are broadly two types of Operator Overloading:
大致有两种类型的运算符重载:
Unary Operator Overloading
: It works for only one operand. Unary Operator Overloading
:仅适用于一个操作数。 Binary Operator Overloading
: It works for two operands. Binary Operator Overloading
:它适用于两个操作数。 Unary Operator Overloading operators on a single operand and works with one class object respectively. Thus, we need not pass any argument to the unary overloading function.
一元运算符在单个操作数上重载运算符 ,并分别处理一个类对象。 因此,我们无需将任何参数传递给一元重载函数。
Note: In case, if we use friend function, then the unary operator can have one argument. Other than that, if unary operator function represents a class/non-static function, then we no to pass zero arguments to the function.
注意:如果使用朋友函数,则一元运算符可以有一个参数。 除此之外,如果一元运算符函数表示 类/非静态函数,则我们不将零参数传递给该函数。
Syntax: Unary Function Definition
语法: 一元函数定义
return_type operator_keyword operator_symbol()
{
//body
}
Syntax: Calling a unary operator overloading function
语法:调用一元运算符重载函数
Class_name object;
operator_symbol object;
Example:
例:
#include
#include
#include
using namespace std;
class Overload
{
int a,b;
public:
void enter_string()
{
cout<<"Input 1: ";
cin>>a;
cout<<"Input 2: ";
cin>>b;
}
void operator+()
{
a++;
b++;
cout<<"Incremented values:\n";
cout<
In the above example, we have overloaded ‘+’ operator to increment the values passed to the unary function.
在上面的示例中,我们重载了“ +”运算符,以增加传递给一元函数的值。
Output:
输出:
Input 1: 100
Input 2: 200
Incremented values:
101 201
Binary Operator Overloading function works for two operands and thus we need to pass a single argument to the binary overloading function.
二进制运算符重载函数适用于两个操作数 ,因此我们需要将一个参数传递给二进制重载函数。
Note: In case of friend function, we need to pass two arguments to the function.
注意:如果是朋友功能,我们需要将两个参数传递给该功能。
Syntax: Binary operator overloading function
语法: 二进制运算符重载函数
return_type operator_keyword operator_symbol(Class_name argument1)
{
//body
}
Thus, now it must be clear for all of you, that the example in the beginning of this tutorial is of type binary overloading.
因此,对于所有人来说,现在很清楚,本教程开始的示例是二进制重载类型。
Syntax: Calling a unary operator overloading function
语法:调用一元运算符重载函数
Class_name obj1, obj2, obj3;
obj3 = obj1 operator_symbol obj2;
Example:
例:
#include
#include
#include
using namespace std;
class Overload
{
public:
int num1, num2;
void enter_num()
{
cout<<"Input 1: ";
cin>>num1;
cout<<"Input 2: ";
cin>>num2;
}
Overload operator+(Overload St)
{
Overload ob;
ob.num1 = this->num1 + St.num1;
ob.num2 = this->num2 + St.num2;
return ob;
}
};
int main()
{
Overload A, B, res;
cout<<"Enter the input values for object A:\n";
A.enter_num();
cout<<"Enter the input values for object B:\n";
B.enter_num();
res=A+B;
cout<<"Result:\n";
cout<
In the above example, we have overloaded ‘+’ operator to add two class objects.
在上面的示例中,我们重载了“ +”运算符以添加两个类对象。
Output:
输出:
Enter the input values for object A:
Input 1: 10
Input 2: 10
Enter the input values for object B:
Input 1: 20
Input 2: 30
Result:
30 40
Thus, in this article, we have understood the working of Operator overloading along with the different types of overloading functions in C++.
因此,在本文中,我们了解了运算符重载的工作方式以及C ++中不同类型的重载函数。
翻译自: https://www.journaldev.com/38315/c-plus-plus-operator-overloading
c 运算符重载前置++