第二节,重载运算符
来源:互联网 发布:学党史知国史征文300字 编辑:程序博客网 时间:2024/06/14 00:31
所谓重载运算符即是扩展你要重载的那个运算符的功能,它就相当于objective-C中得类目一样,你不明白类目得话,也可以这样去理解:比如咱们要扩展一个“+”的功能,在没有扩展的情况下,他的作用就是将基本数据类型的进行相加。我们想要两个复数相加,那么就不能用“+”,这时我们就需要扩展“+”,让他也能让两个复数相加,下面我们就通过一个简单的例子---复数相加,来学习一下运算符的重载。
首先上代码:
<!----------------------h文件--------------------->
#ifndef _________Complex__#define _________Complex__#include <iostream>class Complex{public: double real;//实部 double viurter;//虚部 public: Complex(double aReal=0,double aViuter=0) { this->real = aReal; this->viurter = aViuter; } ~Complex() { std::cout<<"销毁"<<std::endl; } }; Complex operator+(Complex one,Complex two);#endif /* defined(_________Complex__) */
<!----------------------h文件--------------------->
<!----------------------cpp文件--------------------->
#include "Complex.h"Complex operator+(Complex one,Complex two){ return Complex(one.real+two.real,one.viurter+two.viurter);}<!----------------------cpp文件--------------------->
<!----------------------调用--------------------->
#include <iostream>#include "Complex.h"using namespace std;int main(int argc, const char * argv[]){ Complex com1(2,3),com2(5,6),sum; // sum = com1+com2; sum = operator+(com1,com2); cout<<"sum.real = "<<sum.real<<std::endl; return 0;} <!----------------------调用---------------------->1,形式:返回类型 operator重写的符号(参数)2,调用的时候两种方式都可以 sum = com1+com2; 或者 sum = operator+(com1,com2);
- 第二节,重载运算符
- 王老师 C++ 运算符重载 转换函数 第二讲
- 第二周 周赛新知(运算符重载)
- 定义一个复数类Complex 重载运算符 第二题
- 第12周第二项目——Time类的运算(运算符重载)
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 第2.6节:运算符重载(C++)
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- C语言 算术转换 unsigned signed int
- 一网打尽当下NoSQL类型、适用场景及使用公司
- RESTFul介绍
- 【修改PHP上传文件大小限制】
- Win7系统中Telnet服务的安装和启动
- 第二节,重载运算符
- 浅析PageRank算法
- 在QC里,搜索DefectID
- 用xfire快速发布WebService接口
- JAVA annotation入门
- 用php把图片编码嵌入到html
- java的面向对象
- Set RCP Application's default perspective
- POJ 1001