c++中运算符重载,+,-,--,+=,-=,*,/,*=,/=,
#include<iostream>
#include<stdlib.h>
using namespace std;
class Complex
{
public:
Complex(float r=0,float i =0):_r(r),_i(i)
{
}
void print() const
{
cout<<" _r "<<_r<<" _i "<<_i<<endl;
}
private:
float _r;
float _i;
public:
const Complex& operator+=(const Complex & y);
const Complex& operator-=(const Complex & y);
const Complex& operator*=(const Complex & y);
const Complex& operator/=(const Complex & y);
const Complex operator-(const Complex & y);
const Complex operator+(const Complex & y);
const Complex Complex::operator--();
const bool operator==(const Complex & y);
const bool operator!=(const Complex & y);
};
inline const Complex Complex::operator-(const Complex & y)
{
Complex c;
c._r=_r-y._r;
c._i=_i-y._i;
return c;
}
inline const Complex Complex::operator--()
{
_r=_r-1;
_i=_i-1;
return *this;
}
inline const bool Complex::operator==(const Complex & y)
{
bool b=true;
if((*this)._r!=y._r||(*this)._i!=y._i)
b=false; return b;
}
inline const bool Complex::operator!=(const Complex & y)
{
bool b=true;
if((*this)._r==y._r&&(*this)._i==y._i)
b=false; return b; }
inline const Complex Complex::operator+(const Complex & y)
{
Complex c;
c._r=_r+y._r;
c._i=_i+y._i;
return c;
}
inline const Complex& Complex::operator+=(const Complex & y)
{
_r=_r+y._r;
_i=_i+y._i;
return *this;
}
inline const Complex& Complex::operator-=(const Complex & y)
{
*this=*this-y;
return *this;
}
inline const Complex& Complex::operator*=(const Complex & y)
{
_r=_r*y._r-_i*y._i;
_i=_r*y._i+_i*y._r;
return *this;
}
inline const Complex& Complex::operator/=(const Complex & y)
{
if(y._r==0 && y._i==0)
{
exit(1);
}
float den=_r*y._r+_i*y._i;
_r=(_r*y._r+_i*y._i)/den;
_i=(_i*y._r-_r*y._i)/den;
return *this;
}
int main()
{
Complex x(2,3),y(-1,3);
cout<<" x is ";
x.print();
cout<<" y is ";
y.print(); (x+=y).print();
x.operator+=(y).print();
(x-=y).print();
x.operator-=(y).print();
(x*=y).print();
x.operator*=(y).print();
(x/=y).print();
x.operator/=(y).print(); cout<<(x==y)<<endl;
cout<<(x!=y)<<endl;
return 0; }
c++中运算符重载,+,-,--,+=,-=,*,/,*=,/=,的更多相关文章
- c++中运算符重载
c++语言中运算符重载都是通过函数来实现的,所以其实质为函数重载,当c++语言原有的一个运算符被重载之后,它原来所具有的语义并没有消失,只相当于针对一个特定的类定义了一个新的运算符. <1> ...
- string类中运算符重载实现
C++中预定义的加.减等运算符的操作对象只能是基本的数据类型.如果要在用户自定义的类型对象上应用同样的运算符,就需要通过运算符重载来重新定义其实现,使它能够用于自定义类型执行特定的操作,所以运算符重载 ...
- 08 c++中运算符重载(未完成)
参考:轻松搞定c++语言 定义:赋予已有运算符多重含义,实现一名多用(比较函数重载) 运算符重载的本质是函数重载 重载函数的格式: 函数类型 operator 运算符名称(形参表列) { 重载实体 ...
- 问题 B: 矩形类中运算符重载【C++】
题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数.输入坐标的函数,实现矩形加法,以及计算并输出矩形面积的函数.要求使用提示中给出的测试函数并不得改动. 两个矩 ...
- Python 中的运算符重载
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 一种运算符对于不同类型的对象,有不同的使用方式.例如, + 用于整型对象,表示两个数相加:用于字符串 ...
- C++之------运算符重载
① 什么是运算符重载? 何为C++的运算符重载呢? 其实就是运算符给它重新赋予新的含义或者多重含义.让它有另外一种新的功能. 为什么需要运算符重载? 面向对象中为了实现类的多态性,我们就引用了运算符 ...
- 雷林鹏分享:C# 运算符重载
C# 运算符重载 您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的.与其 ...
- C++运算符重载的妙用
运算符重载(Operator overloading)是C++重要特性之中的一个,本文通过列举标准库中的运算符重载实例,展示运算符重载在C++里的妙用.详细包含重载operator<<,o ...
- 《挑战30天C++入门极限》C++运算符重载函数基础及其值返回状态
C++运算符重载函数基础及其值返回状态 运算符重载是C++的重要组成部分,它可以让程序更加的简单易懂,简单的运算符使用可以使复杂函数的理解更直观. 对于普通对象来说我们很自然的会频繁使用算数运 ...
随机推荐
- Makefile学习与进阶之Makefile.am和$$(M)的意思
(1)makefile 中,出现$$(M) 是什么意思,发现还是看实际的Makefile长知识啊 在makefile中,会经常使用shell命令,也经常见到$var 和 $$var的情况,有什么区别呢 ...
- 对Map按key和value分别排序
一.理论准备 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红 ...
- Hadoop文件系统常用命令
1.查看指定目录下内容 hadoop dfs –ls [文件目录] eg: hadoop dfs –ls /user/wangkai.pt 2.打开某个已存在文件 hadoop dfs –cat [f ...
- python PIL下的各种问题
为了实现验证码的功能,使用了PIL.结果出现各种问题: 先是"ImportError: The _imagingft C module is not installed",goog ...
- Nullable问题
在代码中经常遇到int? 其实int?是Nullable<int>的简写方式. int到int?转化 int? i=null; int j=0; i=j; int?到int转化 ; int ...
- PopupWindow 问题集锦
1.响应返回键/响应键盘事件(onKeyListener) 最近在做PopupWindow, 发现使用PopupWindow一出现,不会响应popup外面的事件,经过资料查找,发现有两种方法可以响应外 ...
- UIBezierPath精讲
前言 笔者在写本篇文章之前,也没有系统学习过贝塞尔曲线,只是曾经某一次的需求需要使用到,才临时百度看了一看而且使用最基本的功能.现在总算有时间停下来好好研究研究这个神奇而伟大的贝塞尔先生! 笔者在学习 ...
- 在高版本SDK中打开现存低版本SDK工程
直接打开低版本SDK工程会出现错误提示:“Unable to resolve target 'android-xx” 解决方法: 1.将project.properties文件中的“target=an ...
- String的那点小事儿
1.== 比较的是什么? 1 package xupengwei.string; 2 /** 3 * @describe: 4 * @author chenmo-xpw 5 * @version 20 ...
- MySQL5日期类型DATETIME和TIMESTAMP相关问题详解
MySQL5日期类型DATETIME和TIMESTAMP相关问题详解 MySQL5的日期类型有三种:DATETIME.DATE和TIMESTAMP,除了DATE用来表示一个不带时分秒的是日期,另外两个 ...