c++的运算符重载
来源:互联网 发布:沼泽人 知乎 编辑:程序博客网 时间:2024/06/07 22:06
C++中的加号重载:
如何实现复数的相加:
#include<iostream>using namespace std;class Complex{ public: Complex(double r=0.0,double i=0.0):real(r),imag(i){} Complex operator+(const Complex &c2)const; void display()const{ cout<<"("<<real<<","<<imag<<')'<<endl; } private: double real,imag;};Complex Complex::operator+(const Complex &c2)const{ return Complex(real+c2.real,imag-c2.imag);}int main(){ Complex c1(5,4),c2(2,10),c3; c3=c1+c2; c3.display(); return 0;}C++中的前置++重载:
点的移动:
#include<iostream>using namespace std;class Point{ public: Point(float a,float b):x(a),y(b){} Point& operator++(); ~Point(){} void dispaly()const{ cout<<"("<<x<<","<<y<<")"<<endl; } private: float x,y;};Point& Point::operator++(){ x++;y++; return *this;}int main(){ Point p(1,2); (++p).dispaly();p.dispaly(); return 0;}C++中的后置++重载:
点的移动:
#include<iostream>using namespace std;class Point{ public: Point(float a,float b):x(a),y(b){} Point operator++(int); ~Point(){} void dispaly()const{ cout<<"("<<x<<","<<y<<")"<<endl; } private: float x,y;};Point Point::operator++(int){ Point p=*this; x++;y++; return p;}int main(){ Point p(1,2); (p++).dispaly(); p.dispaly(); return 0;}
阅读全文
1 0
- c++-运算符的重载
- C++--运算符的重载
- [C/C++]运算符的重载
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- C#:运算符重载
- [C++]运算符重载
- 【c++】运算符重载
- java 中finalize()的使用
- iOS开发
- 运动目标检测
- Drawable学习之---BitmapDrawable
- flowable中的processEngineConfiguration配置
- c++的运算符重载
- javascript设计模式初探--单例模式
- AS 在小米手机调试错误 Failed to establish session Error while Installing APKs
- Ubuntu16.04 安装C++ boost库
- STL源码剖析读书笔记5
- html页面缓存资源
- 高斯滤波 模板生成原理 matlab代码
- 关于代码控制unity中动画播放的问题
- C++ map用法整理