Question
C++: Polynomial Class) Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent,
C++: Polynomial Class) Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent, e.g., the term 2x^4 has the coefficient 2 and the exponent 4. 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 one Polynomial to another. d) Overload the multiplication operator (*) to multiply two Polynomials. e) Overload the addition assignment operator (+=), subtraction assignment operator (-=), and multiplication assignment operator (*=).
Need 5 files: main.cpp, Term.hpp, Term.cpp, Polynomial.hpp, Polynomial.hpp
Here is the current code thus far, need help with the Polynomial.hpp, Polynomial.cpp, and main.cpp. The term.hpp and term.cpp should be good.
//Term.hpp
#pragma once #include
using namespace std;
class Term { friend ostream &operator<<(ostream &, const Term &); friend istream &operator >> (istream &, Term &); public: Term(); Term(int c, int e); ~Term(); void setCoefficient(); void setExponent(); void getCoefficient(); void getExponent(); private: int coefficient; int exponent; };
//Term.cpp
#include
using namespace std; using std::setw;
Term::Term(int c, int e) //constructor { setCoefficient(1); setExponent(2); }
Term::~Term() //destructor {
}
void Term::setCoefficient(int c) { coefficient = c; }
void Term::setExponent(int e) { exponent = e; }
void Term::getCoefficient() { return coefficient; }
void Term::getExponent() { return exponent; }
ostream &operator<<(ostream &output, const Term &term) { output << "C:" << term.coefficient << " E:" << term.exponent; return output; }
istream &operator >> (istream &input, Term &term) { input.ignore(); input >> term.coefficient; input.ignore(3) //skips the C: and the space input >> term.exponent; return input; }
//polynomial.hpp
#pragma once #include
using namespace std;
class Polynomial{ friend ostream &operator<<(ostream &, const Polynomial &); friend istream &operator >> (istream &, Polynomial &);
public: Polynomial(); ~Polynomial(); Polynomial &operator+(const Polynomial&); Polynomial &operator-(const Polynomial&); Polynomial &operator=(const Polynomial&); Polynomial &operator+=(const Polynomial&); Polynomial &operator-=(const Polynomial&); void setCoefficient(int); void setExponent(int); int getCoefficient(int); int getExponent(int);
private: Term c[20]; Term e[20]; int index;
};
//polynomial.cpp
#include
using namespace std;
Polynomial::Polynomial() { srand(time(NULL));
for (int i = 0; i < 20; i++) { c[i].setCoefficient(rand()%5); c[i].setExponent(rand()%5); } }
Polynomial::~Polynomial() {
}
int Polynomial :: getExponent(int index) { //return e[index]; }
int Polynomial :: getCoefficient(int index) { //return c[index]; }
Polynomial &Polynomial :: operator+(const Polynomial &plus) { }
Polynomial &Polynomial :: operator-(const Polynomial &subt) { }
Polynomial &Polynomial :: operator=(const Polynomial &equal) { }
Polynomial &Polynomial :: operator+=(const Polynomial &equal) { }
Polynomial &Polynomial :: operator-=(const Polynomial &equal) { }
//main.cpp
#include
using namespace std;
int main() { Term t1; Term t2; Term t3; Polynomial p1; Polynomial p2; Polynomial p3;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started