C++运算符重载——重载二元运算符
1、重载二元操作符的方法
二元运算符又称为双目运算符,即需要2个操作数的运算符,例如 + - * / 等。
运算符重载可以分为3种方式:类的非静态成员函数、类的友元函数、普通函数。
例如有 2 个操作数 a 和 b,二元运算符 ? (表示一个二元运算符),a ? b 的操作会被解释为下面2种形式之一
//a ? b
a.operator?(b); //类的非静态成员函数
operator?(a, b); //友元函数 和 普通函数
第一种形式是运算符被重载为类的非静态成员函数,
这种方式要求运算符左边的的操作数(即第一个操作数a)必须是一个对象,operator?是这个对象的非静态成员函数
并且只能有一个参数。
第二种形式是运算符被重载为类的友元函数 或 普通函数,
这种方式需要2个参数,
重载为 类的友元函数 和 普通函数的区别是 类的友元函数可以直接访问类的私有成员,而普通函数不可以。
2、应用举例(对象 ? 对象)
下例中有3个complex类 ComplexA、ComplexB 和 ComplexC,3个类都重载了加减乘除 运算符。
其中ComplexA使用类的非静态成员函数方式重载,ComplexB使用类的友元函数方式重载,ComplexC使用普通函数方式重载。
需要注意的是复数的加减乘除运算的算法是有问题的,只是一个说明重载方法的例子,
另外重载函数的参数最好使用const关键字限定,至于返回值是否用const限定,需要取决于你的设计,比如允许C3 = ++(C1+C2)这种情况,就不能用cosnt限定。
至于不同类型的对象间的操作,通常是没有意义的。
#include <iostream>
using namespace std; class ComplexA
{
public:
//默认构造函数(Default constructor)
ComplexA(){cout<<"Default Constructor"<<endl;}
//带参数的构造函数(The constructor with parameters)
ComplexA(double re, double im):real(re),image(im){cout<<"Parameter Constructor"<<endl;}
//拷贝构造函数(Copy constructor)
ComplexA(const ComplexA& ref){real = ref.real; image = ref.image; cout<<"Copy Constructor"<<endl;}
//析构函数(destructor)
~ComplexA(){cout<<"Destructor"<<endl;} //Operator Overload : +
ComplexA operator+(ComplexA& ref)
{
return ComplexA(real + ref.real, image + ref.image);
} //Operator Overload : -
ComplexA operator-(ComplexA& ref)
{
return ComplexA(real - ref.real, image - ref.image);
} //Operator Overload : *
ComplexA operator*(ComplexA& ref)
{
return ComplexA(real * ref.real, image * ref.image);
} //Operator Overload : /
ComplexA operator/(ComplexA& ref)
{
return ComplexA(real / ref.real, image / ref.image);
} //display
void display(void){cout<<real<<"+"<<image<<"i"<<endl;}
private:
double real; //复数的实部
double image; //复数的虚部
}; class ComplexB
{
public:
//默认构造函数(Default constructor)
ComplexB(){cout<<"Default Constructor"<<endl;}
//带参数的构造函数(The constructor with parameters)
ComplexB(double re, double im):real(re),image(im){cout<<"Parameter Constructor"<<endl;}
//拷贝构造函数(Copy constructor)
ComplexB(const ComplexB& ref){real = ref.real; image = ref.image; cout<<"Copy Constructor"<<endl;}
//析构函数(destructor)
~ComplexB(){cout<<"Destructor"<<endl;} //Operator Overload : +
friend ComplexB operator+(ComplexB& ref1, ComplexB& ref2)
{
return ComplexB(ref1.real + ref2.real, ref1.image + ref2.image);
} //Operator Overload : -
friend ComplexB operator-(ComplexB& ref1, ComplexB& ref2)
{
return ComplexB(ref1.real - ref2.real, ref1.image - ref2.image);
} //Operator Overload : *
friend ComplexB operator*(ComplexB& ref1, ComplexB& ref2)
{
return ComplexB(ref1.real * ref2.real, ref1.image * ref2.image);
} //Operator Overload : /
friend ComplexB operator/(ComplexB& ref1, ComplexB& ref2)
{
return ComplexB(ref1.real / ref2.real, ref1.image / ref2.image);
} //display
void display(void){cout<<real<<"+"<<image<<"i"<<endl;}
private:
double real; //复数的实部
double image; //复数的虚部
}; class ComplexC
{
public:
//默认构造函数(Default constructor)
ComplexC(){cout<<"Default Constructor"<<endl;}
//带参数的构造函数(The constructor with parameters)
ComplexC(double re, double im):real(re),image(im){cout<<"Parameter Constructor"<<endl;}
//拷贝构造函数(Copy constructor)
ComplexC(const ComplexC& ref){real = ref.real; image = ref.image; cout<<"Copy Constructor"<<endl;}
//析构函数(destructor)
~ComplexC(){cout<<"Destructor"<<endl;} //Get Data
double GetReal(void){return real;}
double GetImage(void){return image;} //display
void display(void){cout<<real<<"+"<<image<<"i"<<endl;}
private:
double real; //复数的实部
double image; //复数的虚部
}; //Operator Overload : +
ComplexC operator+(ComplexC& ref1, ComplexC& ref2)
{
return ComplexC(ref1.GetReal() + ref2.GetReal(), ref1.GetImage() + ref2.GetImage());
} //Operator Overload : -
ComplexC operator-(ComplexC& ref1, ComplexC& ref2)
{
return ComplexC(ref1.GetReal() - ref2.GetReal(), ref1.GetImage() - ref2.GetImage());
} //Operator Overload : *
ComplexC operator*(ComplexC& ref1, ComplexC& ref2)
{
return ComplexC(ref1.GetReal() * ref2.GetReal(), ref1.GetImage() * ref2.GetImage());
} //Operator Overload : /
ComplexC operator/(ComplexC& ref1, ComplexC& ref2)
{
return ComplexC(ref1.GetReal() / ref2.GetReal(), ref1.GetImage() / ref2.GetImage());
} int main(void)
{
ComplexA C1(,), C2(, ), C3;
C3 = C1 + C2; C3.display();
C3 = C1 - C2; C3.display();
C3 = C1 * C2; C3.display();
C3 = C1 / C2; C3.display();
cout <<"--------------------------------------"<<endl;
ComplexB C4(,), C5(, ), C6;
C6 = C4 + C5; C6.display();
C6 = C4 - C5; C6.display();
C6 = C4 * C5; C6.display();
C6 = C4 / C5; C6.display();
cout <<"--------------------------------------"<<endl;
ComplexC C7(,), C8(, ), C9;
C9 = C7 + C8; C9.display();
C9 = C7 - C8; C9.display();
C9 = C7 * C8; C9.display();
C9 = C7 / C8; C9.display();
return ;
}
3、应用举例(对象 ? 基本数据类型 or 基本数据类型 ? 对象)
上面的例子中是对象 和 对象之间的运算符重载,如果需要一个是对象 + char/int/float/double,或者反过来 char/int/float/double + 对象,这时上面的程序的重载方式就不适用了。
需要定义新的重载,如下列程序所示。
#include <iostream>
using namespace std; class ComplexD
{
public:
ComplexD(double re = , double im = ):real(re),image(im){} ComplexD operator+(ComplexD& ref){return ComplexD(real+ref.real, image+ref.image);};
ComplexD operator+(int a){cout<<"IN\t int \t\t";return ComplexD(real+a, image);};
ComplexD operator+(double d){cout<<"IN\t double \t";return ComplexD(real+d, image);};
ComplexD operator+(float f){cout<<"IN\t float \t\t";return ComplexD(real+f, image);}; void display(void){cout<<real<<"+"<<image<<"i"<<endl;}
double GetReal(void){return real;}
double GetImage(void){return image;}
private:
double real;
double image;
}; ComplexD operator+(int a, ComplexD& ref){cout<<"OUT\t int \t\t";return ComplexD(ref.GetReal()+a, ref.GetImage());};
ComplexD operator+(double d, ComplexD& ref){cout<<"OUT\t double \t";return ComplexD(ref.GetReal()+d, ref.GetImage());};
ComplexD operator+(float f, ComplexD& ref){cout<<"OUT\t float \t\t";return ComplexD(ref.GetReal()+f, ref.GetImage());}; int main(void)
{
ComplexD D1(,), D2;
D2 = D1 + ; D2.display();
D2 = D1 + 2.1f; D2.display();
D2 = D1 + 2.1; D2.display(); D2 = +D1; D2.display();
D2 = 2.1f + D1; D2.display();
D2 = 2.1 +D1; D2.display(); return ;
}
C++运算符重载——重载二元运算符的更多相关文章
- C++ 重载运算符和重载函数
C++ 重载运算符和重载函数 C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载. 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是 ...
- JavaScript一元运算符、二元运算符和三元运算符
在JavaScript中,运算符可以根据其实际操作数的个数进行分类. JavaScript中的大多数运算符是一个二元运算符(binary operator),将两个表达式合并成为一个稍复杂的表达式.譬 ...
- C++ 运算符重载一(二元运算符重载)
//二元运算符重载 #include<iostream> using namespace std; class Point { public: Point(int x,int y){ th ...
- C++之运算符重载(二元)
一.加号+ 1.成员函数重载 2.友元函数重载 二.输出符号<< 三.索引符号 [ ] 四.补充说明 1.<二元运算符重载>课程评论: (一)为什么<<运算符的重载 ...
- [置顶] C++基础之六:运算符的重载
网上太多有关运算符的重载了,但是写的太过的详细,不适合新手入门,特别是那什么++和--的前增量后增量重载,一元二元运算符重载,特殊运算符,下标运算符,new和delete,甚至是指针运算符的重载,吓退 ...
- C++解析七-重载运算符和重载函数
重载运算符和重载函数C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载.重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列 ...
- C++ 自增、自减运算符的重载和性能分析
01 ++.--运算符重载函数的格式 自增运算符和自减运算符是有前置和后置之分的,如: a++ // 后置自增运算符 ++a // 前置自增运算符 b-- // 后置自减运算符 --b // 前置自减 ...
- C++基础之自增和自减运算符的重载
1. 格式 1.1 分为前置和后置格式: int x = 0; int y = 0; // 后置自增运算符 x++; // 前置自增运算符 ++x; // 后置自减运算符 y--; // 前置自减运算 ...
- C++ Primer : : 第十四章 : 重载运算符与类型转换之类型转换运算符和重载匹配
类型转换运算符 class SmallInt { public: SmallInt(int i = 0) : val(i) { if (i < 0 || i > 255) throw st ...
随机推荐
- What is the difference between position: static,relative,absolute,fixed
What is the difference between static,relative, absolute,fixed we can refer to this link: expand
- 读:HIS 与医保系统的接入方案及实现
HIS 与医保系统的接入方案及实现刘剑锋 李刚荣第三军医大学西南医院信息科(重庆 400038) 医院HIS和医保系统的接口设计方案涉及两个部分,分别由医院和医保中心分别完成相,应的程序设计,这两部分 ...
- KinectStudio使用教程
在Kinect SDK 2.0安装结束之后,会有一个KinectStudio的调试工具,他可以将动作记录下,以后即便脱离了Kinect传感器也可以愉快的调试了.现在我们来看看如何使用 首先打开Kine ...
- 推荐一款系统软件:Unity tweak tool
功能很多慢慢体会 在软件中心搜索unity tweak tool安装
- 目前国内外主流的linux发行版本
1.linux其实是基于unix发展而来的,还有mac os也是类unix操作系统 2.目前主流的linux发行版本主要有:红帽系列(中国大陆,美洲地区,发源于美国),suse系列(欧洲地区流行,发源 ...
- 程序调试手段之gdb, vxworks shell
调试一个程序主要用到的功能: 启动程序 设置函数断点 设置数据断点 单步执行 查看内存值 修改内存值 linux下的gdb,和vxworks下的shell 虽然使用方式和调试命令略有不同,但是都能满足 ...
- Bootstrap入门五:表格
table样式: .table:表格基本样式,很少的padding,灰色的细水平分隔线. .table-striped:斑马纹样式,隔行换色. .table-bordered:为表格和其中的每个单元格 ...
- Ztree的初步使用--checkbox--指定目录下搜索子节点
这里记录一下zTree的check的使用 首先 <%@ Page Language="C#" AutoEventWireup="true" CodeBeh ...
- 17、android设备如何防止屏幕休眠(转载)
当你需要你的设备需要长期运行时,由于移动设备为了延长电池续航时间,在运行15s-30mins后(用户可自由设置),如果用户在此时间段内没有操作,系统将进入休眠状态并 将屏幕锁上,所以在需要长期运行时, ...
- python--str的几个方法
str.format() :对应取值 name="chenshan" age=30 address="宜山路926号新思大厦15楼" print " ...