Last Edited By **Krjb Donovan** **Last Updated:** Mar 13, 2014 02:42 PM GMT

QUESTION: Hi Zlatko,

Remember the program I had to work on last time with polynomials? Now that we've learned about operator overloading in class, I'm supposed to redo the polynomial assignment but this time overload the following operators:

1. the stream operators >> and << 2. the +, - 3. the * for quadratics only 4. == 5 != 6. the predecrement and postdecrement operators -- which will change the sign for each coefficient of the polynomial.

I think it shouldn't be too hard now we've already done the last assignment however...

Two questions:

A. I don't get where would the == and the != come into play in this assignment? What are we supposed to be comparing using this equality and non-equality checking operators?

B. it says the predecrement and the postdecrement operators will change the sign for each coefficient of the polynomial. This is the result of overloading them?

Thanks,

Mike

ANSWER: Hello Mike.

Yes the operator overloading is easy. You don't really need to think of any new algorithms, you could just rename the old ones. I recommend, as the first step, that your overloaded operators call the old functions, that way you are calling code you already know is working. In the second step, you can move the algorithms in the old functions into the new operator overloaded ones if you like.

For example you had Polynomial calcSum(const Polynomial& p2); Now you add Polynomial operator+(const Polynomial& p2);

and in the body you have Polynomial Polynomial::operator+(const Polynomial& p2) {

return calcSum(p2);

}

A) The == and != are new, and yes you would use them to compare one poly against another. When all the coefficients are the same, the polynomials are equal. Notice that you can implement != by calling ==, and returning the opposite.

B) Any overloaded operator will do exactly what you program it to do. You can program operator+ to do division if you like. You can program predecrement to change the signs of all the coefficients if that is what the assignment asks you to do. In professional programming, it is a bad idea to overload an operator so that it behaves in a way that is different from how it would behave on a built-in type, and in this case, using a predecrement to change signs is something you would normally avoid.

---------- FOLLOW-UP ----------

QUESTION: Hi,

Though I'm not done yet but here's one thing, is there a way to keep things relatively the same (see code below), but you have the user enter in only 3 coefficients when entering in a quadratic? (So you just do 1 2 3 instead of 0 0 1 2 3)

Mike

- include <iostream>

using namespace std;

class Polynomial { friend Polynomial operator+(const Polynomial& p1, const Polynomial& p2); friend Polynomial operator-(const Polynomial& p1, const Polynomial& p2); friend istream &operator>>(istream&, Polynomial& p1);

public:

Polynomial(int t1= 0, int t2= 0, int t3= 0, int t4= 0, int

t5= 0);

Polynomial calcDifference(const Polynomial& p2); Polynomial calcProduct(const Polynomial& p2); void printPolynomial();

private:

int c4, c3, c2, c1, c0;

};

Polynomial :: Polynomial(int t1, int t2, int t3, int t4, int t5) {

c4 = t1; c3 = t2; c2 = t3; c1 = t4; c0 = t5;

}

istream &operator>>(istream &input, Polynomial& p1) { input >> p1.c4 >> p1.c3 >> p1.c2 >> p1.c1 >> p1.c0; return input; }

void Polynomial :: printPolynomial() {

cout << "\n-------------------------------\n"; int coef[5] = {c0, c1, c2, c3, c4}; bool first = true; for(int exp = 4; exp >= 0; --exp) { if (coef[exp] != 0) { if (coef[exp] == -1 && exp != 0) { cout << '-'; } else if (coef[exp] > 0) { if (!first) { cout << "+"; } if (coef[exp] == 1 && exp == 0 || coef[exp]

!= 1)

{ cout << coef[exp]; } } else { cout << coef[exp]; }

if (exp >= 1) cout << "x"; if (exp >= 2) cout << "^" << exp; first = false; } }

if (first) { cout << "0"; } cout << endl;

}

Polynomial operator+(const Polynomial& p1, const Polynomial& p2) {

Polynomial sum; sum.c4 = p1.c4 + p2.c4; sum.c3 = p1.c3 + p2.c3; sum.c2 = p1.c2 + p2.c2; sum.c1 = p1.c1 + p2.c1; sum.c0 = p1.c0 + p2.c0; return sum;

}

Polynomial operator-(const Polynomial& p1, const Polynomial& p2) {

Polynomial difference; difference.c4 = p1.c4 - p2.c4; difference.c3 = p1.c3 - p2.c3; difference.c2 = p1.c2 - p2.c2; difference.c1 = p1.c1 - p2.c1; difference.c0 = p1.c0 - p2.c0; return difference;

}

Polynomial Polynomial:: calcProduct(const Polynomial& p2) {

Polynomial product; product.c4 = c2*p2.c2; product.c3 = c2*p2.c1 + c1*p2.c2; product.c2 = c2*p2.c0 + c1*p2.c1 + c0*p2.c2; product.c1 = c1*p2.c0 + c0*p2.c1; product.c0 = c0*p2.c0; return product;

}

int main() {

Polynomial p1, p2, p3, p4;

cout << "Enter the coefficients of the first polynomial: "; cin >> p1;

cout << "Enter the coefficients of the second polynomial: "; cin >> p2; cout << endl;

cout << "Enter the coefficients of the first quadratic polynomial: "; cin >> p3;

cout << "Enter the coefficients of the first quadratic polynomial: "; cin >> p4;

Polynomial sum = p1 + p2; Polynomial difference = p1 - p2;

cout << "The first polynomial"; p1.printPolynomial(); cout << "\nThe second polynomial"; p2.printPolynomial();

cout << "\nThe sum"; sum.printPolynomial();

cout << "\nThe difference"; difference.printPolynomial(); cout << endl;

cout << "\n===============================\n"; cout << "\n\nThe first quadratic polynomial"; p3.printPolynomial(); cout << "\nThe second quadratic polynomial"; p4.printPolynomial();

Polynomial product = p3.calcProduct(p4); cout << "\nThe product"; product.printPolynomial(); cout << endl; return 0;

}

ANSWER: Hello Mike

If you want to have a special case for quadratics, you need to program it explicitly. You need a separate function for it or you need some flag to indicate that you are entering a quadratic.

A simple way is to have a global variable stating, "I am now initializing a quadratic". This global variable is set just before operator>> is called, and checked for in operator>>. When the variable is true, the quadratic version of the input is done. Probably the operator>> function should unset the variable, so that the caller has to set it again every time a quadratic is wanted.

Another, more complicated, way is to have a special polynomial called Quadratic which derives from Polynomial. You would have a second operator>> which takes a Quadratic, and when you want to initialize a Quadratic, you create one and pass it to operator>>. The C++ language automatically chooses the correct operator>> to call. The advantage of this is that you can allow multiplication on quadratics only, and you don't need special code in your multiplication to ensure that only quadratics are passed in. It is ensured by the compiler. The disadvantage is that you must deal with inheritance.

I suggest you go with the first way, because your instructor is probably not expecting inheritance, but you should ask your instructor for an opinion too.

Do you need an example ?

---------- FOLLOW-UP ----------

QUESTION: Hi Zlatko,

An example would be nice and yes, inheritance is coming later in class, so I'm not going there yet.

But more important things (See the code below now and some of this stuff isn't completely intuitive to me anyway):

A. I'm having a problem with how you get the postdecrement and the predecrement to work? Here, I thought you have to define one with a int in the parameter list, which although this int does nothing but tell the compiler to use the postdecrement operation.

B. The == operator part, did I do that right? Somehow, I kept thinking it looked ok, but the compiler tells me something is wrong.

C. The printing of the polynomials, how would you now declare the array of the terms, if ostream &operator<<(ostream& output, Polynomial& p1) was what I wanted as the function header, so that I may just do cout << p1 to get p1 and so forth in the main? This should just be overloading the << operator anyway I think.

Thanks, Mike

- include <iostream>

using namespace std;

class Polynomial { friend Polynomial operator+(const Polynomial& p1, const Polynomial& p2); friend Polynomial operator-(const Polynomial& p1, const Polynomial& p2); friend Polynomial operator*(const Polynomial& p1, const Polynomial& p2); friend Polynomial operator==(const Polynomial& p1, const Polynomial& p2); friend istream &operator>>(istream&, Polynomial& p1); //friend ostream &operator<<(ostream&, Polynomial& p1);

public:

Polynomial(int t1= 0, int t2= 0, int t3= 0, int t4= 0, int

t5= 0);

void printPolynomial(); Polynomial& operator==(const Polynomial & p1); Polynomial& operator--(void); Polynomial& operator--(int);

private:

int c4, c3, c2, c1, c0;

};

Polynomial :: Polynomial(int t1, int t2, int t3, int t4, int t5) {

c4 = t1; c3 = t2; c2 = t3; c1 = t4; c0 = t5;

}

istream &operator>>(istream& input, Polynomial& p1) { input >> p1.c4 >> p1.c3 >> p1.c2 >> p1.c1 >> p1.c0; return input; }

//ostream &operator<<(ostream& output, Polynomial& p1) void Polynomial :: printPolynomial() {

cout << "

";

//Polynomial c0, c1, c2, c3, c4; int coef[5] = {c0, c1, c2, c3, c4}; bool first = true; for(int exp = 4; exp >= 0; --exp) { if (coef[exp] != 0) { if (coef[exp] == -1 && exp != 0) { cout << '-'; } else if (coef[exp] > 0) { if (!first) { cout << "+"; } if (coef[exp] == 1 && exp == 0 || coef[exp]

!= 1)

{ cout << coef[exp]; } } else { cout << coef[exp]; }

if (exp >= 1) cout << "x"; if (exp >= 2) cout << "^" << exp; first = false; } }

if (first) { cout << "0"; } cout << endl;

}

Polynomial operator+(const Polynomial& p1, const Polynomial& p2) {

Polynomial sum; sum.c4 = p1.c4 + p2.c4; sum.c3 = p1.c3 + p2.c3; sum.c2 = p1.c2 + p2.c2; sum.c1 = p1.c1 + p2.c1; sum.c0 = p1.c0 + p2.c0; return sum;

}

Polynomial operator-(const Polynomial& p1, const Polynomial& p2) {

Polynomial difference; difference.c4 = p1.c4 - p2.c4; difference.c3 = p1.c3 - p2.c3; difference.c2 = p1.c2 - p2.c2; difference.c1 = p1.c1 - p2.c1; difference.c0 = p1.c0 - p2.c0; return difference;

}

Polynomial operator*(const Polynomial& p1, const Polynomial& p2) {

Polynomial product; product.c4 = p1.c2*p2.c2; product.c3 = p1.c2*p2.c1 + p1.c1*p2.c2; product.c2 = p1.c2*p2.c0 + p1.c1*p2.c1 + p1.c0*p2.c2; product.c1 = p1.c1*p2.c0 + p1.c0*p2.c1; product.c0 = p1.c0*p2.c0; return product;

}

bool operator==(const Polynomial& p1, const Polynomial& p2)
{
return (p1 == p2);
}

bool operator!=(const Polynomial& p1, const Polynomial& p2) { return !(p1== p2); }

Polynomial operator--(const Polynomial& p1, const Polynomial& p2) {

}
int main()
{

Polynomial p1, p2, p3, p4, sum, difference, product;

cout << "Enter the coefficients of the first polynomial: "; cin >> p1;

cout << "Enter the coefficients of the second polynomial: "; cin >> p2; cout << endl;

cout << "Enter the coefficients of the first quadratic polynomial: "; cin >> p3;

cout << "Enter the coefficients of the first quadratic polynomial: "; cin >> p4; cout << endl;

sum = p1 + p2; difference = p1 - p2;

cout << "The first polynomial";

p1.printPolynomial(); cout << "

The second polynomial";

p2.printPolynomial();

if(p1 != p2) cout << "

The first polynomial and the

second polynomial are different."; else if(p1 == p2) cout << "

The first polynomial equals the

second polynomial." ;

cout << "

The sum";

sum.printPolynomial();

cout << "

The difference";

difference.printPolynomial(); cout << endl;

cout << " The first polynomial after predecrement";

difference.printPolynomial(); cout << endl;

cout << " The first polynomial after postdecrement";

difference.printPolynomial(); cout << endl;

cout << "

";

cout << "

The first quadratic polynomial";

p3.printPolynomial(); cout << "

The second quadratic polynomial";

p4.printPolynomial();

product = p3 * p4; cout << "

The product";

product.printPolynomial(); cout << endl; return 0;

}

Hi Mike

My answers to you are comments in the code so have a look there. There is more work for you to do. I have added a flag to check for quadratic so that your input asks for only 3 coefficients. You might decide to rip out that code. See if you like it. I haven't tested very much because there is more for you to do.

The all experts web site really butchers the code when the lines are too long, then I have to spend a lot of time fixing it. Please send me code to zlatko.c.help at gmail.com for the remainder of this ploynomial project.

Keep at it.

- include <iostream>

using namespace std;

class Polynomial {

friend Polynomial operator+(const Polynomial& p1, const Polynomial& p2); friend Polynomial operator-(const Polynomial& p1, const Polynomial& p2); friend Polynomial operator*(const Polynomial& p1, const Polynomial& p2); /* Don't need this-> friend Polynomial operator==(const Polynomial& p1, const Polynomial& p2); because it is defined as a class method. It does not need to be a friend. */ friend istream &operator>>(istream&, Polynomial& p1); friend ostream &operator<<(ostream&, Polynomial& p1);

public:

/* how about this for quadratic */ Polynomial(bool isQuadratic) { mIsQuadratic = isQuadratic; c4 = c3 = c2 = c1 = c0 = 0; }

Polynomial(int t1= 0, int t2= 0, int t3= 0, int t4= 0, int t5= 0); void printPolynomial();

/* operator == returns bool, not Polynomial & */ bool operator==(const Polynomial & rhs); /* the convention is to name the paramter rhs (for right hand side) */ /* you need this too */ bool operator!=(const Polynomial & rhs); /* the convention is to name the paramter rhs (for right hand side) */

/* pre decrement returns a reference to this poly */ Polynomial& operator--(void); /* post decrement takes an int as a hint to the compiler that this is a post-decrement the parameter is unused */

/* Traditionally, the post dec/increment returns the old value, so it does not return a reference to this poly, it returns a copy of the old poly. */

/* post decrement */ Polynomial operator--(int);

private:

int c4, c3, c2, c1, c0;

/* how about this for quadratic */ bool mIsQuadratic;

};

Polynomial :: Polynomial(int t1, int t2, int t3, int t4, int t5) {

c4 = t1; c3 = t2; c2 = t3; c1 = t4; c0 = t5;

}

istream &operator>>(istream& input, Polynomial& p1) {

/* how about this for quadratic */ if (p1.mIsQuadratic) { input >> p1.c2 >> p1.c1 >> p1.c0; } else { input >> p1.c4 >> p1.c3 >> p1.c2 >> p1.c1 >> p1.c0; } return input;

}

ostream &operator<<(ostream& output, Polynomial& p1) {

/* If you change printPolynomial to take an ostream parameter then you can call printPolynomial from here, otherwise you need to copy the code from printPolynomial into here and replace all cout with output. */ return output;

}

void Polynomial :: printPolynomial() {

cout << "----------------------------------------"; //Polynomial c0, c1, c2, c3, c4; int coef[5] = {c0, c1, c2, c3, c4};

bool first = true; for(int exp = 4; exp >= 0; --exp) { if (coef[exp] != 0) { if (coef[exp] == -1 && exp != 0) { cout << '-'; } else if (coef[exp] > 0) { if (!first) { cout << "+"; } if (coef[exp] == 1 && exp == 0 || coef[exp] != 1) { cout << coef[exp]; } } else { cout << coef[exp]; }

if (exp >= 1) cout << "x"; if (exp >= 2) cout << "^" << exp; first = false; } }

if (first) { cout << "0"; } cout << endl;

}

Polynomial operator+(const Polynomial& p1, const Polynomial& p2) {

Polynomial sum;

sum.c4 = p1.c4 + p2.c4; sum.c3 = p1.c3 + p2.c3; sum.c2 = p1.c2 + p2.c2; sum.c1 = p1.c1 + p2.c1; sum.c0 = p1.c0 + p2.c0;

return sum;

}

Polynomial operator-(const Polynomial& p1, const Polynomial& p2) {

Polynomial difference;

difference.c4 = p1.c4 - p2.c4; difference.c3 = p1.c3 - p2.c3; difference.c2 = p1.c2 - p2.c2; difference.c1 = p1.c1 - p2.c1; difference.c0 = p1.c0 - p2.c0;

return difference;

}

Polynomial operator*(const Polynomial& p1, const Polynomial& p2) {

Polynomial product; product.c4 = p1.c2*p2.c2; product.c3 = p1.c2*p2.c1 + p1.c1*p2.c2; product.c2 = p1.c2*p2.c0 + p1.c1*p2.c1 + p1.c0*p2.c2; product.c1 = p1.c1*p2.c0 + p1.c0*p2.c1; product.c0 = p1.c0*p2.c0;

return product;

}

/* this is a member of the class so you must define it as such */ bool Polynomial::operator==(const Polynomial& rhs) {

/*return (p1 == p2);*/ /*The above is not how to do it. For 2 polys to be equal, all coefficients must match */ /*Go through all corresponding coefficients of this, and rhs, and compare them and return true if they are all equal, and false otherwise */ cout << "TODO fix operator ==\n"; return false; /* replace this with real code */

}

/* this is a member of the class so you must define it as such */ bool Polynomial::operator!=(const Polynomial& rhs) {

return !(*this == rhs);

}

/* your post and pre decrement must match the declarations this is incorrect. Polynomial operator--(const Polynomial& p1, const Polynomial& p2) { }

- /

/* pre decrement */
Polynomial& Polynomial::operator--(void)
{

cout << "TODO need to implement pre decrement\n"; return *this;

}

/* Traditionally, the post decrement returns the old value, so it does not return a reference to this poly, it returns a copy of the old poly.

- /

/* post decrement */ Polynomial Polynomial::operator--(int) {

Polynomial old = *this; cout << "TODO need to implement post decrement\n"; return old;

}

int main() {

Polynomial p1, p2, sum, difference, product;

cout << "Enter the coefficients of the first polynomial: "; cin >> p1;

cout << "Enter the coefficients of the second polynomial: "; cin >> p2; cout << endl;

/* Now for the quadratic polynomials */ Polynomial p3(true); // this constructor tells that the poly is a quadratic cout << "Enter the coefficients of the first quadratic polynomial: "; cin >> p3;

cout << "Enter the coefficients of the second quadratic polynomial: "; Polynomial p4(true); // this constructor tells that the poly is a quadratic cin >> p4; cout << endl;

sum = p1 + p2; difference = p1 - p2;

cout << "The first polynomial"; p1.printPolynomial(); cout << "The second polynomial"; p2.printPolynomial();

if(p1 != p2) cout << "The first polynomial and the second polynomial are different."; else if(p1 == p2) cout << "The first polynomial equals the second polynomial." ;

cout << "The sum"; sum.printPolynomial();

cout << "The difference"; difference.printPolynomial(); cout << endl;

cout << "The first polynomial after predecrement"; difference.printPolynomial(); cout << endl;

cout << "The first polynomial after postdecrement"; difference.printPolynomial(); cout << endl;

cout << "======================================="; cout << "The first quadratic polynomial"; p3.printPolynomial(); cout << "The second quadratic polynomial"; p4.printPolynomial();

product = p3 * p4; cout << "The product"; product.printPolynomial(); cout << endl; return 0;

}

Advertisement

©2024 eLuminary LLC. All rights reserved.