操作符重载++类成员+全局

 
#include <windows.h>
#include <iostream>
using namespace std;

class Complex
{
public:
 Complex(double real = 0,double img = 0) : m_real(real),m_img(img) { }

 void Display() const
 {
  cout<<"m_real: "<<m_real<<endl<<"m_img: "<<m_img<<endl<<endl;
 }

/*********************************************************************************************/
 Complex Add(const Complex &other) const                                   //1.1类成员函数
 {
  Complex temp;
  temp.m_real = m_real + other.m_real;
  temp.m_img = m_img + other.m_img;
  return temp;
 }

//  Complex operator+(const Complex &other) const                            //1.3操作符重载 (类成员)
//  {
//   Complex temp;
//   temp.m_real = m_real + other.m_real;
//   temp.m_img = m_img + other.m_img;
//   return temp;
//  }

 friend Complex complex_add(const Complex &comp1,const Complex &comp2);   //1.2全局函数
 friend Complex operator+(const Complex &comp1,const Complex &comp2);     //1.4操作符重载 (全局友元)

/*********************************************************************************************/
//  bool operator== (const Complex &other)                 //2.1双目操作符重载 (类成员)
//  {
//   return (m_real == other.m_real) && (m_img == other.m_img);
//  }
//
//  bool operator!=(const Complex &other)                 //2.2双目操作符重载 (类成员)
//  {
//   return !(*this==other);
//  }
// 
//  Complex operator-()                                   //2.3单目操作符重载 (类成员)
//  {
//   Complex temp;
//   temp.m_real = -m_real;
//   temp.m_img = -m_img;
//   return temp;
//  }

 friend bool operator==(const Complex &comp1,const Complex &comp2); //2.4双目操作符重载 (全局友元)
 friend bool operator!=(const Complex &comp1,const Complex &comp2); //2.5双目操作符重载 (全局友元)
 friend Complex operator-(const Complex &other);                    //2.6单目操作符重载 (全局友元)
/*********************************************************************************************/

private:
 double m_real;
 double m_img;
};

Complex complex_add(const Complex &comp1,const Complex &comp2)
{
 Complex temp;
 temp.m_real = comp1.m_real + comp2.m_real;
 temp.m_img = comp1.m_img + comp2.m_img;
 return temp;
}

Complex operator+(const Complex &comp1,const Complex &comp2)
{
 Complex temp;
 temp.m_real = comp1.m_real + comp2.m_real;
 temp.m_img = comp1.m_img + comp2.m_img;
 return temp;
}

bool operator==(const Complex &comp1,const Complex &comp2)
{
 return (comp1.m_real == comp2.m_real) && (comp1.m_img == comp2.m_img);
}

bool operator!=(const Complex &comp1,const Complex &comp2)
{
 return !(comp1 == comp2);
}

Complex operator-(const Complex &other)
{
 Complex temp;
 temp.m_real = -other.m_real;
 temp.m_img = -other.m_img;
 return temp;
}

void main(int argc,TCHAR*argv[])
{
 Complex comp1(3.0,2.0),comp2(6.0,8.0),comp3;
 comp1.Display();
 comp2.Display();
 //comp3 = comp1.Add(comp2);
 //comp3 = complex_add(comp1,comp2);
 comp3 = comp1 + comp2;
 comp3.Display();

 Complex comp4(9.0,10.0);
 if(comp1!=comp2)
  cout<<"comp1 不等于 comp2"<<endl;
 if(comp3==comp4)
  cout<<"comp3 等于 comp4"<<endl;

 Complex comp5;
 comp5 = -comp4;
 comp5.Display();
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值