I am trying add two polynomials by overloading the + operator. When I run the program it only adds the first coeficients. I cannot figure out why it is only returning the first coeficient.

``````//header file
#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

//class file

#include "Poly.h"
#include <iostream>
#include <cmath>
using namespace std;

Poly::Poly():degree(0){
}
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)//this is where I am having trouble
{
Poly temp;//declare to hold new result
for (int i=0; i<degree; i++){
temp.coefs[i] =coefs[i] + p.coefs[i];
}

return temp;//only returns first coefficint

}
Poly Poly:: operator*(const Poly& p)
{
Poly temp;//declare to hold new result
temp.degree =degree * p.degree;
temp.degree=this->degree*p.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;
}

//client file
#include <iostream>
#include "Poly.h"
using namespace std;

int main () {
Poly p1, p2, p3;
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 <<" P2 = "<<p2<<endl;
p3=p1+p2;
cout << p3<<endl;
cout << "What do you want x to be: ";
cin>>x;
cout << p1.evaluate(x);
return 0;
}``````
2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by gerard4143

Well the default constructor set degree to zero....the first element of the array coefs.

``````Poly Poly:: operator+(const Poly& p)
{
Poly temp;
for (int i=0; i<degree; i++){//look here...what's degree equal?
temp.coefs[i] =coefs[i] + p.coefs[i];
}
return temp;
}``````

Edited by gerard4143: n/a

But when I changed degree to 7 I still got the same result

I would put some std::cout's in here and check some values like degree

``````Poly Poly:: operator+(const Poly& p)
{
Poly temp;
for (int i=0; i<degree; i++){//look here...what's degree equal?
temp.coefs[i] =coefs[i] + p.coefs[i];
}
return temp;
}``````
This question has already been answered. 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.