程序如下:
#include<iostream.h>
#include<string.h>
class pica
{
public:
pica()
{
mz=new char[100];
zl=0;
}
pica(char *p,int z):zl(z)
{
strcpy(mz,p);
}
~pica()
{
delete []mz;
}
pica(const pica &other)
{
mz=new char[strlen(other.mz)+1];
strcpy(mz,other.mz);
zl=other.zl;
}
pica& operator =(const pica &other)
{
mz=new char[strlen(other.mz)+1];
strcpy(mz,other.mz);
zl=other.zl;
return *this;
}
friend ostream& operator <<(ostream &out,const pica &other)
{
out<<other.mz;
out<<other.zl;
return out;
}
friend istream& operator >>(istream &in,pica &other)
{
in>>other.mz;
in>>other.zl;
return in;
}
private:
char *mz;
int zl;
};
void main()
{
int b;
cout<<"请输入要查询几辆车"<<endl;
cin>>b;
pica* gaga=new pica[b];
for(int i=0;i<b;i++)
{
char *p=NULL;
p=new char[100];
cout<<"输入生产商"<<endl;
cin>>p;
cout<<"输入制造年份"<<endl;
int m;
cin>>m;
pica a(p,m);
gaga[i]=a;
}
for(int j=0;j<b;j++)
{
cout<<gaga[j]<<endl;
}
delete []gaga;
}
为什么运行事出错啊 一直找不到原因 我合计是NEW 和DELETE的问题 但是看不出来 希望大家指点指点吧