Need help getting started on an assignment

I got this over complicated question today about overloading. The part i need help is basically everything to do with the polynomial and arrays part. I understand the basic concept of operator overloading. if someone could help me on the e and f as well as the part about creating a "test" that would be awesome

Develop class Polynomial to represent polynomial with a maximum degree of 9, where each object represents a polynomial of the form p(x)= a0+ a1x1+ a2x2+ a3x3+ a4x4+ a5x5+ a6x6+ a7x7+ a8x8+ a9x9. The internal representation of a polynomial is an array to store the coefficients, a0…a9 for the polynomial.
Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities:
a- Overload the addition operator (+) to add two Polynomials.
b- Overload the subtraction operator (-) to subtract two Polynomials.
c- Overload the assignment operator (=) to assign a Polynomial to another.
d- Overload the multiplication operator (*) to multiply two Polynomials. You can use lower degree polynomial to test this capability, as the maximum degree you can store is 9.
e- Overload the addition assignment operator (+=)
f- Overload the stream insertion operator (<<)
Create appropriate test for the overloaded operators

Quick note i am not asking any one to answer this question for me, as i am pretty sure that breaks some rule some were. I just needs some help getting started.

Overloading the += operator is very similar to overloading the + operator. In fact, it's usually a good idea to overload the += operator first, then you define operator+ using it:
1
2
3
4
5
Polynomial Polynomial::operator+(const Polynomial &src) {
    Polynomial result(*this);
    result += src;
    return src;
}


The declaration of operator+= is
1
2
3
4
Polynomial & Polynomial::operator+=(const Polynomial &src) {
    // code that adds src to *this
    return *this;
}


The hard part about overloading the stream operator is having the operator access the private members. You can do this by making the operator a friend, but I prefer having it call a member:
1
2
3
4
5
6
7
8
9
10
11
class Polynomial {
public:
    // write the Polynomial to the stream
    void put(std::ostream &os);
    ...
};

ostream &operator<<(osteam &os, const Polynomial &src) {
    src.put(os);
    return os;
};


I hope this helps. If you have more questions, post what you have so far and then try to ask specific questions.
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
#include <iostream>
#include <string>
#include <vector>

struct A
{
    /*explicit*/ A( int i = 0 ) : v(i) {} // for educational purpose: not explicit

    int value() const { return v ; }

    // compound assignment operators are overloaded as member functions
    A& operator+= ( const A& that ) { v += that.v ; return *this ; }

    private: int v ;
};

// canonical: binary operators are implemented as non-member functions
// reason 1: symmetry
//           https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html#Ro-symmetric
// reason 2: favour non-member functions over member functions
//           https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html#Rc-member

// canonical: implement binary operators in terms of the corresponding compound assignment operator
// reason: logical consistency, less code to maintain

// canonical: pass the lhs of the binary operator by value
// reason: simpler, less convoluted code.
A operator+ ( A first, const A& second ) { return first += second ; } // note: first is passed by value

std::ostream& operator<< ( std::ostream& stm, const A& a ) { return stm << a.value() ; }

int main()
{
    A first = 23 ;
    std::cout << first << '\n' ;

    first += 18 ;
    std::cout << first << '\n' ;

    std::cout << first + 17 << '\n' ;
    std::cout << 29 + first << '\n' ; // symmetry
}

http://coliru.stacked-crooked.com/a/46cd667df41b10fd
I'm relatively new to C++, but wouldn't he need more in the + and += operators to overload them? Or is that actually all he needs?
He needs more code in the += operator. The point about operator+ that JLBorges and I are making is that once you have operator+=, operator+ is really easy.
Oh okay, I misread your comment. Thanks!
I've decided to try out the question for practice, and while I was able to do the other operators, I am stuck on operator*. I'm not sure if I'm just having a mind-blank or whatever, but I'm having difficulty with it. Could anyone point me in the right direction?
Last edited on
Start by working it out by hand. How do you multiply polynomials? What is
(18 + 4x + 3x2 + 3x3) * (2 + x + 7x2) ?
How do you multiply polynomials?


Multiplication is inherently more complex than addition.

Consider a simple example:
The two polynomials p and q:

p(x) = a0 + a1x1 

q(x) = b0 + b1x1 


Addition - just add corresponding terms.
Add

p(x) + q(x) =
    (a0 + b0) + (a1 + b1)x1 

Multiplication, basically each term in the second polynomial needs to be multiplied by each term in the first.

When multiplying two powers of x, simply add the powers, e.g x2 * x3 = x(2+3) = x5

This is how a human might do it, but the program doesn't necessarily need to do things quite like this:
Multiply

p(x) * q(x) 
    =   (a0 + a1x1) * (b0 + b1x1) 

    =   a0 * (b0 + b1x1)  +  a1x1 * (b0 + b1x1) 

    =   a0 * b0 + a0 * b1x1  +  a1x1 * b0 + a1x1 * b1x1

    =   a0 * b0 
        + a0 * b1x1  +  a1x1 * b0 
        + a1x1 * b1x1

    =   (a0 * b0) + (a0 * b1 + a1 * b0)x1 + (a1 * b1)x2
Topic archived. No new replies allowed.