Learning Objectives
Objective is to better understand- Constructor
- Destructor
- Operator Overloading
- Dynamic Memory Allocation
Resources
- http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html
- http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html
Problem Sets
Complete the following task with in 2 hoursProblem Set A
1. Consider a polynomial with integer coefficients, for instance 5x3 -4 x2 + 5. We can represent this polynomial by array of integers. The 0th element of the array is the coefficient of the x0 term (5 in our example), the 1st element of the array is the coefficient of the x1 term (0 in our example), the 2nd element of the array is the coefficient of the x2 term (-4 in our example), etc. The size of the array determines how large a degree the polynomial can have: a polynomial with degree d will need an array with at least d+ 1 elements. Note that the actual degree of the polynomial may be smaller than (size of vector - 1), since some of the higher order terms may have zero coefficients.In this problem set, you will implement a Polynomial class. The polynomial class is having two members: array of integers and degree of polynomial as integer. It should support the following member functions.
- Int degree() – Return the degree the of the polynomial
- Polynomial addition(const Polynomial &p) – Returns the sum q + p, where q is the polynomial object whose member function is called. The degree of this sum will be at most max (degree(q), degree(p)).
- Polynomial sub(const Polynomial &p) – Returns the subtraction q - p, where q is the polynomial object whose member function is called. The degree of this subtract will be at most max (degree(q), degree(p)).
- Polynomial multiply(const Polynomial&p) - Returns the product qp, where q is the polynomial object whose member function is called. The degree of this sum will be exactly degree(q) + degree(p).
- bool equal(const Polynomial&p) - Returns true if q is the same polynomial as p, and false otherwise, where q is the polynomial object whose member function is called. Two polynomials are equal if and only if (1) they have the same degree and (2) the coefficient of the terms of the same degree are equal.
- void read() - Reads the polynomial from cin. Your code should prompt the user for the degree of the polynomial, create a array with the correct size, and then use a loop to prompt and input each coefficient.
- void print() - Outputs the polynomial to cout. The polynomial should be printed out using a format that resembles the following: 5x^3 -4x^2 + 5.
- int evaluate(int x) - method to compute and return the polynomial evaluated at x
- Additionally, the class should have the following constructors:
- Polynomial () - The polynomial is initialized to be a degree 1 polynomial whose only coefficient is zero.
- Polynomial (int d) - The polynomial is initialized to be a degree d polynomial. That is, the array should have size d+1 whose coefficients are all zero.
- Polynomial (string s) – The polynomial is initialized to the string.
- Polynomial (const Polynomial &p) – The polynomial is initialized to the polynomial passed (Know as copy constructor).
- The class should have the following operators overloaded
- =
- ==
- +=
- +
- –
- -=
- *
- *=
- << (as cout << href="http://gethelp.devx.com/techtips/cpp_pro/10min/10min0400.asp" class="external free" title="http://gethelp.devx.com/techtips/cpp_pro/10min/10min0400.asp" rel="nofollow">http://gethelp.devx.com/techtips/cpp_pro/10min/10min0400.asp
- >> (as cin >> p) Refer: http://www.csse.monash.edu.au/~jonmc/CSE2305/Topics/10.19.OpOverload/html/text.html#an_important_use_of_operator_overloading
- ~Polynomial() - The class should have destructor to delete the memory allocated to the integer array
"What would you like to do?" #Give a value to a polynomial(print the polynomial) #Add the two polynomials #Multiply the two polynomials #Subtract the two polynomials #Test equality of the two polynomial #Evaluate the two polynomials for a given value of x #QuitYou can add few more option to the menu if you like
Comments
Post a Comment