In: Computer Science
Develop a class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. has the coefficient 2 and the exponent 4. The User should be able to add as many terms as he wants, so the array should be dynamic.
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:
1. Overload the addition operator (+) to add two Polynomials.
2. Overload the subtraction operator (-) to subtract two Polynomials.
3. Overload the assignment operator to assign one Polynomial to another.
4. Overload the multiplication operator (*) to multiply two Polynomials.
5. Overload the addition assignment operator (+=), subtraction assignment operator (-=), and multiplication assignment operator (*=).
6. Enable input and output of Polynomials via overloaded >> and << operators, respectively.
Provide 3 more classes, derived from the main Polynomial class:
1. Linear (highest-degree term is of 1st degree)
2. Quadratic (highest-degree term is of 2nd degree)
3. Cubic (highest-degree term is of 3rd degree)
If the user tries to enter a higher degree polynomial into one of these classes, then print out the error and only store the terms that are valid. In the main function, create instances of all the classes defined, i.e. Linear, Quadric, Cubic, and Polynomial, and test out all these operations on those objects, and print the results on the console.
Explain the code in your own words in a paragraph.
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
//defining the polynomial class
class Polynomial{
public:
//using dynamic array and a pair to hold the coefficient and the exponent
vector<pair<int, int>> polymomial;
Polynomial(){
}
//to get the coeff corrosponding to a degree
int getCoff(int degree){
for(auto it = polymomial.begin(); it != polymomial.end(); it++){
if((*it).second == degree){
return (*it).first;
}
}
return 0;
}
//set a coeff corrosponding to a degree
void setCoff(int degree, int coff){
for(auto it = polymomial.begin(); it != polymomial.end(); it++){
if((*it).second == degree){
(*it).second = coff;
}
}
}
//adding a new term to the polynomial
void addTerm(int coff, int deg){
polymomial.push_back(make_pair(coff, deg));
}
//overloading addition operator
Polynomial operator +(Polynomial &p){
Polynomial r;
//adding the terms that are present in this, this and other
for(auto it = polymomial.begin(); it != polymomial.end(); it++){
r.addTerm((*it).first + p.getCoff((*it).second), (*it).second);
}
//adding the terms that are present in only other
for(auto it = p.polymomial.begin(); it != p.polymomial.end(); it++){
if(p.getCoff((*it).second) == 0){
r.polymomial.push_back(*it);
}
}
return r;
}
Polynomial operator -(Polynomial p){
Polynomial r;
for(auto it = polymomial.begin(); it != polymomial.end(); it++){
r.addTerm((*it).first - p.getCoff((*it).second), (*it).second);
}
for(auto it = p.polymomial.begin(); it != p.polymomial.end(); it++){
if(p.getCoff((*it).second) == 0){
r.polymomial.push_back(*it);
}
}
return r;
}
Polynomial operator *(Polynomial p){
Polynomial r;
for(auto it1 = polymomial.begin(); it1 != polymomial.end(); it1++){
for(auto it2 = polymomial.begin(); it2 != polymomial.end(); it2++){
r.addTerm((*it1).first * (*it2).first, (*it1).second + (*it1).second);
}
}
return r;
}
void operator =(Polynomial &p){
for(auto it = p.polymomial.begin(); it != p.polymomial.end(); it++){
if(p.getCoff((*it).second) == 0){
polymomial.push_back(*it);
}
}
}
Polynomial operator +=(Polynomial &p){
Polynomial r = *this + p;
return r;
}
Polynomial operator -=(Polynomial &p){
Polynomial r = *this - p;
return r;
}
Polynomial operator *=(Polynomial &p){
Polynomial r = *this * p;
return r;
}
friend ostream &operator<<( ostream &out, const Polynomial &p ) {
auto it = p.polymomial.begin();
for(; it != p.polymomial.end() - 1; it++){
out<<(*it).first<<"x"<<"^"<<(*it).second<<" + ";
}
out<<(*it).first<<"x"<<"^"<<(*it).second;
out<<endl;
return out;
}
friend istream &operator>>( istream &in, Polynomial &p ) {
int terms;
cout<<"No.of terms: ";
in>>terms;
for(int i = 0; i < terms; i++){
int c, d;
cout<<"Coefficient, Degree: ";
in>>c; in>>d;
p.addTerm(c, d);
}
return in;
}
~Polynomial(){
polymomial.~vector();
}
};
int main() {
Polynomial p, q;
cout<<"Second Polynomial"<<endl;
cin>>p;
cout<<p;
cout<<"Second Polynomial"<<endl;
cin>>q;
cout<<q;
cout<<"Polynomial Addition"<<endl;
cout<<p+q;
cout<<"Polynomial Subtraction"<<endl;
cout<<p-q;
}