c加加+-*/前置++后置++运算符重载
来源:互联网 发布:黑界扣字软件 编辑:程序博客网 时间:2024/06/10 15:07
运算符重载注意点:
1.算术和关系操作符返回的是一个左值或右值,而不是一个引用
2.赋值操作符一定要定义为成员函数如“=”
3.一般而言,赋值操作符和复合赋值操作符应返回左操作数的引用如"="和''+="
C++不允许赋值运算符被重载为全局形式,这是因为如果可以写出全局形式的赋值运算符函数的话,我们可以写出这样的函数:
- int operator=(int a, integer b);
从而出现这样的语句:
- integer a(3);
- 2 = a;
现在我们写一个简单的integer类并重载赋值运算符:
因为在自赋值的情况下可能给对象造成伤害,所以在重载赋值运算符时必须要注意自赋值的情况
所以integer类中的赋值运算符函数应写成这样:
- integer& integer::operator=(const integer& a)
- {
- if(this != &a)
- i = a.i;
- return *this;
- };
1、 运算符重载的概念
运算符重载是C++的重要组成部分,它可以让程序更加简单易懂,简单的运算符可以使复杂函数的理解更直观。对于普通对象来说可以使用算术运算符让它们参与计算,C++也允许为类的对象构造运算符来实现单目或双目运输,这个特性就叫运算符的重载。其实,任何使用运算符完成的功能,使用普通的函数也能够完成。运算符的重载主要存在两种形式,一种是作为类的友元函数进行使用,另一种则是作为类的成员函数进行使用。运算符的重载的形式为:
2、 运算符的运算规则
- 返回类型 operator 运算符符号(参数说明)
- { //函数体的内部实现
- }
①运算符重载函数也是函数,重载的运算符不会改变运算符的优先级、结合型和参数的个数。
②重载运算符不能违反语言的语法规则。
③赋值运算符除外,重载运算符可由派生类继承下去。
④重载运算符不能使用默认参数。
⑤运算符=、()、[]和->可作为类成员运算符,不能作为友元运算符。
⑥运算符“.”、“::”、“?:”不能重载。
⑦友元运算符的参数规则与类成员运算符的参数规则不同,一员运算符必须显示地声明一个参数,二员运算符必须显示地声明两个参数。类成员运算符重载时,参数中隐含了一个this指针。
3、 实例代码
1)友员
- #include <iostream>
- using std::cout;
- using std::endl;
- class Complex
- {
- public:
- //Attribute
- int x;
- int y;
- //Operator
- void SetX(int a){x=a;}
- void SetY(int b){y=b;}
- friend Complex operator +(Complex &, Complex &);
- friend Complex operator -(Complex &, Complex &);
- friend Complex operator *(Complex &, Complex &);
- friend Complex operator /(Complex &, Complex &);
- friend Complex operator ++(Complex &);//前置方式
- friend Complex operator ++(Complex &, int);//后置方式
- };
- // "+"重载运算符
- Complex operator +(Complex& temp1,Complex& temp2 )
- {
- Complex ret;
- ret.x=temp1.x+temp2.x;
- ret.y=temp1.y+temp2.y;
- return ret;
- }
- // "-"重载运算符
- Complex operator -(Complex& temp1,Complex& temp2 )
- {
- Complex ret;
- ret.x=temp1.x-temp2.x;
- ret.y=temp1.y-temp2.y;
- return ret;
- }
- // "*"重载运算符
- Complex operator *(Complex& temp1,Complex& temp2 )
- {
- Complex ret;
- ret.x=temp1.x*temp2.x;
- ret.y=temp1.y*temp2.y;
- return ret;
- }
- // "/"重载运算符
- Complex operator /(Complex& temp1,Complex& temp2 )
- {
- Complex ret;
- ret.x=temp1.x/temp2.x;
- ret.y=temp1.y/temp2.y;
- return ret;
- }
- // "++"前置运算符
- Complex operator ++(Complex& temp1)
- {
- temp1.x=temp1.x+1;
- temp1.y=temp1.y+1;
- return temp1;
- }
- // "++"后置运算符
- Complex operator ++(Complex& temp1,int)
- {
- temp1.x=temp1.x++;
- temp1.y=temp1.y++;
- return temp1;
- }
- //主函数()
- int main()
- {
- Complex Complex1;
- Complex Complex2;
- Complex Ret;
- Complex1.SetX(30);
- Complex1.SetY(40);
- Complex2.SetX(10);
- Complex2.SetY(20);
- cout<<"重载加法运算"<<endl;
- Ret=Complex1+Complex2;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"重载减法运算"<<endl;
- Ret=Complex1-Complex2;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"重载乘法运算"<<endl;
- Ret=Complex1*Complex2;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"重载除法运算"<<endl;
- Ret=Complex1/Complex2;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"前置++运算"<<endl;
- Ret=++Complex1;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"后置++运算"<<endl;
- Ret=Complex1++;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- return 0;
- }
2)非友员,相对于友员少了一个类对象参数
- #include <iostream>
- using std::cout;
- using std::endl;
- class Complex
- {
- public:
- //Attribute
- int x;
- int y;
- //Operator
- void SetX(int a){x=a;}
- void SetY(int b){y=b;}
- //成员函数
- Complex operator +(Complex &);
- Complex operator -(Complex &);
- Complex operator *(Complex &);
- Complex operator /(Complex &);
- Complex& operator ++();//前置方式
- Complex& operator ++(int);//后置方式
- };
- // "+"重载运算符
- Complex Complex::operator +(Complex& temp1)
- {
- Complex ret;
- ret.x=x+temp1.x;
- ret.y=y+temp1.y;
- return ret;
- }
- // "-"重载运算符
- Complex Complex::operator -(Complex& temp1)
- {
- Complex ret;
- ret.x=x-temp1.x;
- ret.y=y-temp1.y;
- return ret;
- }
- // "*"重载运算符
- Complex Complex::operator *(Complex& temp1)
- {
- Complex ret;
- ret.x=x*temp1.x;
- ret.y=y*temp1.y;
- return ret;
- }
- // "/"重载运算符
- Complex Complex::operator /(Complex& temp1)
- {
- Complex ret;
- ret.x=x/temp1.x;
- ret.y=y/temp1.y;
- return ret;
- }
- // "++"前置运算符
- Complex& Complex::operator ++()
- {
- x=x+1;
- y=y+1;
- return *this;
- }
- // "++"后置运算符
- Complex& Complex::operator ++(int)
- {
- x=x++;
- y=y++;
- return *this;
- }
- //主函数()
- int main()
- {
- Complex Complex1;
- Complex Complex2;
- Complex Ret;
- Complex1.SetX(30);
- Complex1.SetY(40);
- Complex2.SetX(10);
- Complex2.SetY(20);
- cout<<"重载加法运算"<<endl;
- Ret=Complex1+Complex2;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"重载减法运算"<<endl;
- Ret=Complex1-Complex2;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"重载乘法运算"<<endl;
- Ret=Complex1*Complex2;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"重载除法运算"<<endl;
- Ret=Complex1/Complex2;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"前置++运算"<<endl;
- Ret=++Complex1;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- cout<<"后置++运算"<<endl;
- Ret=Complex2++;
- cout<<"Ret.x="<<Ret.x<<endl;
- cout<<"Ret.y="<<Ret.y<<endl;
- return 0;
- }
- c加加+-*/前置++后置++运算符重载
- [C++] C++的运算符重载(+、-、前置--、后置--,前置++,后置++、==)
- [C++] C++的运算符重载(+、-、前置--、后置--,前置++,后置++、==)
- (未完成)C++ 重载后置自加运算符
- 运算符前置++和后置++的重载
- ++运算符的重载前置与后置
- 前置和后置++运算符的重载
- ++ 前置自加 1 运算符重载示例
- 前置加加后置加加,易出错题目
- 重载前置++运算符和后置++运算符
- 对象前置++,前置--;后置++,后置--(没有利用《和》重载运算符)
- 运算符重载(前置/后置++、前置/后置--和+=/-=/-/+/=)
- Time类中的运算符重载(3)-前置++,后置++
- C++前置++、后置++、不等号!及赋值运算符重载
- C++中前置++与后置++运算符重载
- 前置自加与后置自加的理解
- C++运算符前置后置
- 前置和后置运算符
- HTML中标签的嵌套原则
- ie9下出现bug,打开f12bug就消失问题解决
- EasyUI 弹出窗口用法
- 第五章 5-1 曲线的画法
- linux--vi
- c加加+-*/前置++后置++运算符重载
- debug assertion failed
- BZOJ2788: [Poi2012]Festival 差分约束
- 剑指offer 数组中重复的数字
- Django项目开发举例之应用的管理界面(3)
- 【JZOJ5068】【GDSOI2017第二轮模拟】树
- 暗黑游戏
- sql插入数据,varchar转int
- 『Java编程思想-第四版』第二章:一切都是对象