原文:https://blog.csdn.net/zcyzsy/article/details/52146124
#include<iostream>
using namespace std;
class String
{
public:
String(const char* str=NULL); //普通构造函数
Strng(const String &other); //拷贝构造函数
String & operator=(const String &other); //赋值函数
~String(void); //析构函数
public:
char& operator[];
bool operator==(const String& s1,const String& s2 );
String & operator+=(const String &str); //本身相加,返回this
String & operator+(const String &s1,const String &s2) //两个String相加
friend ostream& operator<<(ostream&out,String& s);
friend istream& operator>>(iostream&in,String& s);
private:
char *m_string ; //保存字符串
};
String::~String(void)
{
cout<<"destrcut"<<endl;
if(m_string!=NULL) //不为空,就释放内存
{
delete [] m_string;
m_string = NULL;
}
} String::String(const char* str) //普通构造函数
{
cout<<construct<<endl;
if(str==NULL) //如果str 为NULL,就存一个空字符串“”
{
m_string=new char[];
*m_string ='\0';
}
else
{
m_string = new char[strlen(str)+] ; //分配空间
strcpy(m_string,str);
}
} String::String(const String& other) //拷贝构造函数
{
cout<<"copy construct"<<endl;
m_string=new char[strlen(other.m_string)+] ; //分配空间并拷贝
strcpy(m_string,other.m_string);
} char &String::operator[](int index)
{
return m_string[index];
} bool operator==(const String& s1,const String& s2 )
{
if(strcmp(s1.m_string,s2.m_string)==)
return ture;
return false;
} String & String::operator=(const String & other)//重载‘+’运算符
{
cout<<"operator = funtion"<<endl ;
if(this==&other) //如果对象和other是用一个对象,直接返回本身
{
return *this;
}
delete []m_string; //先释放原来的内存
m_string = new char[strlen(other.m_string)+];
strcpy(m_string,other.m_string);
return * this;
} String & String::operator+=(const String & str)
{
char * temp=m_string;
m_string=new char[strlen(m_string)+strlen(str.m_string)+];
strcpy(m_string,temp);
delete[]temp;
strcat(m_string,str.m_string);
return *this;
}
String & String::operator+(const String & s1,const String &s2)
{
String res;
res.m_string=new char[strlen(s1.m_string)+strlen(s2.m_string)+];
strcpy(res.m_string,s1.m_string);
strcat(res.m_string,s2.m_string);
return res;
}
ostream& operator<<(ostream& out,String& s)
{
for(int i=;i<s.length();i++)
out<<s[i]<<"";
return out;
}
istream& operator>>(istream& in,String& s)
{
char p[];
in.getline(p,);
s=p;
return in;
}
int main()
{
String a("hello"); //调用普通构造函数
String b("world"); //调用普通构造函数
String c(a); //调用拷贝构造函数
c=b; //调用赋值函数
return ;
}

最新文章

  1. nandflash驱动程序编写
  2. struts2中Ajax校验
  3. Go 语言的基本数据类型
  4. Linux find 用法示例
  5. 初识django
  6. Java基础学习-- 继承 的简单总结
  7. 龙威零式_团队项目例会记录_18 (Beta架构讨论)
  8. eclipse字体推荐
  9. ASP.NET之Ajax系列(一)
  10. 测试方法---&quot;SFDIPOT&quot;
  11. Delphi笔记(GL_Scene四轴飞行器模型)
  12. 用CSS3写的钟表
  13. Android Notivation的使用
  14. HTML5 拖拽效果实现
  15. [nodejs,expressjs,angularjs2] LOL英雄列表数据抓取及查询显示应用
  16. Xcode 中armv6 armv7 armv7s arm64 i386 x86_64 归纳 (Architectures, Valid Architectures, Build Active Architecture Only)
  17. StackService.Redis 应用
  18. css 位置居中篇,flex布局【转】
  19. 20170906xlVBA_RecursionGetFiles
  20. WebGL和ThreeJs学习5--ThreeJS基本功能控件

热门文章

  1. python 语法-参数注释
  2. ASA-ACL类型
  3. 【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
  4. 安装oracle11g时出现:在注册表中没有找到指定的主目录名
  5. 小程序使用scroll-view横向滑动时,flex布局失效问题
  6. 吴裕雄--天生自然PythonDjangoWeb企业开发:解决Pythonno module named &quot;XX&quot;问题
  7. RedHat OpenShift QuickStart 1.2
  8. Java 并发锁
  9. Windows驱动开发-手动创建IRP
  10. ApacheDbUtilsUpdate