运算符重载,完成自定义类型的运算

来源:互联网 发布:华为海洋网络 面试 编辑:程序博客网 时间:2024/06/03 17:49


//其本质就是运算符重载  operator+


#include "stdafx.h"#include <vector>#include <algorithm>#include <functional>using namespace std;class complex{public:float real;float imag;public:complex()  //默认构造函数{real=0;imag=0;}//双参数构造函数complex(float re,float im){real=re;imag=im;}//运算符+重载complex operator+(const complex & c)const{complex v;v.real=real+c.real;v.imag=imag+c.imag;return v;}};int _tmain(int argc, _TCHAR* argv[]){//--------------------------------------------------------------//自定义类型运算         complex c1(1.0,2.0);complex c2(3.0,4.0);complex c3=c1+c2;getchar();return 0;}


0 0
原创粉丝点击