Chinaunix首页 | 论坛 | 博客
  • 博客访问: 340545
  • 博文数量: 88
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 885
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-21 14:50
文章分类

全部博文(88)

文章存档

2010年(88)

我的朋友

分类: C/C++

2010-08-10 21:55:50

#include
using namespace std;

class String
{
private:
char *m_string;
public:
String(const char *str=NULL);
String(const String &other);
~String(void);
String &operator=(const String &other);
void display()
{
cout<
}
};

String::String(const char*str)
{
cout<<"Constructing..."<
if(NULL==str)
{
m_string=new char[1];
*m_string='\0';
}
else
{
m_string=new char[strlen(str)+1];
strcpy(m_string,str);
}
}

String::~String(void)
{
cout<<"Destructing..."<
if(NULL!=m_string)
{
delete []m_string;
m_string=NULL;
}
}

String::String(const String &other)
{
cout<<"Copy Constructing.."<
m_string=new char[strlen(other.m_string)+1];
strcpy(m_string,other.m_string);
}

String & String::operator =(const String &other)
{
cout<<"Operate = Function..."<
if(&other==this)
{
return *this;
}
else
{
delete []m_string;
m_string=new char[strlen(other.m_string)+1];
strcpy(m_string,other.m_string);
return *this;
}
}

int main()
{
String string1("Hello");
string1.display();

String string2("World");
string2.display();

String string3(string1);
string3.display();

string3=string2;
string3.display();
return 0;
}
阅读(997) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~