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

 
#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();
}


 

阅读更多
文章标签: include
上一篇D3D 字体
下一篇容器Vector的使用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭