zoukankan      html  css  js  c++  java
  • 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;
    	
    }


  • 相关阅读:
    Qt Quick 简单介绍
    Windows下ElasticSearch及相关插件的安装
    Light OJ 1317 Throwing Balls into the Baskets 概率DP
    Cocos2d-x-lua学习点滴
    JAVA网络编程--UDP通信
    ASP.NET Web API 应用教程(一) ——数据流使用
    NGUI创建Camera参数为Simple 2D的UI UI对象的结构UI Root(2D)
    端口扫描器——ZenmapKail Linux渗透测
    直接操作游戏对象C#游戏开发
    BeagleBone Black项目实训手册(大学霸内部资料)
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3258150.html
Copyright © 2011-2022 走看看