Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++: Here is the assignment at task - Develop class Term. The class contains (1) A coefficient, and an exponent. E.g., term 2x^4 has the

C++: Here is the assignment at task - Develop class Term. The class contains (1) A coefficient, and an exponent. E.g., term 2x^4 has the coefficient of 2 and the exponent 4. (2) Overload the output operator (<<) to properly display the term Develop class Polynomial. The internal representation of a Polynomial is an array of terms. 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 assignment operator to assign one Polynomial to another. c) Overload the addition assignment operator (+=), subtraction assignment operator (-=), d) Overload the output operator (<<) to properly display the polynomial. Needs 5 files

Here is my current code that I'm getting errors for.

//term.hpp

#include

using namespace std; class Term {

friend ostream &operator<<(ostream &, const Term &);

public: void operator=(const Term &term); Term(); Term(int c, int e); ~Term(); void setCoefficient(int c); void setExponent(int e); int getCoefficient(); int getExponent(); private: int coefficient; int exponent; };

//term.cpp

#include #include "term.hpp"

using namespace std; //using std::setw;

Term::Term(int c, int e) //constructor { coefficient = c; exponent = e; }

Term::~Term() //destructor { }

void Term::setCoefficient(int c) { coefficient = c; } void Term::setExponent(int e) { exponent = e; } int Term::getCoefficient() { return coefficient; } int Term::getExponent() { return exponent; } void Term::operator=(const Term &term) { coefficient=term.coefficient; exponent=term.exponent; } ostream &operator<<(ostream &output, const Term &term) { output << term.coefficient << "x^" << term.exponent; return output; }

//polynomial.hpp

#include #include #include "term.hpp" using namespace std; class Polynomial{ friend ostream &operator<<(ostream &, const Polynomial &); public: Polynomial(); ~Polynomial(); int getSize(); Term getTerm(int index); bool addTerm(int c,int e); bool deleteTerm(int c); Polynomial operator+(const Polynomial&); Polynomial operator=(const Polynomial&); Polynomial operator+=(const Polynomial&); Polynomial operator-=(const Polynomial&); Term terms[20]; //Term c[20]; You do not have to make two arrays as the term already consists of coefficients and exponents //Term e[20]; The polynomial would be the array of terms int size; // size of the polynomial };

//polynomial.cpp

#include #include #include #include #include "polynomial.hpp" //#include "term.hpp" using namespace std;

Polynomial::Polynomial() { size=0; srand(time(NULL)); for (int i = 0; i < 20; i++) { terms[i].setCoefficient(rand()%5); terms[i].setExponent(rand()%5); } }

Polynomial::~Polynomial() { delete []terms; }

Term Polynomial :: getTerm(int index) { return terms[index]; }

bool Polynomial::addTerm(int c,int e) //return true if term is added, else return false { if(c==0) // Term with exponent 0 would make the whole term 0 , 0*x^2=0 return false;

else { Term newTerm(c,e); if(size==0) { terms[0]=newTerm; } else if(newTerm.getExponent()>terms[0].getExponent()) { for(int i=size-1;i>=0;i--) { terms[i+1]=terms[i]; } terms[0]=newTerm; } else if(newTerm.getExponent()i;j--) { terms[j+1]=terms[j]; } terms[i]=newTerm; break; } } } size++; return true; } }

bool Polynomial::deleteTerm(int e) // return true if terms is found and deleted, else return false { if(size==0) return false; for(int i=0;i

int Polynomial::getSize() { return size; }

Polynomial &Polynomial :: operator+(const Polynomial &plus) { Polynomial p; for(int i=0;i

//main.cpp (in progress)

#include #include #include #include "polynomial.hpp"

using namespace std;

int main() { Term t1; Term t2; Term t3; Polynomial p1; Polynomial p2; Polynomial p3;

return 0; }

These are the errors that are occurring at polynomial.cpp:

||=== Build: Debug in assignment111 (compiler: GNU GCC Compiler) ===| C:\Users\X\Desktop\assignment\polynomial.cpp||In destructor 'Polynomial::~Polynomial()':| C:\Users\X\Desktop\assignment\polynomial.cpp|22|warning: deleting array '((Polynomial*)this)->Polynomial::terms'| C:\Users\X\Desktop\assignment\polynomial.cpp|108|error: prototype for 'Polynomial& Polynomial::operator+(const Polynomial&)' does not match any in class 'Polynomial'| C:\Users\X\Desktop\assignment\polynomial.hpp|14|error: candidate is: Polynomial Polynomial::operator+(const Polynomial&)| C:\Users\X\Desktop\assignment\polynomial.cpp|117|error: prototype for 'Polynomial& Polynomial::operator=(const Polynomial&)' does not match any in class 'Polynomial'| C:\Users\X\Desktop\assignment\polynomial.hpp|15|error: candidate is: Polynomial Polynomial::operator=(const Polynomial&)| C:\Users\X\Desktop\assignment\polynomial.cpp|124|error: prototype for 'Polynomial& Polynomial::operator+=(const Polynomial&)' does not match any in class 'Polynomial'| C:\Users\X\Desktop\assignment\polynomial.hpp|16|error: candidate is: Polynomial Polynomial::operator+=(const Polynomial&)| C:\Users\X\Desktop\assignment\polynomial.cpp|130|error: prototype for 'Polynomial& Polynomial::operator-=(const Polynomial&)' does not match any in class 'Polynomial'| C:\Users\X\Desktop\assignment\polynomial.hpp|17|error: candidate is: Polynomial Polynomial::operator-=(const Polynomial&)| ||=== Build failed: 8 error(s), 1 warning(s) (0 minute(s), 2 second(s)) ===|

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

Do the calculations to determine the missing amounts in the table.

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago