Polynomial class help

Hi Guys,

I have a small issue with my code, but can't figure it out. When I call the overloaded operator+, everything is working well until i hit the return statement,which returns some garbage values. Can someone tell me what I am doing wrong please. I suspect it's something to do with the constructors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117



#include <iostream>


#define MAX 1000

class Poly{

public:
	Poly();
	Poly(int);
	~Poly();
	Poly(const Poly&);

	int getSize()const;
	void setSize(const int);

	friend std::istream& operator>>(std::istream&,Poly&);
	friend std::ostream& operator<<(std::ostream&,Poly&);

	friend Poly operator+(const Poly&, const Poly&);

private:
	double *arr;
	int size;

};

Poly::Poly(){
	arr=new double[MAX];
	size=0;
}

Poly::Poly(int the_size){
	arr=new double[the_size];
	size=the_size;
}

Poly::~Poly(){
	delete [] arr;
}

Poly::Poly(const Poly& the_pol){
	size=the_pol.size;
	arr=new double[size];
	for(unsigned int i=0;i<size;i++)
		arr[i]=the_pol.arr[i];
}

int Poly::getSize()const{
	return size;
}
void Poly::setSize(const int the_size){
	size=the_size;
}
std::istream& operator>>(std::istream& input,Poly& the_pol){

	double value;
	char choice;
	do{
		std::cout<<"Enter coef for "<<the_pol.size<<" power: ";
		input>>value;
		the_pol.arr[the_pol.size]=value;
		the_pol.size++;
		std::cout<<"Another(y/n)?: ";
		input>>choice;
		if(choice!='y')
			break;
	}while(true);

	return input;
}
std::ostream& operator<<(std::ostream& output,Poly& the_pol){
	output<<"P(X)=";
	for(unsigned int i=0;i<the_pol.size;i++)
		(the_pol.arr[i]>=0)?output<<"+"<<the_pol.arr[i]<<"X^"<<i:output<<the_pol.arr[i]<<"X^"<<i;
	
	return output;
}
Poly operator+(const Poly& p1, const Poly& p2){
	int longest = (p1.size>p2.size)?p1.size:p2.size;
	Poly solution(longest);
	
	for(unsigned int i=0;i<longest;i++){
	
		double temp1 = (i>p1.size-1)?0.0:p1.arr[i];
		double temp2 = (i>p2.size-1)?0.0:p2.arr[i];
		solution.arr[i]=temp1+temp2;
	
		std::cout<<"Sol "<<solution.arr[i]<<",";//works here
	}
	return solution;//fails here
}

int main(void){
	char hold;

	Poly p1, p2, add;

	std::cout<<"Enter first poly\n";
	std::cin>>p1;
	std::cout<<p1<<std::endl;

	std::cout<<"Enter second poly\n";
	std::cin>>p2;
	std::cout<<p2<<std::endl;

	add=p1+p2;
	std::cout<<"Addition\n"<<add<<std::endl;


	std::cin>>hold;
	return 0;
}
Try overloading the assignment operator = also.
I thought about that and added the following...

1
2
3
4
5
6
7
8
9
10
Poly Poly::operator=(const Poly& the_poly){
	Poly sol;
	sol.size=the_poly.size;
	for(unsigned int i=0;i<sol.size;i++){
		sol.arr[i]=the_poly.arr[i];
	}
	return sol;
}



But it's not making any difference.
Your overload for assignment operator seems to be wrong.

Try
1
2
3
4
5
6
7
Poly& Poly::operator=(const Poly& the_poly){
	size=the_poly.size;
	for(unsigned int i=0;i<sol.size;i++){
		arr[i]=the_poly.arr[i];
	}
	return *this;
}
Hi abhishekm,

You are absolutely right, although I am not sure why mine was not working except of course it was not returning a reference.

I remember reading that returning *this pointer is useful for chaining for example...

a=b=c=d=10;

Thanks again,

Mike
The difference between assignment operator overloading and other operators overloading is that you have declared = operator overload as a member function, whereas all others are friend functions.

Hence you need to modify the properties of the current object in consideration referenced by *this.

If you make this as a friend function also, then perhaps (I'm not sure) your way might work.

1
2
3
4
5
6
7
8
Poly operator=(const Poly& the_poly){
	Poly sol;
	sol.size=the_poly.size;
	for(unsigned int i=0;i<sol.size;i++){
		sol.arr[i]=the_poly.arr[i];
	}
	return sol;
}

Topic archived. No new replies allowed.