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;
    	
    }


  • 相关阅读:
    基于springcloud框架搭建项目-Eureka篇(一)
    springcloud项目实现自定义权限注解进行接口权限验证
    基于JWT实现token验证
    前端${}失效
    String常见的方法有哪些?
    Json转化的三种方式
    字符串、bute[]数组和十六进制字符串的相互转换
    Sql Server2008温故而知新系列05:查询-基础查询和条件查询
    Sql Server2008温故而知新系列04:数据增删改查之"改"
    Sql Server2008温故而知新系列03:数据增删改查之"删"
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3258150.html
Copyright © 2011-2022 走看看