string简单成员函数的实现
原文: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简单成员函数的实现的更多相关文章
- String 的成员函数
本篇是把一些string的成员函数的用法记录下来 size()函数和lenth()函数 s.size()或者s.lenth() 它们都会返回长度,是总长度而不是下标长度 find函数 s.find(s ...
- string字符串成员函数
string字符串成员函数 string str1="aaa"; char c='c'; str1.assign("ABCAAAAAAABBBBB");//替换 ...
- string常用成员函数
string常用成员函数 std::string::clear Clear string Erases the contents of the string, which becomes an emp ...
- C++中string的成员函数
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...
- std::string 简单入门
string的定义原型 typedef basic_string<char, char_traits<char>, allocator<char> > string ...
- C/C++ 类成员函数指针 类成员数据指针
普通函数指针: "return_type (*ptr_name)(para_types) " 类成员函数指针: "return_type (class_name::*p ...
- Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法:
Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法: ------------------------------------------------------------ ...
- list排序成员函数对string对象与char*对象排序的差别
对list容器中的对象排序,不能使用sort()算法,只能采用其自身的排序函数sort().因为,算法sort()只支持随机存取的容器的排序,如vector等. 对基本数据对象list排序:成员函数s ...
- 类 this指针 const成员函数 std::string isbn() const {return bookNo;}
转载:http://www.cnblogs.com/little-sjq/p/9fed5450f45316cf35f4b1c17f2f6361.html C++ Primer 第07章 类 7.1.2 ...
随机推荐
- POJ2516 Minimum Cost
亲爱的,一个货物销售者,现在遇到了一个大问题,他需要你的帮助.在他的销售区域有 N 个店主(从 1 到 N)向他储存货物,Dearboy 有M 个供应点(从 1 到 M),每个供应点提供 K 种不同的 ...
- ZOJ4110 Strings in the Pocket(2019浙江省赛)
给出两个字符串,询问有多少种反转方法可以使字符串1变成字符串2. 如果两个串相同,就用马拉车算法找回文串的数量~ 如果两个串不同,从前往后找第一个不同的位置l,从后往前找第二个不同的位置r,反转l和r ...
- Spring Boot 使用 Dom4j XStream 操作 Xml
Xml 现在仍然占据着比较重要的地位,比如微信接口中使用了 Xml 进行消息的定义.本章重点讨论 Xml 的新建.编辑.查找.转化,可以这么理解,本章是使用了 dom4j.xstream 也是在开发者 ...
- 怪异盒子模型和行内元素的float
设置了float属性的行内元素的display值会变成inline-block 怪异盒子模型: box-sizing:border-box:元素content包含内间距和border
- 吴裕雄 python 神经网络——TensorFlow 变量管理
import tensorflow as tf with tf.variable_scope("foo"): v = tf.get_variable("v", ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:中型和大型设备
<!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 中型和大型设备</title> &l ...
- sqlserver数据库中char、varchar、text与nchar、nvarchar、ntext数据类型使用详解
很多开发者进行数据库设计的时候往往并没有太多的考虑char, varchar类型,有的是根本就没注意,因为存储价格变得越来越便宜了,忘记了最开始的一些基本设计理论和原则,这点让我想到了现在的年轻人,大 ...
- Java 1.8 红黑树
红黑树 R-B Tree R-B Tree,全称 Red-Black Tree 又称为 红黑树,它是一种特殊的二叉查找树,红黑树的每个节点都有存储位表示节点的颜色,可以是红Red 或者 黑Black ...
- 2 Struts2的执行流程&配置文件的加载顺序
执行流程: 访问前段页面,通过url访问action 访问xml中Struts2核心过滤器,并执行一组拦截器(这组拦截器在struts-default.xml中,实现了部分功能) 通过action配置 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:引用(Blockquote)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...