I have to build a class polynom (polinom) inherited from class lista (list). I have to add , substract ,multiply , divide 2 objects from the polynom class. I have this piece of code. I don't understand why my destructors aren't working. I also have to overload the operators: +,-,<<,>> but I don't know how to.
#include <iostream>
#include <conio.h>
using namespace std;
struct nod
{float coef;
int grad;
nod* adr_urm;
};
class lista
{
public:
nod *vf,*sf;
lista();
nod* adaug(nod *&vf,nod*& sf , int gr,float cf);
void afis(nod* vz);
~lista();
};
class polinom : public lista
{public:
polinom();
~polinom();
};
void lista::afis(nod* vz)
{nod *c=vz;
cout<<"Elementele polinomului"<<endl;
int i=0;
while (c)
{if (i) cout<<"+";
cout<<c->coef<<"X^"<<c->grad;
c=c->adr_urm;
i++;
}
cout<<endl<<endl;
}
nod* lista::adaug(nod *&vf,nod*& sf ,int gr,float cf)
{ nod *c=new nod;
c->coef=cf;
c->grad=gr;
c->adr_urm=0;
if (vf==0) vf=sf=c;
else {sf->adr_urm=c;
sf=c;}
return vf;
}
lista::lista()
{vf=0;
}
polinom::polinom()
{vf=0;
}
lista::~lista()
{nod *m=vf, *m1=vf->adr_urm;
while (m1)
{delete m;
m=m1;
m1=m->adr_urm;
}
vf=0;
}
polinom::~polinom()
{nod *man=vf, *man1=vf->adr_urm;
while (man1)
{delete man;
man=man1;
man1=man->adr_urm;
}
vf=NULL;
}
int main()
{
int m,nr,nr1;
float n;
nod* vf=0 ;nod *sf;
nod* varFl=0 ;nod *varFv=0;
polinom l,v;
cout<<"Nr de elemente primul pol nr= ";
cin>>nr;
for (int i=1;i<=nr;i++)
{ cout<<"Elementul "<<i<<endl;
cout<<"Coeficientul = ";
cin>>n;
cout<<"Gradul = ";
cin>>m;
l.adaug(vf,sf,m,n);
varFl=vf;
}
l.afis(varFl);
vf=0;
cout<<"Nr de elemente al doilea pol nr= ";
cin>>nr1;
for (int j=1;j<=nr1;j++)
{cout<<"Elementul "<<j<<endl;
cout<<"Coeficientul = ";
cin>>n;
cout<<"Gradul = ";
cin>>m;
v.adaug(vf,sf,m,n);
varFv=vf;
}
v.afis(varFv);
l.~polinom();
v.~polinom();
_getch();
}