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;
}