#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. 一个简单的MDI示范程序(Delphi)

    http://www.cnblogs.com/pchmonster/archive/2012/01/07/2316012.html 最为一个巩固之前有关窗体和对象的有关知识,下面就建立一个简单的MDI ...

  2. HDU 5623KK's Number DP

    题意:bc round 71 div 1 1003(有中文题面) 分析: 显然,每个人的策略就是都会拿剩下的数中最大的某几个数 假如我们用dp[i]表示当剩下i个数的时候先手得分-后手得分的最优值 那 ...

  3. Zabbix探索:模板中发现规则的使用

    其实模板的建立只要多看看系统自带的模板内容就清楚了,一目了然,不用做过多解释. 目前使用到的自动发现规则有端口和文件系统的,其他还没有仔细研究. 下面说说遇到的几个问题. 1.Key不能相同.普通项目 ...

  4. 在windows xp 平台上安装mvc4失败

    使用web 平台安装程序,在windows xp上安装mvc4 出现失败,需要主要是windows powershell 2.0安装失败,需要先卸载power shell 1.0或者 winowrm ...

  5. LeetCode题解——Median of Two Sorted Arrays

    题目: 找两个排序数组A[m]和B[n]的中位数,时间复杂度为O(log(m+n)). 解法: 更泛化的,可以找第k个数,然后返回k=(m+n)/2时的值. 代码: class Solution { ...

  6. CentOS上安装MyCat-MySQL

    1.安装JDK,要求JDK7以上. 2.下载MyCat,地址. 3.解压Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz,到usr/local/ ...

  7. NOIP2006 2k进制数

    2^k进制数 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换 ...

  8. 恒天云技术分享系列3 – KVM性能调优

    恒天云技术分享:http://www.hengtianyun.com/download-show-id-11.html KVM是什么 KVM 是 kernel-based Virtual Machin ...

  9. ListView自定义适配器--10.17

    1. 添加button 2. ViewHolder 优化性能 就是一个持有者的类,他里面一般没有方法,只有属性,作用就是一个临时的储存器,把你getView方法中每次返回的View存起来,可以下次再用 ...

  10. Ubuntu 14.04.3 LTS 配置 DNS Server

    我们目的是用一台局域网机器完成 192.168.1.113 <-->cloudshield.com的解析,指定A记录和CNAME; 0.关于Ubuntu 14.04.2 LTS 下载.安装 ...