64,642
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
class String
{
public:
String(char *p)
{
ch = new char[strlen(p) + 1];
strcpy(ch, p);
cout<<"initialization constructor is called\n";
}
String()
{
char *s = "abcd";
ch = new char[strlen(s) + 1];
strcpy(ch, s);
cout<<"constructor called"<<endl;
}
~String()
{
delete ch;
cout<<"destructor is called\n"<<endl;
}
private:
char *ch;
};
int main(int argc, char* argv[])
{
String *a = new String();
String *b = new String("hello, world!");
delete a;
delete b;
return 0;
}
#include<iostream>
using namespace std;
class String
{
public:
String(char *p){ch=p;cout<<"initialization constructor is called\n";};
String(){char tmp[4]={'a','b','c','d'};ch=tmp;cout<<"constructor called"<<endl;}
~String(){cout<<"destructor is called\n"<<endl;};
private:
char *ch;
};
int main()
{
String *q=new String;
delete q;
getchar();
return 0;
}