朋友运营商中的变量范围

时间:2017-03-17 00:49:51

标签: c++ scope operator-overloading friend

对于我们的一个类赋值,我们必须实现一个多项式类。它存储多项式的系数并输出答案。

在我的类定义中,我有一个输出函数的朋友操作符:

friend std::ostream& operator << (std::ostream& out, Polynomial& p);

我的实现如下:

std::ostream& operator << (std::ostream& out, Polynomial& p) {
    double ans = 0;

    for(int i = 0; i <= p.order; ++i)
        ans += (double)p.coefficents[i] * pow(p.x, i);

    out << ans;

    return out;
}

我的主要功能(由我的导师编写)是:

int main ()
{
    Polynomial p1 (0.5,3); // Invokes two argument constructor for p1
    p1.inputCoefficients(); // Set the coefficient of polynomial p1
    cout << "Polynomial p1 evaluates to " << p1 << endl; 
    Polynomial p2(p1), p3; // Copy constructor for p2 and default constructor for p3
    cout << "Polynomial p2 evaluates to " << p2 << endl; 
    cout << "Polynomial p3 evaluates to " << p3 << endl;
    p3 = p2; // Copy assignment operator
    return 0;

}

我的问题是: 当我用这行代码运行我的程序时:

double ans = 0;

我的输出是:

Polynomial p1 evaluates to 1.375
Polynomial p2 evaluates to 1.375
Polynomial p3 evaluates to 0
Polynomial destroyed!
Polynomial destroyed!
Polynomial destroyed!

哪个是正确的输出 但如果我改变这一行:

double ans;

意外的事情开始发生:

Polynomial p1 evaluates to 1.375
Polynomial p2 evaluates to 1.375
Polynomial p3 evaluates to 1.375
Polynomial destroyed!
Polynomial destroyed!
Polynomial destroyed!

为什么p3评估为1.375? p3是使用默认构造函数创建的,所以不输出0吗?

一旦输出多项式,ans的范围就不会消失?即使它没有,或ans保持上次运行的值,那么p2不会加倍(为2.75),因为<<运算符运行了两次? 请随时获取技术和建议,我很好奇,想知道实际发生的内容。

以下是我的全部代码(仅供参考):

/*
Using dynamic arrays, implement a polynomial class. In mathematics, polynomial is a function of the form f(x) = a0*x^0 + a1*x^1 + a2*x^2 + a3*x^3 + ....n terms. Here, a0, a1, a2 etc. are the coefficients of the polynomial and n is the order of the polynomial.


The private variables include the value of x (a real number), the order of the polynomial (an integer) and the dynamic array that stores the coefficients (real numbers).

The public methods include

a. default constructor that sets the value of x to zero and the order to 0,
b. a two argument constructor that takes as arguments the value of x and the order of the polynomial. The values of the coefficients are set to zero.
c. inputCoefficients(): prompts the user to input the value of the coefficients of the polynomial. For this homework, skip the user input. Instead assign the coefficent values equal to the index of the position in the array. For example, a0 = 0, a1 = 1, a2 = 2 and so on depending on the order of the particular polynomial object
c. a copy constructor
d. << operator overloaded that returns the value of the polynomial (obtained by evaluating the polynomial). 
e. == overloaded (copy assignment operator)
f. destructor. Deallocates dynamic arrays and prints a message "Polynomial destroyed! "

Below is the testing program - 
*/

#include <iostream>
using std::cin;
using std::endl;
using std::cout;
#include <cmath>

class Polynomial {
public:
    Polynomial() {
        x = 0,
        order = 0;
        coefficents = new double[order + 1];
    }

    Polynomial(const double x, const int order) {
        this->x = x;
        this->order = order;

        this->coefficents = new double[order + 1];

        for(int i = 0; i <= order; ++i)
            this->coefficents[i] = 0;
    }

    Polynomial(const Polynomial& p) {
        this->x = p.x;
        this->order = p.order;

        this->coefficents = new double[this->order];

        for(int i = 0; i <= this->order; ++i)
            this->coefficents[i] = p.coefficents[i];
    }

    ~Polynomial() {
        std::cout << "Polynomial destroyed! " << std::endl;
        delete[] coefficents;
    }

    void inputCoefficients() {
        /*
        for(auto& num: coefficents) {
            std::cout << "Enter the next coefficent:: ";
            std::cin >> num;
        }
        std::cout << "Thank you" << std::endl;
        */
        for(int i = 0; i <= order; ++i) {
            coefficents[i] = i;
        }
    }

    Polynomial& operator = (const Polynomial& p) {
        this->x = p.x;
        this->order = p.order;

        delete[] this->coefficents;

        this->coefficents = new double[order + 1];

        for(int i = 0; i <= this->order; ++i)
            this->coefficents[i] = p.coefficents[i];

        return *this;
    }

    friend std::ostream& operator << (std::ostream& out, Polynomial& p);
    friend bool operator == (const Polynomial& p1, const Polynomial& p2);

private:
    double x;
    double* coefficents;
    int order;
};

std::ostream& operator << (std::ostream& out, Polynomial& p) {
    double ans;

    for(int i = 0; i <= p.order; ++i)
        ans += (double)p.coefficents[i] * pow(p.x, i);

    out << ans;

    return out;
}

bool operator == (const Polynomial& p1, const Polynomial& p2) {
    if((p1.x != p2.x) && (p1.order != p2.order))
        return false;

    for(int i = 0; i < p1.order; ++i) {
        if(p1.coefficents[i] != p2.coefficents[i])
            return false;
    }

    return true;
}



int main ()
{
    Polynomial p1 (0.5,3); // Invokes two argument constructor for p1
    p1.inputCoefficients(); // Set the coefficient of polynomial p1
    cout << "Polynomial p1 evaluates to " << p1 << endl; 
    Polynomial p2(p1), p3; // Copy constructor for p2 and default constructor for p3
    cout << "Polynomial p2 evaluates to " << p2 << endl; 
    cout << "Polynomial p3 evaluates to " << p3 << endl;
    p3 = p2; // Copy assignment operator
    return 0;

}

1 个答案:

答案 0 :(得分:0)

您的<<功能包含以下行:

ans += (double)p.coefficents[i] * pow(p.x, i);

如果您未将ans初始化为0,那么ans的初始值将是不确定的,然后您将每个术语添加到此。所以你得到一个随机的结果。

在您的情况下,ans显然保持了之前通话的价值。由于p3是一个空多项式,因此循环从不向它添加任何内容,因此您打印前一个结果。