第二学期第八周项目1--实现复数类中的运算符重载

复数类模板与运算符重载实践及问题解析
本文记录了作者在第二学期第八周的项目中尝试实现复数类模板并重载运算符的过程。代码尝试将类模板与运算符重载结合,但遇到了报错问题。作者分享了只重载double类型的代码片段,并表达了对C++复杂性的体会,认为C++内部机制深奥且丰富。

以下是我将类模板和重载结合起来的代码,可是会报错。张玉老师让问老贺。





/*
 * 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++是个蹊跷的东西,内幕很多哦~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值