第二学期第八周项目1--实现复数类中的运算符重载
来源:互联网 发布:特效视频制作软件 编辑:程序博客网 时间:2024/06/01 07:40
以下是我将类模板和重载结合起来的代码,可是会报错。张玉老师让问老贺。
/* * 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++是个蹊跷的东西,内幕很多哦~~
0 0
- 第二学期第八周项目1--实现复数类中的运算符重载
- 第二学期第八周项目1--实现复数类中的运算符重载(两个复数运算)
- 第二学期第八周项目1--实现复数类中的运算符重载(两个复数的运算友元)
- 第二学期第九周项目1--实现复数类中的运算符重载
- 第二学期第九周项目1--实现复数类中的运算符重载
- 第二学期第九周项目1--实现复数类中的运算符重载(复数与浮点数)
- 第二学期第八周项目2--实现Time类中的运算符重载
- 第二学期第八周项目3--分数类中的运算符重载(1)
- 第八周项目1-3:实现复数类中的运算符重载-完整的复数类
- 第八周项目1-复数类中的运算符重载
- 第八周项目1-复数类中的运算符重载
- 第二学期第八周项目3--分数类中的运算符重载(2)
- 第八周项目一 实现复数类中的运算符重载-(1)定义复数类重在运算符+-*/
- 第八周项目1-实现复数类中的运算符重载(类的成员函数)
- 第八周项目1-1-实现复数类中的运算符重载
- 第八周项目1实现复数类中的运算符重载(1)
- 第八周 项目1-实现复数类中的运算符重载(1)
- 第八周项目1-1:实现复数类中的运算符重载-成员函数
- <转>人名翻译规则
- matlab随机数
- 要把photoshop当一门编程语言来学习
- 一个较隐蔽影响无线网络使用的问题
- 【Boost】timer、progress_timer和progress_display
- 第二学期第八周项目1--实现复数类中的运算符重载
- HDU 4588 Count The Carries 计算二进制进位总数
- Js浮动层插件,点击按钮弹出层,可关闭
- 返回值的第二个作用
- 用TI CC2530DK开发时,用light swtich例子会遇到的一个问题和解决方案
- How to diff and merge files or directories on Linux
- Ubuntu 12.04中文输入法的安装
- 基础数位DP小结
- leetcode之Jump Game