0

I am new to this site and it was recommended by a friend and class mate. I am having serious issues in OOP. Did great in C++ last semester but our new book is killing me. I am trying to get this program going with polynomials. The professor gave us his prototypes and main, which I think has confused me more than if I'd written it myself.
The problem wants us to use dynamic arrays to implement a polynomial class with +, -, and *. I am having trouble grasping hold of the overloaded operators mainly but input on anything else would be helpful. Here is what I have so far. May have some silly errors in it yet though. Any help is greatly appreciated.

#include <iostream>
using namespace std;
#include <cstdlib>

class Polynomial 
{ 
 private:
  double * coef;
  int size;
 public:   
  Polynomial();			// creates an empty polynomial
  Polynomial(const Polynomial & p);	// copy ctor
   // size of the coefficient array is degree of the polynomial + 1
  Polynomial(double coefnt[], int size);
  ~Polynomial();
	// use as r-value to inspect coefficient, as l-value to assign coefficient; 
  double & operator[](int degree);  //       required for 'const' correctness
  const double & operator[](int degree)const;
  //const Polynomial & operator=(const Polynomial & p);
  int getSize();
  //double eval(double arg) const;  // evaluate polynomial for arg
  Polynomial operator+(const Polynomial & p);  
  Polynomial operator-(const Polynomial & p);
  Polynomial operator*(const Polynomial & p);  
};

int main()
{
   double one[] = {1};
   Polynomial One(one, 1);
   double quad[] = {3, -2.1, 1.3}; 
   double cubic[] = {1, 2, 0, 3.2};

   Polynomial q(quad, 3);	// q is 3 - 2.1*x + 1.3*x*x
   Polynomial c(cubic, 4);	// c is 1 + 2*x + 0*x*x + 3.2*x*x*x
   Polynomial p(q);		// test copy constructor

   cout << " value of q(2) is " << q.eval(2) << endl;
   cout << " value of p(2) is " << p.eval(2) << endl;
   Polynomial r;
   r = c;				//test operator=
   cout << " value of r(2) is " << r.eval(2) << endl;
   cout << " value of c(2) is " << c.eval(2) << endl;
   cout << "\nPolynomial q " << endl;

   for ( int k = 0; k < q.getSize(); k++ )
      cout << " term with degree " << k << " has coefnt " <<  q[k] << endl;

   cout << "\nPolynomial c " << endl;

   for ( int k = 0; k < c.getSize(); k++ )
      cout << " term with degree " << k << " has coefnt " <<  c[k] << endl; 
   r = q + c;			// test operator+
   cout << "\nPolynomial r (= q + c) " << endl;

   for ( int k = 0; k < r.getSize(); k++ )
      cout << " term with degree " << k << " has coefnt " <<  r[k] << endl;
   cout << " value of (q + c)(2) is " << r.eval(2) << endl;

   r = q - c;			// test operator-
   cout << "\nPolynomial r (= q - c) " << endl;

   for ( int k = 0; k < r.getSize(); k++ )
      cout << " term with degree " << k << " has coefnt " <<  r[k] << endl;
   cout << " value of (q - c)(2) is " << r.eval(2) << endl;
   r = q * c;			// test operator*

   cout << " size of q*c is " << r.getSize() << endl;
   cout << "\nPolynomial r (= q * c) " << endl;
   for ( int k = 0; k < r.getSize(); k++ )
     cout << " term with degree " << k << " has coefnt " <<  r[k] << endl;
   cout << " value of (q * c)(2) is " << r.eval(2) << endl;
   return 0;
} 
 
Polynomial::Polynomial()
{//creates an empty polynomial
  size = 0;
  coef = new double[size+1];
  coef[0] = 0;
}

//copy constructor
Polynomial::Polynomial(const Polynomial & p)
{
  size = p.size;
  coef = new double[size+1];
  for (int i = 0; i <= size; i++)
    coef[i] = p.coef[i];
}
Polynomial::Polynomial(double coefnt[], int size)
{
  size = p.size;
  coef = new double[size+1];
  for (int i = 0; i <= size; i++)
    coef[i] = p.coef[i];
}

//destructor
Polynomial::~Polynomial()
{
  delete [] coef;
}

//overloaded []
double Polynomial::& operator[](int degree) //       required for 'const' correctness
{
	if (degree < 0)
	{
		cout << "Invalid input";
		exit(1);
	}
	else if (degree > size)
	{
		cout << "Out of bounds";
		exit(1);
	}
	else 
		return size;
}
/*
const double Polynomial::& operator[](int degree)const
{

}
*/
const Polynomial & operator=(const Polynomial & p)
{
	if ((size != p.size) 
	{
	delete [] coef;	
	coef= new double[p.size];
	for (int i = 0; i < size; i++)
		coef[p.size] = new double[size];
	}

	sizes = p.size;
	assert(coef != NULL);

	for (int i=0; i < size; i++)
		coef[i] = p.coef[i];	

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

}
/*double Polynomial::eval(double arg) const  // evaluate polynomial for arg
{

}
*/

Polynomial::Polynomial operator+(const Polynomial & p)  
{
	
}
Polynomial::Polynomial operator-(const Polynomial & p)
{


}
Polynomial::Polynomial operator*(const Polynomial & p) 
{

}
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by ithelp
0

a+b means
a.+(b) (class a has a function + which is taking b as an argument)
Stroustrup has better explanation.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.