c 运算符重载前置++_C ++运算符重载–综合指南

c 运算符重载前置++

Hello, folks! In this article, we will understand a very interesting yet magical power provided by C++ – Operator Overloading.

大家好! 在本文中,我们将了解C ++提供的一种非常有趣但不可思议的功能- 运算符重载



操作员重载入门 (Getting started with Operator Overloading)

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


运营商超载总则 (General Rules of Operator Overloading)

  • Operator overloading can overload only the predefined operators. No new operator can be created or overloaded.

    运算符重载只能使预定义的运算符重载。 不能创建或重载新的运算符。
  • The basic requirement of the operators cannot be changed. For example, for ‘+’ operator it is necessary for us to supply two variables to the function. Thus, this needs to be maintained.

    操作员基本要求不能改变 。 例如,对于“ +”运算符,我们有必要向函数提供两个变量。 因此,这需要保持。
  • The overloaded operators do not have default arguments.

    重载的运算符没有默认参数
  • The associativity and precedence of the operators cannot be changed.

    运算符的关联性优先级 无法更改
  • While performing operator overloading, there has to be at least one operand of user-defined type.

    执行运算符重载时,必须至少有一个用户定义类型的操作数。
  • We cannot overload the following operators: ‘.'(dot) operator, scope resolution operator(::), Ternary operator(?:) and sizeof operator.

    我们不能重载以下运算符: '。'(点)运算符范围分辨率运算符(::)三元运算符(?:)sizeof运算符


C ++中的运算符重载类型 (Types of Operator Overloading in C++)

There are broadly two types of Operator Overloading:

大致有两种类型的运算符重载:

  1. Unary Operator Overloading: It works for only one operand.

    Unary Operator Overloading :仅适用于一个操作数。
  2. Binary Operator Overloading: It works for two operands.

    Binary Operator Overloading :它适用于两个操作数。


1.一元运算符重载 (1. Unary 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


2.二进制运算符重载 (2. Binary Operator Overloading)

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


操作员超载一目了然! (Operator Overloading at a glance!)

  • Thus, Operator overloading redefines the usual or basic functionality of the operator.

    因此,操作员超载重新定义了操作员的常规或基本功能。
  • It preserves the original function of the operators and work with user defined types only.

    它保留了运算符的原始功能,并且仅与用户定义的类型一起使用。
  • Operator overloading function can be used as class or friend function too.

    运算符重载函数也可以用作类或朋友函数。


结论 (Conclusion)

Thus, in this article, we have understood the working of Operator overloading along with the different types of overloading functions in C++.

因此,在本文中,我们了解了运算符重载的工作方式以及C ++中不同类型的重载函数。



参考资料 (References)

  • Operator Overloading in C++ — Official Documentation

    C ++中的运算符重载—官方文档

翻译自: https://www.journaldev.com/38315/c-plus-plus-operator-overloading

c 运算符重载前置++

你可能感兴趣的:(c++,java,python,类,php)