超载+与含有数组指针类操作者(C ++)(Overloading + operator with c

2019-10-29 07:19发布

我目前以实践操作符重载写了“多项式”级。 我已经成功重载流提取和插入运营商,但我有一些麻烦,用“+”操作符。

我班有继续创建在构造一个数组的私人指针。 我明白怎么一会超载“+”操作符与复数类,例如,但我越来越糊涂了这一计划。

在找到解决办法的指导,将不胜感激。 谢谢。

#include<iostream>
#include<stdexcept>
using namespace std;

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

class Polynomial 
{
      friend istream& operator>>(istream& in, Polynomial& p);
      friend ostream& operator<<(ostream& out, const Polynomial& p);
      public:
             Polynomial(int = 10);
             ~Polynomial();
             void assignExponent();
             Polynomial operator+(const Polynomial& other);

      private:
              int SIZE;
              int *exponents;
              int *polyPtr; //***************
};

#endif

//CONSTRUCTOR
Polynomial::Polynomial(int arraySize)
{
     if(arraySize > 0)
                  SIZE = arraySize;
     else
     {
         cout << "Array size must be greater than 0. Program will now "
              << "Terminate..." << endl;

         system("pause");
         exit(0);
     }

     polyPtr = new int[SIZE]; //*********************
     exponents = new int[SIZE];

     for(int i = 0; i<SIZE; i++)
             polyPtr[i] = 0;

     assignExponent();
};

//DESTRUCTOR
Polynomial::~Polynomial() //******************
{
     delete [] polyPtr;                         
};

//STREAM INSERTION
istream& operator>>(istream& in, Polynomial& p)
{
         for(int i = 0; i<p.SIZE; i++)
         {
               in >> p.polyPtr[i];  //*************        
         }

         return in;
};

//STREAM EXTRACTION
ostream& operator<<(ostream& out, const Polynomial& p)
{
         int exponent;
         for(int i = 0; i<p.SIZE; i++)
         {
               exponent = (p.SIZE - 1) - i;

               if(p.polyPtr[i] != 1)
               {  
                  if(exponent > 0 && exponent != 1)
                           out << p.polyPtr[i] << "x^" << exponent << " + ";  

                  if(exponent == 1)
                           out << p.polyPtr[i] << "x" << " + ";

                  if(exponent == 0)
                           out << p.polyPtr[i];  
               }

               //In order to not display coefficient if = 1
               else
               {
                   if(exponent > 0 && exponent != 1)
                           out << "x^" << exponent << " + ";  

                   if(exponent == 1)
                           out << "x" << " + ";

                   if(exponent == 0)
                           out << p.polyPtr[i];
               }    
         }       

         return out;
}; 

//Assigns a value for exponent
void Polynomial::assignExponent()
{
     for(int i = 0; i<SIZE; i++)
     {
             exponents[i] = (SIZE - 1) - i;        
     }
};

//OVERLOAD OF + OPERATOR
Polynomial Polynomial::operator+(const Polynomial& other)
{
         Polynomial sum(SIZE);
         int difference;

         //If the first polynomial is larger
         if (SIZE > other.SIZE)
         {
            difference = SIZE - other.SIZE;

            for(int i = 0; i<SIZE; i++)
            {
                 if(i - difference < 0)
                      sum.polyPtr[i] = polyPtr[i];

                 else
                 {
                     sum.polyPtr[i] = polyPtr[i] + 
                                  other.polyPtr[i - difference];  
                 }                            
            }         
         }

         //If the second polynomial is larger       **PROBLEM**  
         if(other.SIZE > SIZE)
         {
            difference = other.SIZE - SIZE;

            for(int i = 0; i<other.SIZE; i++)
            {
                 if(i - difference < 0)
                      sum.polyPtr[i] = other.polyPtr[i];

                 else
                 {
                     sum.polyPtr[i] = other.polyPtr[i] + 
                                  polyPtr[i - difference];  
                 }                            
            }         
         }

         //If the polynomials are equal
         if(SIZE == other.SIZE)     
         {
                  for(int i = SIZE-1; i >= 0; i--)
                  {
                          sum.polyPtr[i] = polyPtr[i] + other.polyPtr[i];        
                  }   
         }

         return sum;
};

int main()
{
    int polySize;

    //User enters a size for the first & second polynomial
    cout << "Enter a size for the first polynomial: ";
    cin >> polySize;
    Polynomial pOne(polySize);

    cout << "\nEnter a size for the second polynomial: ";
    cin >> polySize;
    Polynomial pTwo(polySize);

    //User enters in values (Overload of >> operator
    cout << "\n\nEnter in values for the first polynomial, "
         << "in the format - (x x x x): " << endl;
    cin >> pOne;
    cout << "\nEnter in values for the second polynomial, "
         << "in the format - (x x x x): " << endl;
    cin >> pTwo;

    //Overload << operator for output
    cout << "\nPolynomial 1 is: " << pOne << endl
         << "Polynomial 2 is: " << pTwo << endl;

    Polynomial pThree = pOne + pTwo;

    cout << "\nAfter being added together, the new polynomial is: "
         << pThree << endl;

 system("pause");   
} 

我已经更新了我当前的代码,因为我没想到打开另一个问题将是最好的一段路要走。 不管怎样,在试图排队我的多项式添加的,我已经部分成功。 该出现的唯一问题是,当所述第二多项式,pTwo,比第一大。 我已经标记的代码段, 问题 。 提前致谢。

Answer 1:

我猜+应该写象下面这样:

class Polynomial 
{
  // ...

  Polynomial operator+(const Polynomial& other);

 // ...
};

Polynomial Polynomial::operator+(const Polynomial& other)
{
     Polynomial sum(SIZE);

     for(int i = 0; i< SIZE; i++)
     {
         sum.polyPtr[i] = polyPtr[i] + other.polyPtr[i];
     }

     return sum;
}


文章来源: Overloading + operator with classes containing array pointers (C++)