2017-03-17 14 views
0

私たちのクラスの割り当ての1つとして、多項式クラスを実装する必要がありました。多項式の係数を格納し、解を出力します。私のクラス定義でフレンドオペレータの変数の範囲

、私は機能出力友人演算子があります。

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; 

} 

私の質問は: 私はトンと私のプログラムを実行すると私はこれまで、その行を変更した場合

正しい出力 ですが
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 = 0; 

私の出力がこれです

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が最後に実行されたときの値を保持していたとしても、<<演算子が2回実行されたため、p2は2倍にならないでしょう(2.75)。 私は興味があり、実際に何が起こっているのかを知りたいと思っています。 、その後、初期

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

あなたはans0に初期化しない場合:あなたの<<関数は行が含まれてい

/* 
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; 

} 

答えて

0

:ここ

は、(参考)私のコードの全体です ansの値は不定となり、各項をこれに追加します。だからあなたはランダムな結果を得る。

あなたのケースでは、ansは明らかに前回の呼び出しの値を保持しています。 p3は空の多項式なので、ループには何も追加されないので、以前の結果を出力します。

+0

これは私が想定したものです。しかし 'ans'が以前の呼び出しからその値を保持していた場合、' p2'への呼び出しは2.75ではありませんでした( 'p1'の呼び出し値に加えただけです)。 'p3'への呼び出しも2.75となりますか? – Aryan

+0

他に起こっていることがあるため、あなたはそれに頼ることはできません。したがって、場合によっては上書きされる可能性があり、そうでない場合には上書きされることがあります。 – Barmar

+0

それは理にかなっています。ただランダムに起こった。次にコンパイルを実行すると、まったく異なる答えが表示されますか? – Aryan