Mong các bác giúp đỡ!
khi chạy đến dòng ''delete[] a '' nó văng ra và báo lỗi:
----------------------------------
Windows has triggered a breakpoint in phansotoantu.exe.
This may be due to a corruption of the heap, which indicates a bug in phansotoantu.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while phansotoantu.exe has focus.
-----------------------------------
The output window may have more diagnostic information.
em làm bài này đây .sao em cấp pháp mà đến lúc giải phóng bị lỗi là sao nhỉ!!
khi xóa ''delete[] a'' thì chương trình chạy bình thườnghic
không hiễu tại sao!!
Code:
#include"stdio.h"
#include"iostream"
#include"math.h"
#include"conio.h"
using namespace std;
class phanso
{
protected:
int ms,ts;
public:
phanso(int i=1,int j=1)
{
ts=i;ms=j;
}
void xuat()
{
cout <<" "<<ts<<"/"<<ms<<endl;
}
void nhap();
phanso &operator+(phanso &y);
friend istream &operator>>(istream &is,phanso &a)
{
is>>a.ts>>a.ms;
return is;
}
friend bool operator >(const phanso &y,const phanso &x)
{
//bool c;
float a=(float)x.ts/x.ms;
float b=(float)y.ts/y.ms;
if(b>a)
return 1 ;
else return 0;
}
void rutgon();
};
class honso :public phanso
{
private:
int song;
phanso x;
public:
honso();
honso(int j,int i,int ng);
void nhaphonso();
friend void conghaihonso(honso &a ,honso &c);
void xuathonso();
};
honso::honso()
{
song=1;
}
honso::honso(int j,int i,int ng):x(j,i)
{
song=ng;
}
void honso::nhaphonso()
{
cout<<"\n Nhap so ng , tuso, mauso : ";
cin>>song;
x.nhap();
}
void conghaihonso(honso &a ,honso &c)
{
c.song=c.song+a.song;
c.x=c.x+a.x;
c.xuathonso();
}
void honso::xuathonso()
{
cout<<"\nhon so can xuat la: "<<song <<" ";
x.xuat();
}
int UCLN(int a,int b)
{
if(a==b)
return a;
else
{
if(a>b)
return a=a-b;
else return b=b-a;
}
return 0;
}
void phanso::rutgon()
{
int x=UCLN(ts,ms);
ts=ts/x;
ms=ms/x;
}
void phanso::nhap()
{
cin >>ts>>ms;
}
phanso& phanso::operator+(phanso &y)
{
this->ts=ts*y.ms+y.ts*ms;
this->ms=ms*y.ms;
return *this;
}
int main()
{
phanso b,*a;
honso c,d;
a = new phanso[2];
for(int i =1;i<=2;++i)
{
cout <<"\n nhap phan so gom tu va mau : ";
cin >>a[i];
}
for(int j=1;j<=2;++j)
{
a[j].xuat();
}
b=a[1];
for(int j=2;j<=2;++j)
{
b=b+a[j];
}
b.xuat();
a[1].rutgon();
a[2].rutgon();
for(int j=1;j<=2;++j)
{
cout<<"\n kq la: ";
a[j].xuat();
}
if(a[1]>a[2])cout<<"\n dung ";
else cout<<"\n sai roi ";
cout<<"\n---------------------------------";
c.nhaphonso();
d.nhaphonso();
c.xuathonso();
d.xuathonso();
conghaihonso(d,c);
delete[] a;
system("pause");
}
thanks!!