标签:c++ 运算符重载operator
运算符的重载实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该重载的运算符时调用此函数。这个函数叫做运算符重载函数,通常为类的成员函数。operator是关键字,它与重载的运算符一起构成函数名。因函数名的特殊性,C++编译器可以将这类函数识别出来。
具体的加减乘除等代码运算如下:
#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; }
标签:c++ 运算符重载operator
原文地址:http://blog.csdn.net/qaz3171210/article/details/46393061