Hello! I have a code:
[CODE=C++]
// 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::Pol ynomial()
{
this->n = 0;
this->p = NULL;
}
Polynomial::Pol ynomial(int n1)
{
this->n = n1;
this->p = new double[n1+1];
if(NULL == p)
{
cout << "Not enough memory.";
exit(1);
}
}
Polynomial::~Po lynomial()
{
this->n = 0;
delete this->p;
}
ostream& operator<< (ostream& os, const Polynomial& pol)
{
os << "Coefficien ts 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 = new double[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::ope rator+ (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];
else if(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;
}
[/CODE]
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?
[CODE=C++]
// 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::Pol ynomial()
{
this->n = 0;
this->p = NULL;
}
Polynomial::Pol ynomial(int n1)
{
this->n = n1;
this->p = new double[n1+1];
if(NULL == p)
{
cout << "Not enough memory.";
exit(1);
}
}
Polynomial::~Po lynomial()
{
this->n = 0;
delete this->p;
}
ostream& operator<< (ostream& os, const Polynomial& pol)
{
os << "Coefficien ts 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 = new double[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::ope rator+ (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];
else if(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;
}
[/CODE]
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?
Comment