(1)再定义一目运算符 -,-c相当于0-c。
/* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *dood luck *文件名称:d.cpp *作 者:张旺华 *完成日期:2015年4月29日 *版 本 号:v1.0 * */ #include<iostream> using namespace std; class Complex { public: Complex() { real=0; imag=0; } Complex(double r,double i) { real=r; imag=i; } Complex operator-(); friend Complex operator+(const Complex &c1,const Complex &c2); friend Complex operator-(const Complex &c1,const Complex &c2); friend Complex operator*(const Complex &c1,const Complex &c2); friend Complex operator/(const Complex &c1,const Complex &c2); friend Complex operator+(const double &b,const Complex &c2); friend Complex operator-(const double &b,const Complex &c2); friend Complex operator*(const double &b,const Complex &c2); friend Complex operator/(const double &b,const Complex &c2); friend Complex operator+(const Complex &c1,const double &b); friend Complex operator-(const Complex &c1,const double &b); friend Complex operator*(const Complex &c1,const double &b); friend Complex operator/(const Complex &c1,const double &b); void display(); private: double real; double imag; }; //下面定义成员函数 Complex Complex::operator-() { Complex p(-real,imag); return p; } Complex operator+(const double &b,const Complex &c2) { Complex c(b,0); return c+c2; } Complex operator-(const double &b,const Complex &c2) { Complex c(b,0); return c-c2; } Complex operator*(const double &b,const Complex &c2) { Complex c(b,0); return c*c2; } Complex operator/(const double &b,const Complex &c2) { Complex c(b,0); return c/c2; } Complex operator+(const Complex &c1,const double &b) { Complex c(b,0); return c1+c; } Complex operator-(const Complex &c1,const double &b) { Complex c(b,0); return c1-c; } Complex operator*(const Complex &c1,const double &b) { Complex c(b,0); return c1*c; } Complex operator/(const Complex &c1,const double &b) { Complex c(b,0); return c1/c; } void Complex ::display() { cout<<"("<<real<<","<<imag<<")"<<endl; } Complex operator+(const Complex &c1,const Complex &c2) { Complex a; a.real=c1.real+c2.real; a.imag=c1.imag+c2.imag; return a; } Complex operator-(const Complex &c1,const Complex &c2) { Complex a; a.real=c1.real-c2.real; a.imag=c1.imag-c2.imag; return a; } Complex operator*(const Complex &c1,const Complex &c2) { Complex a; a.real=c1.real*c2.real+c1.imag*c2.imag; a.imag=c1.imag+c2.real+c1.real*c2.imag; return a; } Complex operator/(const Complex &c1,const Complex &c2) { Complex a; double k; k=1/(c2.real*c2.real+c2.imag*c2.imag); a.real=(c1.real*c2.real+c1.imag*c2.imag)*k; a.imag=(c1.imag+c2.real-c1.real*c2.imag)*k; return a; } //下面定义用于测试的main()函数 int main() { Complex c1(3,4),c2(5,-10),c3; c3=-c1; c3.display(); double d=11; cout<<"c1="; c1.display(); cout<<"c2="; c2.display(); cout<<"d="<<d<<endl<<endl; cout<<"下面是重载运算符的计算结果: "<<endl; c3=c1+c2; cout<<"c1+c2="; c3.display(); cout<<"c1+d="; (c1+d).display(); cout<<"d+c1="; (d+c1).display(); c3=c1-c2; cout<<"c1-c2="; c3.display(); cout<<"c1-d="; (c1-d).display(); cout<<"d-c1="; (d-c1).display(); c3=c1*c2; cout<<"c1*c2="; c3.display(); cout<<"c1*d="; (c1*d).display(); cout<<"d*c1="; (d*c1).display(); c3=c1/c2; cout<<"c1/c2="; c3.display(); cout<<"c1/d="; (c1/d).display(); cout<<"d/c1="; (d/c1).display(); return 0; }
学习心得: 就是不太清楚为什么我用friend 就不行了,好像一定要用成员函数
原文地址:http://blog.csdn.net/wh201458501106/article/details/45602183