I am using an overloading operator to add two polynomials. I have gotten it to work, but I am wondering if I can improve on my if statement by not changing the value of degree for the rest of my program. The if statement is used if the second polynomial is larger then the first so the program will use the larger polynomial for the loop. When I do this it changes degree(the smaller polynomial length) to p.degree(the larger one) for the rest of the program. I am pretty sure this is not good coding. Any tips?

Poly Poly:: operator+(const Poly& p)
{

	Poly temp;//declare to hold new result
	if (degree < p.degree) {
		degree=p.degree;//is there another way to do this?
	}
	for (int i=0; i<degree+1; i++){
	temp.coefs[i] =coefs[i] + p.coefs[i];
	}
	temp.degree = degree;
	return temp;
	
}

Here is the entire program

#include <iostream>
using namespace std;
#ifndef _POLY_H
#define _POLY_H
const int CAPACITY =100;
 
 
class Poly {
public:
	Poly();
	~Poly();
	void display(ostream & out)const;
	void input(istream &in);
	int evaluate(int x);
	Poly operator+(const Poly &p);
	Poly operator *(const Poly &p);
private:
	int degree;
	int coefs[CAPACITY];
};
ostream & operator<<(ostream & out, const Poly & p);
istream & operator>>(istream &in, Poly &p);
#endif
 
 
 
#include "Poly.h"
#include <iostream>
#include <cmath>
using namespace std;
 
Poly::Poly():degree(0){
	for (int i=0; i<CAPACITY; i++) {
		coefs[i]=NULL;
	}
}
Poly::~Poly(){
}
void Poly::display(ostream &out)const{
	for (int i=0; i<degree; i++) {
		out<<coefs[i]<<"x^"<<i<<" + ";
	}
	for (int i=degree; i<degree+1; i++) {
		out<<coefs[i]<<"x^"<<i;
	}
 
}
void Poly::input(istream & in){
	in>>degree;
	for (int i=0; i<degree+1; i++) {
		in>>coefs[i];
	}
}
int Poly::evaluate(int x){
	int sum=0;
	double xd = static_cast<double>(x);
	double totalPow;
	for (int i=0; i<degree+1; i++) {
		totalPow =pow(xd, i);
		sum+=(coefs[i]*totalPow);
	}
	return sum;
 
}
Poly Poly:: operator+(const Poly& p)
{
	Poly temp;//declare to hold new result
	if (degree < p.degree) {
		degree=p.degree;
	}
	for (int i=0; i<degree+1; i++){
	temp.coefs[i] =coefs[i] + p.coefs[i];
	}
	temp.degree = degree;
	return temp;
 
}
Poly Poly:: operator*(const Poly& p)
{
 
	Poly temp;//declare to hold new result
	for (int i=0; i<degree+1; i++){
		temp.coefs[i] =coefs[i] * p.coefs[i];//I don't know what to do here to add the exponents of the coefficients.  This is also where I get zero after the shorter polynomial ends.
	}
		temp.degree = degree;
	return temp;
}
istream & operator>>(istream & in, Poly &p){
	p.input(in);
	return in;
}
ostream &operator<<(ostream &out, const Poly &p){
	p.display(out);
	return out;
}
 
#include <iostream>
#include "Poly.h"
using namespace std;
 
int main () {
	Poly p1, p2, p3, p4;
	int x;
    cout << "Please enter the number of degrees you want followed by the number of coefficints: ";
	cin>>p1;
	cout<<endl;
	cout << "Please do the same again: ";
	cin>>p2;
	cout << "P1 = "<<p1<<endl;
	cout << "What do you want x to be for P1: ";
	cin>>x;
	cout << "P1 ="<<p1.evaluate(x)<<endl;
	cout <<" P2 = "<<p2<<endl;
	p3=p1+p2;
	cout << "addition "<<p3<<endl;
	p4=p1 *p2;
	cout << "time "<<p4<<endl;
    return 0;
}

Recommended Answers

All 2 Replies

You are right about the coding not being good because it alters degree, in fact operator+ and all binary operators and act on 2 operands to produce a new result are best declared const Poly Poly:: operator+(const Poly& p) const;. That way the compiler enforces the policy of not changing the object that the operator is being called on.

Basically in your operator+ you need to just set the degree of temp rather than altering the degree of the object it is being called on.

Try implementing it like this

Poly& Poly::operator+ (const Poly& p)  {
   coefs[i]=coefs[i]+p.coefs[i];
   return *this;
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.