第二学期第八周项目1--实现复数类中的运算符重载
2014-04-22 19:22
543 查看
以下是我将类模板和重载结合起来的代码,可是会报错。张玉老师让问老贺。
/** Copyright (c) 2013, 烟台大学计算机学院* All rights reserved.* 作 者: 沈远宏* 完成日期:2013 年 04月15日* 版 本 号:v1.0* 问题描述:一个定义完整的类,是可以当作独立的产品发布,成为众多项目中的“基础工程”,这样的类在方案二的基础上,扩展+、-、*、/运算符的功能,使之能与double型数据进行运算。设Complex c; double d;c+d和d+c的结果为“将d视为实部为d的复数同c相加”,其他-、*、/运算符类似。* 输出:两个复数+,-,*,/后的结果输出*/#include <iostream>using namespace std;template <class numtype>class Complex{public:Complex(numtype r,numtype i){real=r;imag=i;}Complex<numtype> operator+(numtype &d);Complex<numtype> operator+(numtype &d,Complex <numtype>&c);Complex<numtype> operator-(Complex <numtype>&c, numtype&d);Complex<numtype> operator-(numtype &d,Complex<numtype> &c);Complex<numtype> operator*(Complex<numtype> &c, numtype &d);Complex<numtype> operator*(numtype &d,Complex<numtype> &c);Complex<numtype> operator/(Complex<numtype> &c, numtype &d);Complex<numtype> operator/(numtype &d,Complex<numtype> &c);void set(numtype a,numtype b);void display();private:numtype real;numtype imag;};//下面定义成员函数template <class numtype>Complex Complex<numtype>::operator+(Complex<numtype> &c, numtype &d)//+{return Complex(d+c.real,c.imag);}template <class numtype>Complex Complex<numtype>::operator+(numtype &d,Complex<numtype> &c){return Complex(d+c.real,c.imag);}template <class numtype>Complex Complex<numtype>::operator-(Complex <numtype>&c, numtype &d)//-{return Complex(d-c.real,c.imag);}template <class numtype>Complex Complex<numtype>::operator-(numtype &d,Complex<numtype> &c){return Complex(d-c.real,c.imag);}template <class numtype>Complex Complex<numtype>::operator*(Complex<numtype> &c, numtype &d)//*{return Complex(d*c.real,c.imag);}template <class numtype>Complex Complex<numtype>::operator*(numtype &d,Complex<numtype> &c){return Complex(d*c.real,c.imag);}template <class numtype>Complex Complex<numtype>::operator/(Complex<numtype> &c, numtype &d)// /{return Complex(d/c.real,c.imag);}template <class numtype>Complex Complex<numtype>::operator/(numtype &d,Complex<numtype> &c){return Complex(d/c.real,c.imag);}void Complex::display(){cout<<real<<"+"<<"("<<imag<<")i\n";}template <class numtype>void Complex<numtype>::set(numtype a,numtype b){real=a;imag=b;}//下面定义用于测试的main()函数int main(){Complex<int>c1,c3;int d;cout<<"请依次输入附属的实部和虚部\n(整型)";cin>>a>>b;c1.set(a,b);cout<<"c1=";c1.display();cout<<"请输入一个整型数:\n";cin>>d;cout<<"c1+d=";c3=d+c1;c3.display();cout<<"c1-d=";c3=c1-d;c3.display();cout<<"c1*d=";c3=d*c1;c3.display();cout<<"c1/d=";c3=c1/d;c3.display();Complex<double>c2,c4;double a1,b1,d1;cout<<"请依次输入附属的实部和虚部\n(浮点型)";cin>>a1>>b1;c2.set(a1,b1);cout<<"c2=";c2.display();cout<<"请输入一个浮点数:\n";cin>>d1;cout<<"d1+c2=";c4=d1+c2;c4.display();cout<<"d1-c2=";c4=d1-c2;c4.display();cout<<"d1*c2=";c4=d1*c2;c4.display();cout<<"d1/c2=";c4=d1/c2;c4.display();return 0
}
下面只重载double的代码:
/** Copyright (c) 2013, 烟台大学计算机学院* All rights reserved.* 作 者: 沈远宏* 完成日期:2013 年 04月15日* 版 本 号:v1.0* 问题描述:一个定义完整的类,是可以当作独立的产品发布,成为众多项目中的“基础工程”,这样的类在方案二的基础上,扩展+、-、*、/运算符的功能,使之能与double型数据进行运算。设Complex c; double d;c+d和d+c的结果为“将d视为实部为d的复数同c相加”,其他-、*、/运算符类似。* 输出:两个复数+,-,*,/后的结果输出*/#include <iostream>using namespace std;class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}friend Complex operator+( Complex &c,double &d);friend Complex operator+(double &d,Complex &c);friend Complex operator-(double &d,Complex &c);friend Complex operator-(Complex &c,double &d);friend Complex operator*(Complex &c, double &d);friend Complex operator*( double &d,Complex &c);friend Complex operator/(Complex &c,double &d);friend Complex operator/( double &d,Complex &c);void set(double a,double b);void display();private:double real;double imag;};//下面定义成员函数Complex operator+(Complex &c, double &d)//+{return Complex(d+c.real,c.imag);}Complex operator+( double&d,Complex &c){return Complex(d+c.real,c.imag);}Complex operator-(Complex &c ,double &d)//-{return Complex(d-c.real,c.imag);}Complex operator-( double&d,Complex &c){return Complex(d-c.real,c.imag);}Complex operator*(Complex &c, double &d)//*{return Complex(d*c.real,c.imag);}Complex operator*( double&d,Complex &c){return Complex(d*c.real,c.imag);}Complex operator/(Complex &c, double &d)// /{return Complex(d/c.real,c.imag);}Complex operator/( double&d,Complex &c){return Complex(c.real/d,c.imag);}void Complex::display(){cout<<real<<"+"<<"("<<imag<<")i\n";}void Complex::set( double a, double b){real=a;imag=b;}//下面定义用于测试的main()函数int main(){Complex c1(3.0,4.0),c3;double d=5.0;cout<<"c1=";c1.display();c3=c1+d;cout<<"c1+d=";c3.display();c3=c1-d;cout<<"c1-d=";c3.display();c3=c1*d;cout<<"c1*d=";c3.display();c3=c1/d;cout<<"c1/d=";c3.display();c3=d+c1;cout<<"d+c1=";c3.display();c3=d-c1;cout<<"d-c1=";c3.display();c3=d*c1;cout<<"d*c1=";c3.display();c3=d/c1;cout<<"d/c1=";c3.display();return 0;}运行结果:心得体会:总觉得C++是个蹊跷的东西,内幕很多哦~~
相关文章推荐
- 使用OpenCV进行相机标定(基于OpenCV2.4.3)
- C++ 之Boost 实用工具类及简单使用
- C#:依据目录填充树视图
- Time类中运算符重载的改造
- Oracle数据库案例整理-Oracle系统运行时故障-断电导致数据文件状态变为RECOVER
- 使用protobuf定义消息
- BackBone.js之Router
- mysql5.5.15配置主从数据库
- Oracle数据库案例整理-Oracle系统运行时故障-无法释放已经结束的进程资源导致进程死锁
- 张国祥老师在上海讲授流程管理取得圆满成功
- sys 用户登录提示ora-01031权限不足 解决办法
- 返回值的第二个作用
- 最短路
- 基础数位DP小结
- Oracle数据库案例整理-Oracle系统运行时故障-sql_trace设置为TRUE导致Oracle根目录可用空间不足
- 在c++中尽量使用const,enmu,inline来代替#define
- 用TI CC2530DK开发时,用light swtich例子会遇到的一个问题和解决方案
- linux下进行数据备份和计划任务练习题
- 【Boost】timer、progress_timer和progress_display
- JAVA基础---数组的创建及遍历