Skip to main content

Creation and Behavior of software Objects

Learning Objectives

Objective is to better understand
  1. Constructor
  2. Destructor
  3. Operator Overloading
  4. Dynamic Memory Allocation
and also gain additional practice with classes

Resources


Problem Sets

Complete the following task with in 2 hours

Problem 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.
  1. Int degree() – Return the degree the of the polynomial
  2. 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)).
  3. 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)).
  4. 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).
  5. 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.
  6. 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.
  7. 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.
  8. int evaluate(int x) - method to compute and return the polynomial evaluated at x
  9. Additionally, the class should have the following constructors:
    1. Polynomial () - The polynomial is initialized to be a degree 1 polynomial whose only coefficient is zero.
    2. 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.
    3. Polynomial (string s) – The polynomial is initialized to the string.
    4. Polynomial (const Polynomial &p) – The polynomial is initialized to the polynomial passed (Know as copy constructor).
  10. The class should have the following operators overloaded
    1. =
    2. ==
    3. +=
    4. +
    5. -=
    6. *
    7. *=
    8. << (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
    9. >> (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
  11. ~Polynomial() - The class should have destructor to delete the memory allocated to the integer array
Write a short test main function for polynomial class with the menu option like
"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
#Quit
You can add few more option to the menu if you like

Comments

Popular posts from this blog

Mini Project - Railway Reservation System

Module 1 Overview Railway Reservation System-(RRS) Project is developed to help students learn and realize capability of C programming language, and appreciate procedural approach of developing a system. RRS helps users reserve/cancel berths, search for trains, check reservation/cancellation history, etc... On the other hand it helps the railway reservation officers to prepare reservation charts, organize train schedules, etc... RRS provides two types of log-ins one is the User log-in for railway customers and the other one is Admin log-in for railway reservation officers. Project is divided into 10 modules of four hours duration each. All the modules will give exactly what to develop in the 4 hour time frame. Since most of the modules are inter-related it is important to keep track of the previous code. After submission it is allowed to make changes in the previous code to suit / integrate well to the system. Before we start the project, we need to make the followin...

Inheritance

Week 1 - M5 : Inheritance Learning Objectives At the end of this module, you will be able to Understand the implementation of inheritance in C++ Override the base class members Access overridden base class members using the scope resolution operator Understand the concept of base class initialization Introduction Inheritance The philosophy behind inheritance is to portray things as they exist in the real world. As inheritance is found in real world, it is important feature of OO programming. Inheritance has many advantages, the most important of them being the resuability of code. Once a class is defined and debugged, it can be used to create new subclasses. The reuse of existing class saves time and effort. The Class from which another class is derived is called the base class . The class which inherits the properties of the base class is called the derived class . Each instance of the derived class includes all the members of the base class. Since...