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:
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.
#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
}
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.
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?