复数模板类

时间:2015-12-19 01:39:34

标签: c++ class templates

#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H

#include <iostream>

using namespace std;

template <class T>
class ComplexNumber
{

    friend ostream& operator<<(ostream&, const ComplexNumber&);
    friend istream& operator>>(istream&, ComplexNumber&);
public:

    void setComplexNumber(const T&, const T&);
    T getComplexNumber(T&, T&) const;
    ComplexNumber();
    ComplexNumber(T real , T imaginary);
    T operator+(const ComplexNumber&)const;
    T operator-(const ComplexNumber&)const;
    T operator*(const ComplexNumber&)const;
    T operator/(const ComplexNumber&)const;
    T& operator=(const ComplexNumber&);
    bool operator==(const ComplexNumber&) const;
    bool operator!=(const ComplexNumber&) const;

private:

    T realnumber;
    T imaginarynumber;
};

template<class T>
ostream& operator<<(ostream& out, const ComplexNumber<T>& complex)
{

    out<<complex.realnumber;
    out<<"+";
    out<<complex.imaginarynumber;
    out<<"i";
    return out;
}

template <class T>
istream& operator>>(istream& in, ComplexNumber<T>& complex)
{
    char ch;
    in>>complex.realnumber;
    in>>complex.imaginarynumber;
    in>>ch;
    return in;
}

template <class T>
T& ComplexNumber<T>::operator=(const ComplexNumber& other)
{
    if (this != &other)
    {
        realnumber = other.realnumber;
        imaginarynumber = other.imaginarynumber;
    }

    return  *this;
}

template <class T>
bool ComplexNumber<T>::operator ==(const ComplexNumber& other) const
{
    return (realnumber == other.realnumber && imaginarynumber == other.imaginarynumber);
}

template<class T>
bool ComplexNumber<T>::operator !=(const ComplexNumber& other) const
{
    return (realnumber != other.realnumber && imaginarynumber != other.imaginarynumber);
}


//Default Constructor
template<class T>
ComplexNumber<T>::ComplexNumber()
{
    realnumber = 0.0;
    imaginarynumber = 0.0;
}


//Constructor which takes in two arguments
template<class T>
ComplexNumber<T>::ComplexNumber(const T real,const T imaginary)
{
    realnumber = real;
    imaginarynumber = imaginary;
}

template<class T>
void ComplexNumber<T>::setComplexNumber(const T& real, const T& imaginary)
{
    realnumber = real;
    imaginarynumber = imaginary;
}

//Overloaded Addition Operator
template<class T>
T ComplexNumber<T>::operator +(const ComplexNumber& other) const
{
    ComplexNumber<T>temp;
    temp.realnumber = realnumber + other.realnumber;
    temp.imaginarynumber = imaginarynumber + other.imaginarynumber;
    return ComplexNumber;
}

//Overloaded subtraction operator
template<class T>
T ComplexNumber<T>::operator -(const ComplexNumber& other) const
{
    ComplexNumber<T> temp;
    temp.realnumber = realnumber - other.realnumber;
    temp.imaginarynumber = imaginarynumber - other.imaginarynumber;
    return temp;
}

//Overloaded multiplication operator
template<class T>
T ComplexNumber<T> ::operator *(const ComplexNumber& other)const
{
    ComplexNumber<T> temp;
    temp.realnumber = (realnumber * other.realnumber) - (imaginarynumber * other.imaginarynumber);
    temp.imaginarynumber = (realnumber * other.imaginarynumber) + (imaginarynumber * other.realnumber); 
    return temp;
}

//Overloaded division operator
template<class T>
T ComplexNumber<T> ::operator/(const ComplexNumber& other)const
{
    ComplexNumber<T> temp;  
    temp.realnumber = ((realnumber * other.realnumber) + (imaginarynumber * other.imaginarynumber))/((pow(other.realnumber,2.0))+(pow(other.imaginarynumber,2.0)));
    temp.imaginarynumber = ((other.realnumber * imaginarynumber) - (realnumber * other.imaginarynumber))/((pow(other.realnumber,2.0))+(pow(other.imaginarynumber,2.0))); 
    return temp;
}


#endif

我遇到一个问题,说我的加法,减法,乘法和除法运算符无法从ComplexNumber转换为double或我想要的任何其他数据类型。请让我知道我做错了什么。感谢。

2 个答案:

答案 0 :(得分:0)

重载的operator +按值返回一个项目,这样就可以了。要记住的关键是你不能返回一个地址。例如,假设重载写为

ComplexNumber<T> &ComplexNumber<T>::operator +(const ComplexNumber<T>& other) const...

然后,您将返回指向将在垃圾收集器中删除的内容的指针。我还没有找到一种易于解释的方法来返回这种需要两个参数的运算符的指针(也是 - ,* ,?等)。喜欢听听如何做到这一点,特别是在分成.h和.cpp文件以隐藏实现时。

答案 1 :(得分:-1)

#include <iostream>
using namespace std;

template<class T=int>    // LINE-1
class mytype {
    T a, b;
public:
    mytype(T _a, T _b) : a(_a), b(_b) { }
 
 mytype<T> operator + (   mytype<T> & b) //LINE 2
{
  mytype<T> c(0,0);
  c.a = a + b.a;
  c.b = this->b + b.b;
  return(c);   // LINE 3
}
    void show() {
        cout << a << ", " << b << endl;
    }
};

int main() {
    int i1=10, i2=20, i3=30, i4=40;
   // cin >> i1 >> i2 >> i3 >> i4;

    mytype<> obj1(i1, i2);
    mytype<> obj2(i3, i4);
    mytype<> obj3 = obj1 + obj2;

    double d1=10.1, d2=20.2, d3=30.3, d4=40.4;
  //  cin >> d1 >> d2 >> d3 >> d4;

    mytype<double> obj4(d1, d2);
    mytype<double> obj5(d3, d4);
    mytype<double> obj6 = obj4 + obj5;

    obj3.show();
    obj6.show();`enter code here`

    return 0;
}`enter code here`