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<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class Concatenate
{
char inp_str[100];
public:
void enter_string()
{
cout<<"Input the string: ";
cin>>inp_str;
}
void show_string()
{
cout<<inp_str;
}
Concatenate operator+(Concatenate St)
{