原文: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 ;
}

string简单成员函数的实现的更多相关文章

  1. String 的成员函数

    本篇是把一些string的成员函数的用法记录下来 size()函数和lenth()函数 s.size()或者s.lenth() 它们都会返回长度,是总长度而不是下标长度 find函数 s.find(s ...

  2. string字符串成员函数

    string字符串成员函数 string str1="aaa"; char c='c'; str1.assign("ABCAAAAAAABBBBB");//替换 ...

  3. string常用成员函数

    string常用成员函数 std::string::clear Clear string Erases the contents of the string, which becomes an emp ...

  4. C++中string的成员函数

    string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...

  5. std::string 简单入门

    string的定义原型 typedef basic_string<char, char_traits<char>, allocator<char> > string ...

  6. C/C++ 类成员函数指针 类成员数据指针

    普通函数指针:  "return_type (*ptr_name)(para_types) " 类成员函数指针: "return_type (class_name::*p ...

  7. Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法:

    Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法: ------------------------------------------------------------ ...

  8. list排序成员函数对string对象与char*对象排序的差别

    对list容器中的对象排序,不能使用sort()算法,只能采用其自身的排序函数sort().因为,算法sort()只支持随机存取的容器的排序,如vector等. 对基本数据对象list排序:成员函数s ...

  9. 类 this指针 const成员函数 std::string isbn() const {return bookNo;}

    转载:http://www.cnblogs.com/little-sjq/p/9fed5450f45316cf35f4b1c17f2f6361.html C++ Primer 第07章 类 7.1.2 ...

随机推荐

  1. 【代码学习】PYHTON 元组

    Python的元组与列表类似,不同之处在于元组的元素不能修改.也可进行分片 和 连接操作. 元组使用小括号,列表使用方括号. 一.访问元组 #coding=utf-8 Tuple = ('name', ...

  2. Nexus升级、license安装和恢复密码

    原文链接:https://blog.csdn.net/ligang636/article/details/42386639 一.Nexus系列物理硬件1.1 Nexus 7010 1.2 Nexus ...

  3. XCOJ1250: 语言战争

    1250: 语言战争 时间限制: 1 Sec  内存限制: 64 MB提交: 203  解决: 46 标签提交统计讨论版 题目描述 llc和yrc语言的优劣一直都是大家所争论的焦点,但它们之间最大的区 ...

  4. Priority Queue(优先队列)

    今天早上起来完成了一个完整的基于二叉堆实现的优先队列,其中包含最小优先和最大优先队列. 上篇说了优先队列的特性,通过建堆和堆排序操作,我们就已经看到了这种数据结构中的数据具有某种优先级别,要么非根节点 ...

  5. vue.js 第十课-第十六课

    第十课: http://note.youdao.com/noteshare?id=25b5ba45286464856f21eb4b6b391ecd&sub=19C4429995384F72BD ...

  6. MyBatis映射器(转载)

    什么是MyBatis映射器? MyBatis框架包括两种类型的XML文件,一类是配置文件,即mybatis-config.xml,另外一类是映射文件,例如XXXMapper.xml等.在MyBatis ...

  7. Spring中@MapperScan注解

    之前是,直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦. 通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,比如: @Sp ...

  8. MySQL - 设置UTF-8编码

    1. 在Windows上,安装时请选择UTF-8编码,以便正确地处理中文. 在Mac或Linux上,需要编辑MySQL的配置文件,把数据库默认的编码全部改为UTF-8.MySQL的配置文件默认存放在/ ...

  9. 20 个新的且值得关注的 Vue 开源项目

    译者:前端小智作者:Nastassia Ovchinnikova来源:flatlogic.com 个人专栏 ES6 深入浅出已上线,深入ES6 ,通过案例学习掌握 ES6 中新特性一些使用技巧及原理, ...

  10. Vue中全局监听键盘事件

    全局监听enter键,是把监听事件绑定到document上 常用的keyCode键盘编码在这里:https://www.cnblogs.com/wbyixx/p/12029508.html creat ...