运算符重载和普通类

来源:互联网 发布:android上传图片到php 编辑:程序博客网 时间:2024/06/08 08:10
#include<iostream>using namespace std;class CComplex{public:CComplex(int r = 10, int i = 10) :mreal(r), mimage(i){ ; }CComplex& operator=(const CComplex&obj){return *this;}CComplex operator+(const CComplex&obj){return CComplex(mreal+obj.mreal,mimage+obj.mimage);}void  operator +=(const CComplex&obj){mreal += obj.mreal;mimage += obj.mimage;}CComplex operator ++(int)//后置++重载{return CComplex(mreal++, mimage++);}CComplex&  operator ++()//前置++重载{mreal ++;mimage ++;return *this;}operator char*()//从类类型到char*类型的重载,用得比较少{return "sasfas";}friendostream &operator<<(ostream&os,CComplex& obj)//这个类的全局友元函数,与声明定义的位置无关,依然是全局的函数{os << obj.mreal << "  " << obj.mimage << endl;return os;}friendistream &operator>>(istream&os, CComplex& obj)//这个类的全局友元函数,与声明定义的位置无关,依然是全局的函数{os >> obj.mreal >> obj.mimage;return os;}private:int mreal;int mimage;};/*other ==>类类型   类(other)的构造函数类类型==>other    operator other()类型重载优先调用成员方法,找不到就去调用全局的重载+法函数全局的重载方法要调用类的私有的变量需要声明为友元函数*/