Lab 2 Vectors Classes Create Class Pospoly Stores Polynomials X Positive Integer Coefficie Q43890361
c++ help



Lab 2: Vectors and Classes Create a class PosPoly that stores polynomials in x with positive integer coefficients. For example, 4x + 2x + 3×2 + x. The instance variables of your class should be (a) a counter for the number of powers; and (b) a vector of Pair’s, where a Pair is a struct (or a class) that stores two ints: the power and the coefficient. The powers should be in the order they were first added. For example, the above polynomial could be stored as: entry Count = 4 power 5 6 21… coeff 4 2 3 1 … The class should be stored in files PosPoly.cpp and PosPoly.h. (The code for Pair can be in its own files, but it is also okay to include it in PosPoly.h. It is also okay to adapt/use the Pair class given on the class website as solution to Practice 2.) Your PosPoly class should have the following methods: • A default constructor that initializes the polynomial to zero • An overloaded << operator for output. (It’s completely okay if it prints out in any order and doesn’t have all the bells and whistles; e.g. prints above polynomial ending with +1x 1 ) • void incrementBy(int c, int p): increment the current polynomial by cxp, where both c and and p should be positive. For example, PosPoly A; A.incrementBy(3,2); A.incrementBy(2,6); A.incrementBy (1,5); A.incrementBy (1,1); A.incrementBy(3,5); should produce the above example polynomial. • A boolean test for whether two polynomials are equal A sample test driver is provided. Adapt as desired. (Do not add main to PosPoly.cpp.) Submit via handin the files PosPoly.h/cpp (and Pair.h/cpp if created). (Your driver (should you create one) will not be used in grading.) #include <iostream> using namespace std; #include “PosPoly.h” int main() PosPoly A, B; int cof, pow; bool is Done=false; cout << “Enter first ” << endl; while( !isDone) cout << “Enter pair (0,0 to finish) “; cin >> cof >> pow; isDone = (cofr=0 && pow==0); if(!isDone ) A.incrementBy( cof, pow ); isDone=false; cout << “Enter second ” << endl; while( !isDone) { cout << “Enter pair (0,0 to finish)”; cin >> cof >> pow; isDone = (cof==0 && pow==0); if(!isDone) B.incrementBy( cof, pow ); cout << endl; cout << “A is ” << A << endl; cout << “Bis ” << B << endl; cout << (A==B ? “Equal” : “Not equal” ) << endl; return 0; Show transcribed image text Lab 2: Vectors and Classes Create a class PosPoly that stores polynomials in x with positive integer coefficients. For example, 4x + 2x + 3×2 + x. The instance variables of your class should be (a) a counter for the number of powers; and (b) a vector of Pair’s, where a Pair is a struct (or a class) that stores two ints: the power and the coefficient. The powers should be in the order they were first added. For example, the above polynomial could be stored as: entry Count = 4 power 5 6 21… coeff 4 2 3 1 … The class should be stored in files PosPoly.cpp and PosPoly.h. (The code for Pair can be in its own files, but it is also okay to include it in PosPoly.h. It is also okay to adapt/use the Pair class given on the class website as solution to Practice 2.) Your PosPoly class should have the following methods: • A default constructor that initializes the polynomial to zero • An overloaded
Expert Answer
Answer to Lab 2: Vectors and Classes Create a class PosPoly that stores polynomials in x with positive integer coefficients. For e…
OR