I have this assignment for adding two polynomials using operator overloading. I am new to C++ and am having trouble putting the pieces together for the operator overloading portion of the code. I understand how to add two poylnomials together, I just don't know where to begin to put down this into code. Any direction would be appreciated. This is what I have so far:

#include <iostream>

using namespace std;

class Poly
{
//PRIVATE MEMBER FUNCTIONS
private:
   int order;
   int coeff[0];  // array of coefficients
   int size;

//PUBLIC MEMBER FUNCTIONS
public:
       
   Poly::Poly() //default constructor: order = 0 & coeff[0] =1
   {
       cout << "Setting order to 0 and coeff[0] to 1..." << endl;
       order =0;
       coeff[0]=1;
   }   
   void set();
   void get();
   
   Poly(int Order , int Default = 0)// creates  Nth order poly and inits all coeffs
   {
        order=Order;
        size=order + 1;
        for (int i=order; i>=0; i--)
            {
                coeff[i] = 0;
                cout << i << "x^" << coeff[i] << " ";
            }
        cout << endl;
   }   
};

Poly operator+(const Poly &rhs)
{

//code goes here.  

}

void Poly::set(){
     int i, j;
     cout << "(Highest power is " << order << ")\n";
     for (i=0; i<=order; i++)
     {
         cout << "Enter coefficient for degree ("<<i<<"):  ";
         cin >> j;
         coeff[i] = j; 
     }
     cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}

void Poly::get(){
     for (int i=order; i>=0; i--)
     {
         cout << coeff[i] << "x^" << i << " ";
     }
     cout << "\n\nAlternate display:\n";
     for (int i=order; i>=0; i--)
     {
         cout << i << ": " << coeff[i] << endl;
     }
     cout << endl;
     cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
     cout << endl;
}

  
int main()
{
    int one, two;
    cout << "Enter highest power for polynomial #1: ";
    cin >> one;
    cout << endl;
    cout << "Enter highest power for polynomial #2: ";
    cin >> two;
    cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    cout << "Initializing polynomials...\n" << endl;
    Poly P1(one,0), P2(two,0), P3;
    cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    cout << "Setting values for polynomial #1...\n";
    P1.set();
    cout << "Setting values for polynomial #2...\n";
    P2.set();
    cout << "Displaying polynomial #1...\n";
    P1.get();
    cout << "Displaying polynomial #2...\n";
    P2.get();
    
    cout << "Adding polynomials...\n";
    P3 = P1 + P2;
    cout << P3;
   
    system("pause");
    return 0;
}

Recommended Answers

All 6 Replies

Operator overloadings are often tricky. You'd do an addition operator like this:

Poly Poly::operator+(const Poly &rhs)
{
		Poly  temp;

		temp.size = size + rhs.size;
		return temp;
}

Note that this isn't going to solve all your problems in this program.

I would add that there's no point implementing operator+() until you're sure you can correctly input and output polynomials. I suspect something's going to need to change in how you define your Poly class.... (Hint: in your non-default constructor, how big is the array coeff[]?)

I would add that there's no point implementing operator+() until you're sure you can correctly input and output polynomials. I suspect something's going to need to change in how you define your Poly class.... (Hint: in your non-default constructor, how big is the array coeff[]?)

I am not sure what you mean by "how big is the array coeff[]". I was hoping to set the size of the array by prompting the user for the size.

I realize that this needs a LOT of work, but i'm trying to piece things together one at a time. For this line: "cout << "Test << " << pnomial.get() << endl;" can you help explain why the pnomial.get() doesn't work? I'm trying to display the values stored in the array, but I get an error that says "no match for 'operator<<'...

#include <iostream>

using namespace std;

typedef int* IntPtr;

class Poly
{
friend istream& operator>> (istream& stream, Poly& pnom);      
friend ostream& operator<< (ostream& stream, Poly& pnom);

//PRIVATE MEMBER FUNCTIONS
private:
   int order;
   int coef;
   int coeff[0];  // array of coefficients
   int size;

//PUBLIC MEMBER FUNCTIONS
public:
       
   Poly::Poly() //default constructor: order = 0 & coeff[0] =1
   {
       cout << "Setting order to 0 and coeff[0] to 1..." << endl;
       order =0;
       coeff[0]=1;
   }   
   friend Poly operator+( const Poly& p1, const Poly& p2);

   void set();
   void get();
//   int * get();  //returns pointer of coeff array
   int getOrder();  //get order of polynomial
   
   Poly(int Order , int Default = 0)// creates  Nth order poly and inits all coeffs
   {
        order=Order;
        coef=Order;
        cout << "order: " << order << endl;
        size=order + 1;
        for (int i=order; i>=0; i--)
            {
                coeff[i] = 0;
                cout << coeff[i] << "x^" << i << " ";
            }
        cout << endl;
   }   
};

Poly operator+(Poly& p1, Poly& p2)
{
 cout << "test + " << p1 << endl;
 Poly result;
 return result;
}

Poly operator-(Poly& p1, Poly& p2)
{
 cout << "test - " << p1;           
 Poly result;
 return result;
}

istream& operator>> (istream& stream, Poly& pnomial)
{
	cout << "test >> ";
    return stream;
}

ostream& operator<< (ostream& stream, Poly& pnomial)
{
   cout << "Test << " << pnomial.get() << endl;
    return stream;
}

void Poly::set(){
     int i, j;
     cout << "(Highest power is " << coef << ")\n";
     for (i=0; i<=coef; i++)
     {
         cout << "Enter coefficient for degree ("<<i<<"):  ";
         cin >> j;
         coeff[i] = j; 
     }
     cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}

void Poly::get(){
     for (int i=order; i>=0; i--)
     {
         cout << coeff[i] << "x^" << i << " ";
     }
     cout << "\n\nAlternate display:\n";
     for (int i=order; i>=0; i--)
     {
         cout << i << ": " << coeff[i] << endl;
     }
     cout << endl;
     cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
     cout << endl;
}

  
int main()
{
    int one, two;
    IntPtr p, q;
    cout << "Enter highest power for polynomial #1: ";
    cin >> one;
    cout << endl;
    cout << "Enter highest power for polynomial #2: ";
    cin >> two;
    cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    cout << "Initializing polynomials...\n" << endl;
    Poly P1(one,0), P2(two,0);
    cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    cout << "Setting values for polynomial #1...\n";
    P1.set();
    cout << "Setting values for polynomial #2...\n";
    P2.set();
    cout << "Displaying polynomial #1...\n";
    P1.get();
    cout << "Displaying polynomial #2...\n";
    P2.get();
    
    Poly P3;
    cout << "Adding polynomials...\n";
    P3 = P1 + P2;
    cout << P3;   
    cout << "Subtracting polynomials...\n";
    P3 = P1 - P2;
    cout << P3;   
    system("pause");
    return 0;
}

int coeff[0];

declares an array with capacity to hold no ints.

If you want to be able to specify how many coefficients will be in coeff at run time then you need to declare coeff with enough capacity to hold the largest number of coefficients an end user could possibly enter for any polynomial or you could use dynamic memory instead of static memory and get just enough capacity for any given polynomial.

As far as line 72 above, cout << "Test << " << pnomial.get() << endl; , Poly::get() returns a void. You can't "cout" a void.

You have the concepts scrambled between "have a void function that outputs the info to cout" and "implement a custom operator<<() that inlines the info in the specified output-stream, the same way a predefined type works."

The way Poly::get() is currently implemented, it does its own output to cout, so you could just call:

cout << "Test -- ";
pnomial.get();
cout << endl;

Or if you want to be able to output a Poly instance as though it's any other object, implement your friend function operator<<() to output the desired contents from the get() method. I've rather-thoroughly re-implemented it to print the polynomial in a "standard" form. Make sure you understand what each line does, in case your course instructor asks!

ostream& operator<< (ostream& stream, const Poly& pnomial)
{
    bool needs_preceding_plus = false;
    for (int i=pnomial.order; i>=0; i--)
    {
        if (pnomial.coeff[i] == 0.0)
            continue;
        if (needs_preceding_plus)
            stream << " + ";
        else
        {
            // will need it for the next term
            needs_preceding_plus = true;
        }
        if (i > 1)
            stream << pnomial.coeff[i] << " x^" << i;
        else if (i == 1)
            stream << pnomial.coeff[i] << " x";
        else
            stream << pnomial.coeff[i];
    }
    return stream;
}

Untested, so sorry if I've inadvertently introduced any compiler errors!

Thank you for the replies. The project was handed in last week, but i'm in it for the long haul so even though the assignment is done with, I will continue working on this and studying what you have provided. Thanks!

As far as line 72 above, cout << "Test << " << pnomial.get() << endl; , Poly::get() returns a void. You can't "cout" a void.

You have the concepts scrambled between "have a void function that outputs the info to cout" and "implement a custom operator<<() that inlines the info in the specified output-stream, the same way a predefined type works."

The way Poly::get() is currently implemented, it does its own output to cout, so you could just call:

cout << "Test -- ";
pnomial.get();
cout << endl;

Or if you want to be able to output a Poly instance as though it's any other object, implement your friend function operator<<() to output the desired contents from the get() method. I've rather-thoroughly re-implemented it to print the polynomial in a "standard" form. Make sure you understand what each line does, in case your course instructor asks!

ostream& operator<< (ostream& stream, const Poly& pnomial)
{
    bool needs_preceding_plus = false;
    for (int i=pnomial.order; i>=0; i--)
    {
        if (pnomial.coeff[i] == 0.0)
            continue;
        if (needs_preceding_plus)
            stream << " + ";
        else
        {
            // will need it for the next term
            needs_preceding_plus = true;
        }
        if (i > 1)
            stream << pnomial.coeff[i] << " x^" << i;
        else if (i == 1)
            stream << pnomial.coeff[i] << " x";
        else
            stream << pnomial.coeff[i];
    }
    return stream;
}

Untested, so sorry if I've inadvertently introduced any compiler errors!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.