#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++中运算符重载,+,-,--,+=,-=,*,/,*=,/=,的更多相关文章

  1. c++中运算符重载

    c++语言中运算符重载都是通过函数来实现的,所以其实质为函数重载,当c++语言原有的一个运算符被重载之后,它原来所具有的语义并没有消失,只相当于针对一个特定的类定义了一个新的运算符. <1> ...

  2. string类中运算符重载实现

    C++中预定义的加.减等运算符的操作对象只能是基本的数据类型.如果要在用户自定义的类型对象上应用同样的运算符,就需要通过运算符重载来重新定义其实现,使它能够用于自定义类型执行特定的操作,所以运算符重载 ...

  3. 08 c++中运算符重载(未完成)

    参考:轻松搞定c++语言 定义:赋予已有运算符多重含义,实现一名多用(比较函数重载) 运算符重载的本质是函数重载 重载函数的格式: 函数类型 operator 运算符名称(形参表列)  {  重载实体 ...

  4. 问题 B: 矩形类中运算符重载【C++】

    题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数.输入坐标的函数,实现矩形加法,以及计算并输出矩形面积的函数.要求使用提示中给出的测试函数并不得改动. 两个矩 ...

  5. Python 中的运算符重载

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 一种运算符对于不同类型的对象,有不同的使用方式.例如, + 用于整型对象,表示两个数相加:用于字符串 ...

  6. C++之------运算符重载

    ①  什么是运算符重载? 何为C++的运算符重载呢? 其实就是运算符给它重新赋予新的含义或者多重含义.让它有另外一种新的功能. 为什么需要运算符重载? 面向对象中为了实现类的多态性,我们就引用了运算符 ...

  7. 雷林鹏分享:C# 运算符重载

    C# 运算符重载 您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的.与其 ...

  8. C++运算符重载的妙用

    运算符重载(Operator overloading)是C++重要特性之中的一个,本文通过列举标准库中的运算符重载实例,展示运算符重载在C++里的妙用.详细包含重载operator<<,o ...

  9. 《挑战30天C++入门极限》C++运算符重载函数基础及其值返回状态

        C++运算符重载函数基础及其值返回状态 运算符重载是C++的重要组成部分,它可以让程序更加的简单易懂,简单的运算符使用可以使复杂函数的理解更直观. 对于普通对象来说我们很自然的会频繁使用算数运 ...

随机推荐

  1. MAC虚拟机NAT方式共享上网设置

    有部分FY需要,我写一下我的方法吧,当初安装完MAC后,无法上网,网络搜索用的是HOST-only方法,试了几次都没有成功,后来尝试NAT方法,发现很简单. 我的主机系统:win7 64位,自动获取I ...

  2. e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (一) 京东 商品搜索

    之前有发布一篇文章“e2e 自动化集成测试 环境搭建 Node.js Selenium WebDriverIO Mocha Node-Inspector”, 主要是讲了,如何搭建环境, 其中开发环境使 ...

  3. 魅族MX2代理设置

    魅族MX2买了快2年了,今天才知道有这个功能,唉 连接一个无线网络,比如我的centos 长按网络名字 选代理设置,设置自己的代理,再也不用SS 或 VPN 的android端了,老是提示ROOT权限 ...

  4. centos vpn client set

    http://my.oschina.net/fishman/blog/96227 先检查服务器是否安装过 ppp, pptp, pptp-setup, 如果没有, yum -y install  安装 ...

  5. js中的继承2--原型继承

    一. 原型与构造函数 Js所有的函数都有一个prototype属性,这个属性引用了一个对象,即原型对象,也简称原型.这个函数包括构造函数和普通函数,我们讲的更多是构造函数的原型,但是也不能否定普通函数 ...

  6. MultiByteToWideChar和WideCharToMultiByte用法详解

    今天写ini文件的时候发现的问题: TCHAR temp[]; //strcpy_s(temp, request.newVersion); MultiByteToWideChar(CP_ACP, , ...

  7. android 链接蓝牙不稳定的解决建议

    My workaround I scan BLE for a short period of time 3-4 seconds then I turn scan OFF for 3-4 seconds ...

  8. usb device selection

  9. 查看Linux系统之终端登录的方式(last命令)

    更多,请看他人博客:last 命令 http://www.cnblogs.com/kerrycode/p/4223751.html http://www.xitongzhijia.net/xtjc/2 ...

  10. SQL2008-截取字段函数

    ltrim()  int转字符 Left('ABC',2)='AB' right('ABC',2)='BC' SUBSTRING('ABC',1,2)='AB'  和DELPHI中的COPY一样Sub ...