Help with Polynomial Class and Dynamic Arrays
Mar 25, 2017 at 7:17pm UTC
Hello, I am doing an assignment for my 2nd year programming class. I am supposed to create a program that can add, subtract, and multiply polynomials and constants. I need help overloading my arithmetic operators as well as my
<<
and
>>
operators. If someone could also take a look at what I have so far and make sure that it looks good and makes sense, that would be really helpful. I'm still pretty new so I might need the vocab to be simplified haha. My original thought process is that I could use the dynamic array where the size and index number of the array will be the degrees so a[2] would be X^2 and and a[0] refers to a constant, then the actual value stored at that index place would be the coefficient of the x term. Here is what my class structure looks like, as well as the functions that I have already attempted to code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include<iostream>
#include<cstdlib>
using namespace std;
typedef int * IntPtr;
class polynomial
{
public :
polynomial(int degree_Size, int coefficient[]);
polynomial();//sets degreeSize to 10
polynomial(const polynomial& polynomailObject);//copy constructor
void input();
polynomial& operator = (const polynomial& rightSide);
~polynomial();
friend const polynomial operator +(const polynomial& number1, const polynomial& number2);
friend const polynomial operator -(const polynomial& number1, const polynomial& number2);
friend const polynomial operator *(const polynomial& number1, const polynomial& number2);
friend istream& operator >> (istream& inputStream, polynomial newNumber);
friend ostream& operator <<(ostream& outputStream, const polynomial& number);
int getDegreeSize()const { return degreeSize; }
private :
int degreeSize;
int *privateCoefficient;
};
polynomial::polynomial(int degree_Size,int coefficient[])
{
degreeSize = degree_Size;
privateCoefficient = new int [degreeSize];
for (int i = 0; i < degreeSize; i++)
{
privateCoefficient[i] = coefficient[i];
}
}
polynomial::polynomial()
{
degreeSize = 10;
privateCoefficient = new int [degreeSize];
}
polynomial::~polynomial()
{
if (privateCoefficient)
{
delete [] privateCoefficient;
privateCoefficient = NULL;
}
}
polynomial::polynomial(const polynomial& polynomialObject):degreeSize(polynomialObject.getDegreeSize())
{
privateCoefficient= new int [degreeSize];
for (int i = 0; i < degreeSize; i++)
{
privateCoefficient[i] = polynomialObject.privateCoefficient[i];
}
}
polynomial& polynomial::operator = (const polynomial& rightSide) {
if (this == &rightSide)
{
return *this ;
}
else
{
degreeSize = rightSide.degreeSize;
delete [] privateCoefficient;
privateCoefficient = new int [degreeSize];
for (int i = 0; i <= degreeSize; i++)
{
privateCoefficient[i] = rightSide.privateCoefficient[i];
}
return *this ;
}
}
istream& operator >> (istream& inputStream, polynomial& newNumber)
{
int degreeSize;
int *privateCoefficient;
cout << "Please enter the highest degree of your polynomial: X^" ;
cin >> degreeSize;
privateCoefficient = new int [degreeSize];
for (int i = 0; i <= degreeSize; i++)
{
cout << "Please enter the coefficient for term X^" << i << endl;
cin >> privateCoefficient[i];
}
}
If you see any logic flaws please let me know. I want this to be done right and I want to learn. I am welcome to your teaching!
Last edited on Mar 25, 2017 at 8:43pm UTC
Topic archived. No new replies allowed.