// class Polynomial
// Constructor, Destructor
// Friend Function, Operator Overloading
#include <iostream.h>
class Polynomial
{
private:
int n; // Degree of polynomial
double* p; // Pointer to array contains
// coefficients of polynomial
public:
Polynomial();
Polynomial(int n1);
~Polynomial();
friend ostream& operator<< (ostream& os, const Polynomial& pol);
friend istream& operator>> (istream& is, Polynomial& pol);
// plus two polynomials
Polynomial operator+ (const Polynomial& daThuc2);
};
Polynomial::Polynomial()
{
this->n = 0;
this->p = NULL;
}
Polynomial::Polynomial(int n1)
{
this->n = n1;
this->p = newdouble[n1+1];
if(NULL == p)
{
cout << "Not enough memory.";
exit(1);
}
}
Polynomial::~Polynomial()
{
this->n = 0;
deletethis->p;
}
ostream& operator<< (ostream& os, const Polynomial& pol)
{
os << "Coefficients of the polynomial( from ao ): ";
for(int i = 0; i <= pol.n; ++i)
os << pol.p[i] << " ";
os << endl;
return os;
}
istream& operator>> (istream& is, Polynomial& pol)
{
cout << "Enter degree of polynomial: ";
is >> pol.n;
pol.p = newdouble[pol.n+1];
cout << "Enter coefficients of the polynomial( from ao ): ";
for(int i = 0; i <= pol.n; ++i)
is >> pol.p[i];
return is;
}
Polynomial Polynomial::operator+ (const Polynomial& pol2)
{
int n1 = n > pol2.n ? n : pol2.n;
Polynomial polSum(n1);
int i = 0;
while(i <= n1)
{
if(i <= n && i <= pol2.n)
polSum.p[i] = p[i] + pol2.p[i];
elseif(i <= n)
polSum.p[i] = p[i];
else
polSum.p[i] = pol2.p[i];
++i;
}
i = n1;
while(i > 0 && 0 == polSum.p[i])
--i;
polSum.n = i;
return polSum;
}
int main()
{
Polynomial f, p, q;
cout << "Enter polynomial p:\n";
cin >> p;
cout << "Enter polynomial q:\n";
cin >> q;
f = p+q;
cout << "Polynomial f:\n";
cout << f;
return 0;
}
Why does this code run normally?
I think when method "operator+" finishes, the memory which is allocated to polSum will be deallocated by the destructor automatically.
But in this case when I try to compile it, it runs normally. The polynomial f is equal to sum of two polynomial p and q.
In addition, why if I try to change type of p( pointer to array contains coefficients of polynomial ) to int, the result become wrong?
I am expecting the results to be undefined since you don't provide a copy constructor.
There are several issues I see with this code:
1. No copy constructor or assignment operator provided, and the default one is insufficient.
2. Memory leak if operator>> is called more than once, or even just once if the non-default constructor was used.
3. Line 35 will never be true since new throws on allocation failure.
4. operator+ should be declared const.
@dionisis:
For example, assume after we plus two polynomial we have a polynomial such as following:
0x^4 + 0x^3 + 2x^2 + x + 1
The degree of this polynomial is equal to n1( 4 ). But actually, the degree is only 2.
That piece of code solve this problem.
@jsmith:
I want to say after finishing operator+, the memory which is allocated for polSum is deallocated. So what does this method return? And why is the result exact?
i think i know the answer.
First of all the result of operator + is stored in p.Correct me if i'm wrong
Second when code reaches the assignment
f = p+q;
the default assignment operator is used.This operator does a 'shallow' copy which mean that it doesn't allocate memory for the p member of the polynomial so p and f share the same p (i mean the same space) thus if one was to be deleted for instance polynomial p that would result in the destruction of p data member.But because p and f polynomials share the same pf.p would be destructed too.
In your code none of the polynomials is being destroyed during the execution of int main() that's why it runs normally.
Well that's my answer.I would like to hear your opinions about it!
@dionisis:
Wrong. The result of operator+ is being stored in a temporary. A copy of the temporary is returned.
It is working by luck because of the shallow copy. When memory is freed, it is not typically zeroed or modified in
any way until something else allocates that exact memory and writes to it. There is essentially a dangling pointer
in the code thanks to the shallow copy. This dangling pointer refers to the freed memory, which used to contain
the coefficients. Nothing allocated that memory and overwrote it (by luck), so it appears to output the "correct"
result.
that copy you say isn't it stored in object p?
I'm saying that because f = p + n; is equal to f = p.operator + (n);
so p object invoke operator +() method and thus the return polSum; goes to p.
@everyone: Thank you very much.
Now I think I understood.
f = p+q;
First, compiler will creat a copy of polSum( assume it is called "temp" ) by copy constructor and then return it by return statement ( at this time, polSum will be destructed ).
After that, "temp" is assigned to f by assignment operator and "temp" will be destructed.
So we need to implement copy constructor and assignment operator to ensure result is correct.
Maybe we don't need to implement assignment operator, but in main() function we need to change:
1 2
// Assume we declared p, q, f and input data to p, q
f = p+q;