如何使用多项式类重载运算符以及要返回的类型

时间:2012-10-13 20:28:26

标签: c++ operator-overloading

我在重载+运算符时遇到问题,我无法弄清楚原因是什么。 +运算符返回一个多项式(称为C),但它按值返回,其中赋值运算符期望多项式对象作为通过引用传递的参数。为了使C = A + B有效,我是否需要第二个赋值运算符函数,它将一个多项式作为参数传递?谢谢!

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
void line(int lines);

class Polynomial
{
    private:
        int degree;
        int* coeffs;
    public:
        //constructors
        Polynomial() {degree=0;coeffs=new int[1];}
        Polynomial(int deg) {degree=deg;coeffs=new int[deg+1];}
        Polynomial(const Polynomial& A);

        //mutators
        void GetCoeffs(istream& in);
        void EditCoeff(int deg);
        void ResetCoeffs();
        int Coeff(int deg);
        void Randomize(int max);

        //accessors
        void Show(ostream& out);
        int Degree() {return degree;}

        //operators
        friend Polynomial operator+(Polynomial& A, Polynomial& B);
        void operator =(Polynomial A);
};


int main()
{
    Polynomial A(5);
    Polynomial B(5);
    A.Randomize(5);
    B.Randomize(5);

    A.Show(cout);
    line(2);
    B.Show(cout);
    line(2);
    Polynomial C(5);
    C=A+B;
    C.Show(cout);

    return 0;
}

void Polynomial::Randomize(int max)
{
    for (int i=degree; i>=0; i--)
    {
        coeffs[i]=rand()%(max+1) + 1;
        if ((rand()%(101) + 1)%2 == 0)
            coeffs[i]*=-1;
    }
}

void Polynomial::operator =(Polynomial A)
{
    if (degree==A.degree)
    {
        for (int i=degree; i>=0; i--)
        {
            coeffs[i]=A.coeffs[i];
        }
    }
}

Polynomial Polynomial::operator+(Polynomial& A, Polynomial& B)
{
    Polynomial C;
    if (A.degree>=B.degree)
    {
        C=A;
        for (int i=B.degree; i>=0; i--)
        {
            C.coeffs[i]=A.coeffs[i]+B.coeffs[i];
        }
        C.Show(cout);
        return C;
    }
    else
    {
        C=B;
        for (int i=A.degree; i>=0; i--)
        {
            C.coeffs[i]=A.coeffs[i]+B.coeffs[i];
        }
        C.Show(cout);
        return C;

    }

}

int Polynomial::Coeff(int deg)
{
    return coeffs[deg];
}

void line(int lines)
{
    for (int i=0; i<lines; i++)
        cout << endl;
}

void Polynomial::GetCoeffs(istream& in)
{
    for (int i=degree; i>=0; i--)
    {
        in >> coeffs[i];
    }
    in.ignore();
}

void Polynomial::Show(ostream& out)
{
    for (int i=degree; i>=0; i--)
    {
        if (coeffs[i]>=0)
        {
            if (i!=degree)
                out << " + ";
            out << coeffs[i];


        }
        else
        {
            if (coeffs[i]<0)
                out << " - ";
                out << 0-coeffs[i];
        }
        if (i>1)
            out << "x^" << i;
        else if (i==1)
            out << "x";

    }
}

Polynomial::Polynomial(const Polynomial& A)
{
    coeffs=new int[A.degree+1];
    degree=A.degree;
    for (int i=A.degree; i>=0; i--)
    {
        coeffs[i]=A.coeffs[i];

    }

}

2 个答案:

答案 0 :(得分:0)

这个问题是你混淆了全局(外部类定义)operator+operator+的成员类定义。

staticPolynomial operator+(Polynomial& A, Polynomial& B);此运算符在全局范围内使用,因此在类之外。

在课堂内你需要使用以下签名。

Polynomial& operator+(const Polynomial& other);

这是一个例子。

Polynomial p;
Polynomial q;
p = p + q;

如果在类中定义运算符,则代码为:

p = p.operator+(q); //only needs one parameter.

如果运营商全局定义,则代码为:

p = ::operator+(p, q); //needs both parameter

注意: 要将其用作非成员函数,请从您的定义Polynomial operator+(Polynomial& A, Polynomial& B);中移除Polynomial Polynomial::operator+(Polynomial& A, Polynomial& B){/**/},应该移到主函数之上,它现在变为:

static Polynomial operator+(Polynomial& A, Polynomial& B){/**/}

答案 1 :(得分:0)

通常,您会以两种方式之一重载operator+

Polynomial operator+(const Polynomial &other) const;

或作为非会员职能,

Polynomial operator+(Polynomial a, const Polynomial &b);

对于后者,您通常不需要friend限定符,因为实现可能是根据已在类中定义的另一个重载运算符:

Polynomial operator+=(const Polynomial &other);

然后您的非会员实施将是:

Polynomial operator+(Polynomial a, const Polynomial &b)
{
     a+=b;
     return a;
}

如果您坚持使用代码中定义的功能,那么需要将其设为 friend如果您需要访问私人会员:

Polynomial operator+(Polynomial &a, Polynomial &b)
{
     Polynomial p;
     // add them as needed
     return p;
}