C++(三十五) — 运算符重载
运算符重载的实质:函数重载。除了增加一个关键字 operator 外,与函数重载没有区别,都是通过该类的某个对象来访问重载运算符。
(1)重载运算符时,运算符运算顺序和优先级不变,操作数个数不变;
(2)不能创造新的运算符;
(3)形式:重载为类的成员函数。对象本身就是其中的一个操作数,所以形参表内参数的数目比操作数的数目少一个。
重载为类的友元函数。一元运算符有一个形参;二元运算符有两个形参;每个形参代表运算符的一个操作数。
1、友元函数和成员函数的应用举例,一般情况下应用成员函数。
#include <iostream>
using namespace std; class Complex
{
public:
Complex(int a = , int b = )
{
this->a = a;
this->b = b;
}
void printCom()
{
cout << a << " + " << b << "i" << endl;
}
public:
// 友元函数实现,必须和实现的完全一样
friend Complex operator+(Complex &c1, Complex &c2);
friend Complex& operator++(Complex &c1);
friend Complex operator++(Complex &c1, int);
friend ostream& operator<<(ostream &out, Complex &c1);
//friend void operator<<(ostream &out, Complex &c1); //成员函数法实现 - 号运算符重载
Complex operator-(Complex &c2)
{
Complex tmp(a - c2.a, b - c2.b);
return tmp;
}
//成员函数实现 前置-- 操作
Complex& operator--()
{
a--; b--;
return *this;
}
//成员函数实现 后置-- 操作,与 前置-- 使用占位符进行区别。
Complex operator--(int)
{
Complex tmp = *this;
a--; b--;
return tmp;
}
private:
int a, b;
}; //友元函数实现 + 号运算符重载
Complex operator+(Complex &c1, Complex &c2)
{
Complex tmp(c1.a + c2.a, c1.b + c2.b);
return tmp;
}
// 友元函数实现 一元运算符 重载,前置++
Complex& operator++(Complex &c1)
{
c1.a++;
c1.b++;
return c1;
}
// 友元函数实现 一元运算符 重载,后置++
Complex operator++(Complex &c1, int)
{
Complex tmp = c1;
c1.a++;
c1.b++;
return tmp;
} void main()
{
Complex c1(, ), c2(, );
//1、全局函数实现,对于私有成员使用了友元函数。
Complex c3 = c1 + c2;
c3.printCom();
//2、成员函数实现
Complex c4 = c1 - c2;
c4.printCom(); //3、全局函数实现,前置++
++c1;
c1.printCom();
//4、成员函数实现,前置--
--c1;
c1.printCom(); //5、全局函数实现,后置++
Complex c5 = c1++;
c5.printCom();
c1.printCom();
//6、成员函数实现,后置--
Complex c6 = c1--;
c6.printCom();
c1.printCom(); system("pause");
}
2、友元函数的应用场景,左移右移操作符的重载。
//只能使用友元函数实现左移、右移操作运算符
/*oid operator<<(ostream &out, Complex &c1)
{
out << c1.a << " + " << c1.b << "i" << endl;
}*/
//只能使用友元函数实现左移、右移操作运算符
ostream& operator<<(ostream &out, Complex &c1)
{
out << c1.a << " + " << c1.b << "i" << endl;
return out;
} void main()
{
int a = ;
Complex c1(, ), c2(, );
//cout << c1;
cout << c1 << "abv" << endl;
system("pause");
}
C++(三十五) — 运算符重载的更多相关文章
- JAVA之旅(三十五)——完结篇,终于把JAVA写完了,真感概呐!
JAVA之旅(三十五)--完结篇,终于把JAVA写完了,真感概呐! 这篇博文只是用来水经验的,写这个系列是因为我自己的java本身也不是特别好,所以重温了一下,但是手比较痒于是就写出了这三十多篇博客了 ...
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课
centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件.目录属性 shell数组简单用法 $( ) 和$ ...
- NeHe OpenGL教程 第三十五课:播放AVI
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Java进阶(三十五)java int与integer的区别
Java进阶(三十五)java int与Integer的区别 前言 int与Integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而Integer是对象 ...
- Gradle 1.12用户指南翻译——第三十五章. Sonar 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- SQL注入之Sqli-labs系列第三十四关(基于宽字符逃逸POST注入)和三十五关
开始挑战第三十四关和第三十五关(Bypass add addslashes) 0x1查看源码 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理. if(isset($_ ...
- “全栈2019”Java多线程第三十五章:如何获取线程被等待的时间?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- Python进阶(三十五)-Fiddler命令行和HTTP断点调试
Python进阶(三十五)-Fiddler命令行和HTTP断点调试 一. Fiddler内置命令 上一节(使用Fiddler进行抓包分析)中,介绍到,在web session(与我们通常所说的se ...
- 第三百三十五节,web爬虫讲解2—Scrapy框架爬虫—豆瓣登录与利用打码接口实现自动识别验证码
第三百三十五节,web爬虫讲解2—Scrapy框架爬虫—豆瓣登录与利用打码接口实现自动识别验证码 打码接口文件 # -*- coding: cp936 -*- import sys import os ...
- sdut 面向对象程序设计上机练习十(运算符重载)
面向对象程序设计上机练习十(运算符重载) Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 定义一个复数类Complex,重载运算符"+" ...
随机推荐
- SAP 更新模块1
RSM13000 / RSM13000 / 5.747FORM / VB_CALL_FUNC CALL 'ThVBCall' ID 'OPCODE' FIELD vb_update_modul_pro ...
- [LeetCode] 261. Graph Valid Tree 图是否是树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] 369. Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- [LeetCode] 529. Minesweeper 扫雷
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- 端口占用问题:java.net.BindException: Address already in use: bind
解决方法 方法一:换一个端口 若仍然想要使用该端口,则可以将占用该端口的进程杀死即可. 方法二:杀死占用该端口的进程 若仍然想要使用该端口,则可以将占用该端口的进程杀死即可 查找端口被占用的进程id ...
- ORACLE链接SQLSERVER数据库数据操作函数范例
ORACLE链接SQLSERVER数据库数据操作函数范例 create or replace function FUN_NAME(LS_DJBH IN varchar2 ,LS_ITM varchar ...
- [高清·非影印] 深度学习入门:基于Python的理论与实现 + 源代码
------ 郑重声明 --------- 资源来自网络,纯粹共享交流, 如果喜欢,请您务必支持正版!! --------------------------------------------- 下 ...
- 解决springboot 新版本 2.1.6 spring-boot-starter-actuator 访问报404
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- python ID3决策树实现
环境:ubuntu 16.04 python 3.6 数据来源:UCI wine_data(比较经典的酒数据) 决策树要点: 1. 如何确定分裂点(CART ID3 C4.5算法有着对应的分裂计算方式 ...
- Linux 系统中如何进入退出 vim 编辑器
在 Linux 中,vim 编辑器是系统自带的文本编辑器,但要修改某个文本文件,可不是像 Windows 那样操作,更有新手,进入 vi 编辑器后,无法退出以致于强制关机,其实,这个vim(vi)也是 ...