运算符重载实际上是一种特殊的函数重载,你必须定义一个函数。并告诉C++翻译器,当遇到重载运算符调用该函数。该功能称为运算符重载函数,通常类的成员函数。
定义运算符重载函数的一般格式:
返回值类型 类名::operator重载的运算符(參数表)
{……}
注:在进行測试时。因为+=、-=、*=、/=它会改变原来值,无需单独测试。
定义运算符重载函数的一般格式:
返回值类型 类名::operator重载的运算符(參数表)
{……}
operator是keyword。它与重载的运算符一起构成函数名。
因函数名的特殊性。C++编译器能够将这类函数识别出来。
详细的加减乘除等代码运算例如以下:
<span style="font-size:18px;">#include<iostream> #include<string> using namespace std; class Complex; //提前声明 Complex operator+(int x, Complex &c); //整数和对象的运算 ostream& operator<<(ostream &out, const Complex &c); //输出流 istream& operator>>(istream &in, Complex &c); //输入流 class Complex //定义Complex类 { friend istream& operator>>(istream &in, Complex &c);//定义友元,能够訪问私有成员 friend ostream& operator<<(ostream &out, const Complex &c); friend Complex operator+(int x, Complex &c); public: Complex(const double real=0,const double imag=0):m_real(real),m_imag(imag)//构造函数(初始化) {} ~Complex() //析构函数 {} void Show()const //显示 { cout<<"("<<m_real<<","<<m_imag<<")"<<endl; } Complex operator+(int x) //整型加 { return Complex(m_real+x,m_imag); } Complex operator+(Complex &c) //对象加 { return Complex(m_real+c.m_real,m_imag+c.m_imag); } Complex operator-(Complex &c) //对象减 { return Complex(m_real-c.m_real,m_imag-c.m_imag); } Complex operator*(Complex &c) //对象乘 { return Complex(m_real*c.m_real-m_imag*c.m_imag, m_real*c.m_imag+m_imag*c.m_real); } Complex operator/(Complex &c) //对象除 { double t = c.m_real*c.m_real+ c.m_imag * c.m_imag;//分母 return Complex((m_real *c.m_real+ m_imag * c.m_imag)/t, (m_imag *c.m_real - m_real * c.m_imag)/t); } Complex operator+=(Complex &c) //对象加等于 { return Complex(m_real+m_real+c.m_real,m_imag+m_imag+c.m_imag); } Complex operator-=(Complex &c) //对象减等于 { return Complex(c.m_real,c.m_imag); } Complex operator*=(Complex &c) //对象乘等于 { return Complex(m_real*m_real*c.m_real-m_real*m_imag* c.m_imag-m_imag*m_real*c.m_imag-m_imag*m_imag*c.m_real , m_real*m_real*c.m_imag+m_real*m_imag*c.m_real+ m_imag*m_real*c.m_real-m_imag*m_imag*c.m_imag); } Complex operator/=(Complex &c) //对象除等于 { double t = c.m_real*c.m_real+ c.m_imag * c.m_imag;//分母的分母 double m = (((m_real *c.m_real+ m_imag * c.m_imag)/t)*//分母 ((m_real *c.m_real+ m_imag * c.m_imag)/t) +((m_imag *c.m_real - m_real * c.m_imag)/t)* ((m_imag *c.m_real - m_real * c.m_imag)/t)); double a = (m_real *c.m_real+ m_imag * c.m_imag)/t;//后值的实部 double b = (m_imag *c.m_real - m_real * c.m_imag)/t;//后值的虚部 return Complex((m_real*a-m_imag*b)/m,(m_imag*a+m_real*b)/m); } private: double m_real; //私有成员 double m_imag; //私有成员 }; Complex operator+(int x, Complex &c) //类外定义 { return Complex(x+c.m_real,c.m_imag); } ostream& operator<<(ostream &out, const Complex &c) { out<<"("<<c.m_real<<","<<c.m_imag<<")"; return out; } istream& operator>>(istream &in, Complex &c) { in>>c.m_real>>c.m_imag; return in; } void main() //函数測试 { Complex c1(1,2); Complex c2(2,3); Complex n; int s; cin >> s; n = s; cout<<n<<endl; Complex a; a = c1 + c2; cout<<a<<endl; Complex b; b = c1 - c2; cout<<b<<endl; Complex c; c = c1 * c2; cout<<c<<endl; Complex d; d = c1 / c2; cout<<d<<endl; Complex e; e = c1 += c2; cout<<e<<endl; Complex f; f = c1 -= c2; cout<<f<<endl; Complex g; g = c1 *= c2; cout<<g<<endl; Complex k; k = c1 /= c2; cout<<k<<endl; Complex l; int x; cin >> x; l = x + c1; cout<<l<<endl; }</span>
假设有不完好的地方希望大家能够指出,谢谢~
在后期的学习中。我发现对于+=、-=、*=、/=的理解有错误。改正例如以下:
<span style="font-size:18px;">Complex operator+=(Complex &c) //对象加等于 { m_real = m_real+c.m_real; m_imag = m_imag+c.m_imag; return *this; } Complex operator-=(Complex &c) //对象减等于 { m_real = m_real-c.m_real; m_imag = m_imag-c.m_imag; return *this; } Complex operator*=(Complex &c) //对象乘等于 { int new_m_real = m_real; m_real = m_real*c.m_real-m_imag*c.m_imag; m_imag = new_m_real*c.m_imag+m_imag*c.m_real; return *this; } Complex operator/=(Complex &c) //对象除等于 { double t = c.m_real*c.m_real+ c.m_imag * c.m_imag;//分母 int new_m_real = m_real; m_real = (m_real *c.m_real+ m_imag * c.m_imag)/t; m_imag = (m_imag *c.m_real - new_m_real * c.m_imag)/t; return *this; }</span>
注:在进行測试时。因为+=、-=、*=、/=它会改变原来值,无需单独测试。
版权声明:本文博主原创文章。博客,未经同意不得转载。