65,210
社区成员
发帖
与我相关
我的任务
分享
class Cstring
{
private:
char* ps;
public:
//构造函数
Cstring(const char* s = "")
{
int max = strlen(s) + 1;
ps = new char[max];
strcpy_s(ps, max, s);
}
//复制构造函数
Cstring(const Cstring* cs)
{
int max = strlen(cs->ps) + 1;
ps = new char[max];
strcpy_s(ps, max, cs->ps);
}
//析构函数
~Cstring()
{
delete [] ps;
}
void show() const
{
printf("%s\n", ps);
}
};
int main(void)
{
Cstring asdf(new Cstring("asdf"));
asdf.show();
return 0;
}
#include <iostream>
//
using namespace std;
class Cstring
{
private:
char* ps;
public:
//构造函数
Cstring(const char* s = "")
{
int max = strlen(s) + 1;
ps = new char[max];
strcpy_s(ps, max, s);
}
//复制构造函数
Cstring(const Cstring* cs)
{
int max = strlen(cs->ps) + 1;
ps = new char[max];
strcpy_s(ps, max, cs->ps);
}
//析构函数
~Cstring()
{
delete [] ps;
}
void show() const
{
printf("%s\n", ps);
}
};
int main(void)
{
Cstring* a=new Cstring("asdf");
Cstring asdf(a);
asdf.show();
delete a;
return 0;
}