运算符重载和普通类
来源:互联网 发布: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()类型重载优先调用成员方法,找不到就去调用全局的重载+法函数全局的重载方法要调用类的私有的变量需要声明为友元函数*/
阅读全文
0 0
- 运算符重载和普通类
- 运算符重载(重载为普通函数)
- 重载运算符 ++ 和 --
- 抽象和类,this,运算符重载
- OJ积累--运算符重载+(普通函数)
- C#运算符重载和方法重载
- 【C++】重载运算符和重载函数
- C++ 重载运算符和重载函数
- C++ 重载运算符和重载函数
- 类运算符重载
- 运算符重载和字符串
- 不要重载&&和||运算符
- []运算符的重载和二维数组类的[][]运算符重载
- 运算符重载——重载+和-对复数类对象进行加减运算
- 可重载运算符和不可重载运算符
- c++ 可重载运算符和不可重载运算符
- c++ 可重载运算符和不可重载运算符
- 重载双目运算符和重载单目运算符
- 2017-12-9
- ACM模版记录
- Python实现Pat 1049. Counting Ones (30)
- 三序遍历相互求法
- 图像基本知识
- 运算符重载和普通类
- HR8825是一种内置步进表的集成微步进电机驱动器,兼容DRV8825
- 服务器部署遇到的问题
- VHDL实现4线-16线译码器
- 使用现代C++编写的读写json的库
- Unit文件详解
- angular(查询、删除、添加、排序)案例
- HR4988是一种便于使用的内部集成了译码器的微步进电机驱动器,单脉冲控制步进电机,节省MCU I/0的驱动IC,兼容A4988 的产品
- HDOJ1406 完数