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)  {  Concatenate ob;  strcat(inp_str,St.inp_str);  strcpy(ob.inp_str,inp_str);  return ob;  }
};
int main()
{  Concatenate st1,st2,res;   st1.enter_string();  st2.enter_string();  res=st1+st2;cout<<"The concatenated string is:\n";res.show_string();   return 0;
}

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<iostream>
#include<conio.h>
#include<string.h>
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<<a<<'\t'<<b;}
};
int main()
{  Overload obj;obj.enter_string();+obj;return 0;
}

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<iostream>
#include<conio.h>
#include<string.h>
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<<res.num1<<'\t'<<res.num2; return 0;
}

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 DocumentationC ++中的运算符重载—官方文档

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

c 运算符重载前置++

c 运算符重载前置++_C ++运算符重载–综合指南相关推荐

  1. C++_类和对象_C++运算符重载_递增运算符重载_重载++运算符_前置++_后置++重载---C++语言工作笔记057

    然后我们再来看这个递增运算符的重载. 我们知道,递增运算符,有前置++,和后置++,可以看到,上面写了 然后我们这次就自己定义一个int的类型,MyInteger,然后实现,递增,递减的操作. 首先我 ...

  2. 运算符重载(加减运算符、前置加加(减减)后置加加(减减)运算符、赋值运算符、输入输出运算符、关系运算符、函数调用)

    编译器对于一个类会默认生成以几种函数: 1.默认构造函数(空形参,空函数体) 2.默认拷贝构造函数(浅拷贝,也叫值拷贝.字节拷贝) 3.析构函数(空形参,空函数体.析构函数要求形参列表必须是空的,所以 ...

  3. c#中重载单目运算符-_C#程序重载二进制运算符(-,*,/)

    c#中重载单目运算符- Here, we will design overloaded methods for binary operators: minus, multiply and divide ...

  4. C++_类和对象_C++运算符重载_函数调用运算符重载_---C++语言工作笔记060

    然后我们再来看函数调用运算符重载, 其实这个,重载后的,函数调用运算符有点像,仿函数,什么是仿函数,一会我们再说 我们去写一个MyPrint类 里面重载函数调用运算符,可以看到函数调用运算符的重载的写 ...

  5. 重载运算与类型转换——基本概念,输入和输出运算符,算术和关系运算符,赋值运算符,下标运算符,递增和递减运算符,成员访问运算符...

    一.基本概念 重载的运算符时具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.和其他函数一样,重载的运算符也包含返回类型.参数列表以及函数体. 重载运算符函数的参数 ...

  6. c++,运算符重载,左移运算符自增运算符。

    假设有类A,它的私有成员是一个整型变量a_ A a; 我们想要把它的私有成员直接用 cout<<a<<endl 输出的话要用到左移运算符的重载. 我们把<<运算符重 ...

  7. c++重载(以运算符重载为主)

    重载(OverLoading)是面向对象程序设计多态性的一种体现.所谓重载,是指"同一标识符"在同一作用域的不同场合具有不同的语义,这个标识符可以是函数名或运算符.也就是说,重载可 ...

  8. python 运算符重载_Python3面向对象-运算符重载

    1:运算符重载介绍 运算符重载,就是在某个类的方法中,拦截其内置的操作(比如:+,-,*,/,比较,属性访问,等等),使其实例的行为接近内置类型. 当类的实例出现在内置操作中时(比如:两个实例相加 + ...

  9. C++ 重载强制类型转换运算符

    C++ 中,类型的名字(包括类的名字)本身也是一种运算符,即类型强制转换运算符. 类型强制转换运算符是单目运算符,也可以被重载,但只能重载为成员函数,不能重载为全局函数.经过适当重载后,(类型名)对象 ...

最新文章

  1. 如何解决diff: /../Podfile.lock: No such file or directory 的问题
  2. Hadoop 2.2.0安装和配置lzo
  3. MySQL 账户管理
  4. JavaScript this指向相关内容
  5. Excel为整列设置函数
  6. python分位数回归模型_如何理解分位数回归风险价值 (VaR) 模型?
  7. log4j slf4j实现_slf4j 与log4j logback的区别以及使用场景
  8. Linux之ssh服务默认端口修改
  9. 阿里P8架构师谈:Quartz调度框架详解、运用场景、与集群部署实践
  10. 简易自动电阻测试仪_开始自动测试您的网站的简单方法
  11. ES6 变量的6种方式
  12. transit, transfer, convey-conveyance, transport-transportation
  13. 车牌自动识别在智能交通系统中的应用及前景分析
  14. 提高工作效率的12个小建议
  15. 用计算机画画的图片,儿童电脑画画大全图片
  16. 一个公式,把营销从玄学变回科学
  17. Cookie存储购物车
  18. mysqld: File './mysql-bin.index' not found (Errcode: 13 - Permission denied)
  19. 自定义类加载器加载冲突类(一)-ClassLoader
  20. MFC程序逆向 – 消息篇(上)+(下)

热门文章

  1. AE “每用户订阅上的所有人SID 不存在”
  2. [置顶] Android仿人人客户端(v5.7.1)——应用主界面之左侧面板UI实现
  3. android style 与theme的区别
  4. UIProgressView的详细使用
  5. FreeMarker 基础语法教程
  6. Android ADV 虚拟卡常见错误Failed to push的解决
  7. nhibernate源码分析之六: Criteria数据加载
  8. [转载] python numpy 总结
  9. Java匹马行天下之 Java国出了个Java——举国欢庆
  10. Django-manage.py