以下是我将类模板和重载结合起来的代码,可是会报错。张玉老师让问老贺。
/*
* 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++是个蹊跷的东西,内幕很多哦~~