原文: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. python学习笔记:字符串格式化

    % 格式化方法 "我的名字是 %s, 我的年龄是 %d" % (name, age) 常用格式:%[(name)][flags][width].[precision]typecod ...

  2. Python 中多进程、多线程、协程

    进程: 一个运行的程序(代码)就是一个进程,没有运行的代码叫程序,进程是系统资源分配的最小单位,进程拥有自己独立的内存空间,所以进程间数据不共享.开销大. 线程: 调度执行的最小单位,也叫执行路径,不 ...

  3. mybatis用mybatis-generator-core-1.3.5.jar自动生成实体类

    原文出处:https://blog.csdn.net/shuoshuo_12345/article/details/80626241,本文只是个人总结而已! 方法1:在pom文件中添加依赖 只需在搭建 ...

  4. java课极限测试

    在临近国庆的9月30号补课日 碰到了惨绝人寰的java课极限测试 我从下午两点半做到晚上九点 不得不说人在烦躁的时候是真的没办法写代码的 根本不想思考也不想学习.最后的几个小时基本就是在叹息和坐着.也 ...

  5. (转)__attribute__之section 分析详解

    原文地址:__attribute__之section详解 前言 第一次接触 "section" 是在公司的一个STM32的项目代码中,前工程师将所有的初始化函数都使用的" ...

  6. 【JS 日期】获取当前日期时间

    获取当前日期时间 <!DOCTYPE html> <html> <head></head> <body> <script> wi ...

  7. Python 基础之面向对象之八步理解装饰器

    装饰器:在不改变原有代码的情况下,为该原函数扩展新功能特征:返回新函数,替换旧函数语法:@ 语法糖 1.装饰器原型 #例1: def kuozhan(func):    def newfunc():  ...

  8. Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter

    1.DateTimeFormatter final修饰,线程安全,用于打印和解析日期-时间对象的格式化程序. 创建DateTimeFormatter: DateTimeFormatter dateTi ...

  9. teraterm中状态框statusbox

    ;Author : Bing ;Date : 1/17/2019;Usage: modify log drictory according to actual drictorylogfile=&quo ...

  10. C 语言入门第十二章---C语言文件操作

    C语言具有操作文件的能力,比如打开文件.读取和追加数据.插入和删除数据.关闭文件.删除文件等. 在操作系统中,为了同意对各种硬件的操作,简化接口,不同的硬件设备也都被看成一个文件.对这些文件的操作,等 ...