C++语言基础(17)-运算符重载
运算符重载的格式为:
- 返回值类型 operator 运算符名称 (形参表列){
- //TODO:
- }
一.在类里面实例运行符重载
- #include <iostream>
- using namespace std;
- class complex{
- public:
- complex();
- complex(double real, double imag);
- public:
- //声明运算符重载
- complex operator+(const complex &A) const;
- void display() const;
- private:
- double m_real; //实部
- double m_imag; //虚部
- };
- complex::complex(): m_real(0.0), m_imag(0.0){ }
- complex::complex(double real, double imag): m_real(real), m_imag(imag){ }
- //实现运算符重载
- complex complex::operator+(const complex &A) const{
- complex B;
- B.m_real = this->m_real + A.m_real;
- B.m_imag = this->m_imag + A.m_imag;
- return B;
- }
- void complex::display() const{
- cout<<m_real<<" + "<<m_imag<<"i"<<endl;
- }
- int main(){
- complex c1(4.3, 5.8);
- complex c2(2.4, 3.7);
- complex c3;
- c3 = c1 + c2;
- c3.display();
- return ;
- }
运行结果:
6.7 + 9.5i
1.运算符重载其实就是定义一个函数,在函数体内实现想要的功能,当用到该运算符时,编译器会自动调用这个函数。也就是说,运算符重载是通过函数实现的,它本质上是函数重载。
2.operator
是关键字,专门用于定义重载运算符的函数。我们可以将operator 运算符名称
这一部分看做函数名,对于上面的代码,函数名就是operator+
。
3.上面的例子中,我们在 complex 类中重载了运算符+
,该重载只对 complex 对象有效。
二.在全局范围内重载运算符
运算符重载函数不仅可以作为类的成员函数,还可以作为全局函数。更改上面的代码,在全局范围内重载+
,实现复数的加法运算:
- #include <iostream>
- using namespace std;
- class complex{
- public:
- complex();
- complex(double real, double imag);
- public:
- void display() const;
- //声明为友元函数
- friend complex operator+(const complex &A, const complex &B);
- private:
- double m_real;
- double m_imag;
- };
- complex operator+(const complex &A, const complex &B);
- complex::complex(): m_real(0.0), m_imag(0.0){ }
- complex::complex(double real, double imag): m_real(real), m_imag(imag){ }
- void complex::display() const{
- cout<<m_real<<" + "<<m_imag<<"i"<<endl;
- }
- //在全局范围内重载+
- complex operator+(const complex &A, const complex &B){
- complex C;
- C.m_real = A.m_real + B.m_real;
- C.m_imag = A.m_imag + B.m_imag;
- return C;
- }
- int main(){
- complex c1(4.3, 5.8);
- complex c2(2.4, 3.7);
- complex c3;
- c3 = c1 + c2;
- c3.display();
- return ;
- }
运算符重载函数不是 complex 类的成员函数,但是却用到了 complex 类的 private 成员变量,所以必须在 complex 类中将该函数声明为友元函数。
当执行c3 = c1 + c2;
语句时,编译器检测到+
号两边都是 complex 对象,就会转换为类似下面的函数调用:
- c3 = operator+(c1, c2);
三.重载数学运算符(+-*/)
- #include <iostream>
- #include <cmath>
- using namespace std;
- //复数类
- class Complex{
- public: //构造函数
- Complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ }
- public: //运算符重载
- //以全局函数的形式重载
- friend Complex operator+(const Complex &c1, const Complex &c2);
- friend Complex operator-(const Complex &c1, const Complex &c2);
- friend Complex operator*(const Complex &c1, const Complex &c2);
- friend Complex operator/(const Complex &c1, const Complex &c2);
- friend bool operator==(const Complex &c1, const Complex &c2);
- friend bool operator!=(const Complex &c1, const Complex &c2);
- //以成员函数的形式重载
- Complex & operator+=(const Complex &c);
- Complex & operator-=(const Complex &c);
- Complex & operator*=(const Complex &c);
- Complex & operator/=(const Complex &c);
- public: //成员函数
- double real() const{ return m_real; }
- double imag() const{ return m_imag; }
- private:
- double m_real; //实部
- double m_imag; //虚部
- };
- //重载+运算符
- Complex operator+(const Complex &c1, const Complex &c2){
- Complex c;
- c.m_real = c1.m_real + c2.m_real;
- c.m_imag = c1.m_imag + c2.m_imag;
- return c;
- }
- //重载-运算符
- Complex operator-(const Complex &c1, const Complex &c2){
- Complex c;
- c.m_real = c1.m_real - c2.m_real;
- c.m_imag = c1.m_imag - c2.m_imag;
- return c;
- }
- //重载*运算符 (a+bi) * (c+di) = (ac-bd) + (bc+ad)i
- Complex operator*(const Complex &c1, const Complex &c2){
- Complex c;
- c.m_real = c1.m_real * c2.m_real - c1.m_imag * c2.m_imag;
- c.m_imag = c1.m_imag * c2.m_real + c1.m_real * c2.m_imag;
- return c;
- }
- //重载/运算符 (a+bi) / (c+di) = [(ac+bd) / (c²+d²)] + [(bc-ad) / (c²+d²)]i
- Complex operator/(const Complex &c1, const Complex &c2){
- Complex c;
- c.m_real = (c1.m_real*c2.m_real + c1.m_imag*c2.m_imag) / (pow(c2.m_real, ) + pow(c2.m_imag, ));
- c.m_imag = (c1.m_imag*c2.m_real - c1.m_real*c2.m_imag) / (pow(c2.m_real, ) + pow(c2.m_imag, ));
- return c;
- }
- //重载==运算符
- bool operator==(const Complex &c1, const Complex &c2){
- if( c1.m_real == c2.m_real && c1.m_imag == c2.m_imag ){
- return true;
- }else{
- return false;
- }
- }
- //重载!=运算符
- bool operator!=(const Complex &c1, const Complex &c2){
- if( c1.m_real != c2.m_real || c1.m_imag != c2.m_imag ){
- return true;
- }else{
- return false;
- }
- }
- //重载+=运算符
- Complex & Complex::operator+=(const Complex &c){
- this->m_real += c.m_real;
- this->m_imag += c.m_imag;
- return *this;
- }
- //重载-=运算符
- Complex & Complex::operator-=(const Complex &c){
- this->m_real -= c.m_real;
- this->m_imag -= c.m_imag;
- return *this;
- }
- //重载*=运算符
- Complex & Complex::operator*=(const Complex &c){
- this->m_real = this->m_real * c.m_real - this->m_imag * c.m_imag;
- this->m_imag = this->m_imag * c.m_real + this->m_real * c.m_imag;
- return *this;
- }
- //重载/=运算符
- Complex & Complex::operator/=(const Complex &c){
- this->m_real = (this->m_real*c.m_real + this->m_imag*c.m_imag) / (pow(c.m_real, ) + pow(c.m_imag, ));
- this->m_imag = (this->m_imag*c.m_real - this->m_real*c.m_imag) / (pow(c.m_real, ) + pow(c.m_imag, ));
- return *this;
- }
- int main(){
- Complex c1(, );
- Complex c2(, );
- Complex c3(, );
- Complex c4(, );
- Complex c5(, );
- Complex c6(, );
- Complex c7 = c1 + c2;
- Complex c8 = c1 - c2;
- Complex c9 = c1 * c2;
- Complex c10 = c1 / c2;
- cout<<"c7 = "<<c7.real()<<" + "<<c7.imag()<<"i"<<endl;
- cout<<"c8 = "<<c8.real()<<" + "<<c8.imag()<<"i"<<endl;
- cout<<"c9 = "<<c9.real()<<" + "<<c9.imag()<<"i"<<endl;
- cout<<"c10 = "<<c10.real()<<" + "<<c10.imag()<<"i"<<endl;
- c3 += c1;
- c4 -= c2;
- c5 *= c2;
- c6 /= c2;
- cout<<"c3 = "<<c3.real()<<" + "<<c3.imag()<<"i"<<endl;
- cout<<"c4 = "<<c4.real()<<" + "<<c4.imag()<<"i"<<endl;
- cout<<"c5 = "<<c5.real()<<" + "<<c5.imag()<<"i"<<endl;
- cout<<"c6 = "<<c6.real()<<" + "<<c6.imag()<<"i"<<endl;
- if(c1 == c2){
- cout<<"c1 == c2"<<endl;
- }
- if(c1 != c2){
- cout<<"c1 != c2"<<endl;
- }
- return ;
- }
运行结果:
c7 = 35 + 55i
c8 = 15 + 15i
c9 = -450 + 850i
c10 = 1.9 + -0.3i
c3 = 26 + 37i
c4 = -6 + -11i
c5 = 220 + 4460i
c6 = 5.2 + 1.592i
c1 != c2
四.重载下标运算符[]
- #include <iostream>
- using namespace std;
- class Array{
- public:
- Array(int length = );
- ~Array();
- public:
- int & operator[](int i);
- const int & operator[](int i) const;
- public:
- int length() const { return m_length; }
- void display() const;
- private:
- int m_length; //数组长度
- int *m_p; //指向数组内存的指针
- };
- Array::Array(int length): m_length(length){
- if(length == ){
- m_p = NULL;
- }else{
- m_p = new int[length];
- }
- }
- Array::~Array(){
- delete[] m_p;
- }
- int& Array::operator[](int i){
- return m_p[i];
- }
- const int & Array::operator[](int i) const{
- return m_p[i];
- }
- void Array::display() const{
- for(int i = ; i < m_length; i++){
- if(i == m_length - ){
- cout<<m_p[i]<<endl;
- }else{
- cout<<m_p[i]<<", ";
- }
- }
- }
- int main(){
- int n;
- cin>>n;
- Array A(n);
- for(int i = , len = A.length(); i < len; i++){
- A[i] = i * ;
- }
- A.display();
- const Array B(n);
- cout<<B[n-]<<endl; //访问最后一个元素
- return ;
- }
运行结果:
0, 5, 10, 15, 20
C++语言基础(17)-运算符重载的更多相关文章
- C语言基础知识-运算符与表达式
C语言基础知识-运算符与表达式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用运算符分类 1>.算术运算符 用于处理四则运算. 2>.赋值运算符 用于将表达式的 ...
- GO学习-(6) Go语言基础之运算符
Go语言基础之运算符 运算符用于在程序运行时执行数学或逻辑运算. 运算符 Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 算数运算符 运算符 描述 + 相加 - ...
- C++_基础_运算符重载2
内容: (1)只能用成员形式重载的运算符 (2)new/delete操作符的重载 (3)封装和继承的初识 (4)继承的特性 (5)子类及其函数的特性 (6)多重继承和虚继承 1.只能用成员形式重载的运 ...
- C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)
运算符重载 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.重载的运算符是遵循函数重载的选择原则,根据不同类型或不同参数来选择不同的重载运算符. 运 ...
- C#-语言基础+数据类型+运算符
一.C#语言基础 新建项目:文件→新建→项目→Visual C#(默认.NET Framework 4.5)→控制台应用程序 1.项目结构 (1)项目后缀 .config ——配置文件(存放配置参数文 ...
- C语言基础学习运算符-赋值运算符
简单赋值 在C语言里,运算符=并不表示相等,而是一个赋值运算符.这意味着,符号=的左边该是一个可修改的变量名,而右边是赋给该变量的值. 如下程序语句: i = i+; 在数学上,该等式无法成立.而作为 ...
- C++_基础_运算符重载
内容: (1)输入输出运算符重载 (2)友元类和友元函数 (3)双目运算符重载 (4)单目运算符重载 (5)不能被重载的运算符 (6)只能定义为成员形式的运算符 1.输入输出运算符重载如: int n ...
- [Python学习笔记1]Python语言基础 数学运算符 字符串 列表
这个系列是我在学习Python语言的过程中记录的笔记,主要是一些知识点汇总,而非学习教程,可供有一定编程基础者参考.文中偏见和不足难以避免,仅供参考,欢迎批评指正. 本系列笔记主要参考文献是官网文档: ...
- C语言基础学习运算符-基本算术运算符
C语言中用于基本算术运算的运算符有:+,-,*,%,/.这些运算符的用法和你想像到的基本无异: 加法运算符 “+”使得它两侧的值被加到一起. 减法运算符“-”用它前面的数减去后面的数. 乘法由“*”表 ...
随机推荐
- 【推导】Codeforces Round #402 (Div. 2) A. Pupils Redistribution
一次交换,会让Group A里面的某个数字的数量-1,另一个数字的数量+1:对Group B恰好相反. 于是答案就是xigma(i=1~5,numA[i]-numB[i]>0)(numA[i]- ...
- 【最近公共祖先】【块状树】CODEVS 1036 商务旅行
在线块状树LCA模板. #include<cstdio> #include<vector> #include<algorithm> #include<cmat ...
- 【动态规划】【零一背包】CODEVS 1014 装箱问题 2001年NOIP全国联赛普及组
#include<cstdio> #include<algorithm> using namespace std; ],f[]; int main() { scanf(&quo ...
- Nginx下载防盗链(迅雷等下载软件)
什么是下载盗链 假设我们是一个B站,有些视频资源是可以提供给用户下载的.这时迅雷等其他下载软件,也提供下载该视频的服务, 但是迅雷很不厚道的,将我们的下载资源提供给他的用户,下载.占用我们的带宽来 ...
- [Java基础] 使用JMAP dump及分析dump文件
转载:http://blog.csdn.net/kevin_luan/article/details/8447896 http://liulinxia02.blog.163.com/blog/stat ...
- Kubernetes环境下调整WebLogic JVM基本参数
基于Kubernetes启动WebLogic后,发现JVM的最大heap size一直在700多M左右,通过 kubectl logs 察看pod启动状态,发现日志中并没有-Xms和-Xmx参数.日志 ...
- js基础的思维导图
纯粹转载下,我可画不出这么好看的思维导图,这得感谢@ChokCoco javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript ...
- IP windows相关
nbtstat: 假设 我们 通过net view 获取了 局域网内一些计算机名 如上标红的计算机名称 如何 才能获取计算机的ip呢? 接下来使用 nbtstat -a 列出远程机器的名称表: ...
- Linux C 面试题总结
1.进程和线程的区别,及优劣性比较 进程和线程的主要差别在于它们是不同的操作系统资源管理方式.进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路 ...
- Java平时需要注意的事项
1.String 相等 稍微有点经验的程序员都会用equals比较而不是用 ==,但用equals就真的安全了吗,看下面的代码 user.getName().equals("xiaoming ...