Polynomial Class
Learn to implement a polynomial class in this lesson.
We'll cover the following
Challenge
An expression like + + + – is called a Polynomial. Write a program that creates a polynomial class called poly
and implement addition of two such polynomials.
This class contains a structure called a term
. This structure stores the coefficient, coeff
, and exponent, exp
, of the term of a polynomial. The data member noofterms
stores the total number of terms that an object of the poly
class is supposed to hold.
Note: It is assumed that the exponent of each successive term is less than that of the previous term.
Sample run
First polynomial:
1x^7 + 2x^6 + 3x^5 + 4x^4 + 5x^2
Second polynomial:
1x^4 + 1x^3 + 1x^2 + 1x^1 + 2
Resultant polynomial:
1x^7 + 2x^6 + 3x^5 + 5x^4 + 1x^3 + 6x^2 + 1x^1 + 2
Coding exercise
Your job is to define a constructor and methods outside the poly
class given below.
Get hands-on with 1400+ tech skills courses.