|
以下是引用heliujin在2006-7-28 9:03:03的发言:
程序如下:
#include<iostream.h>
#include<string.h>
class pica
{
public:
pica()
{
mz=new char[100];
zl=0;
}
pica(char *p,int z):zl(z)
{
mz=new char[zl];
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;
delete p;
}
for(int j=0;j<b;j++)
{
cout<<gaga[j]<<endl;
}
delete []gaga;
} |
|