#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyString
{
public:
MyString::MyString();         //无参构造
MyString(const char* str );     //默认参数
MyString(const MyString& other);   //拷贝构造
MyString& operator=(const MyString& other);  //重载等号(参数不同)
MyString& operator=(const char* str);      //重载等号(参数不同)
~MyString();              //析构函数
char& operator[](unsigned int index);      //重载[]号 MyString& operator+=(const MyString& other);  //重载+=号
friend MyString operator+(const MyString& s1, const MyString& s2);  //重载加号;用全局的友元函数
friend ostream& operator<<(ostream& os, const MyString& str);      //重载左移操作符;用全局函数
  
friend istream& operator>>(istream& is, MyString& str);          //重载右移操作符,用全局函数
private:
char* m_str;
}; MyString::MyString()
{
m_str = new char[];
m_str = '\0';
} MyString::MyString(const char* str) //默认参数
{
if (str == NULL)
{
m_str = new char[];
m_str = '\0'; }
else
{
int len = strlen(str);
m_str = new char[len + ];
strcpy(m_str, str);
}
} MyString::MyString(const MyString& other)
{
int len = strlen(other.m_str);
m_str = new char[len + ];
strcpy(m_str, other.m_str);
} MyString& MyString::operator=(const MyString& other)
{
if (&other == this)
return *this;
if (m_str != NULL)
{
delete[]m_str;
} int len = strlen(other.m_str);
m_str = new char[len + ];
strcpy(m_str, other.m_str);
return *this;
} MyString& MyString::operator=(const char* str)
{
if (m_str != NULL)
{
delete[]m_str;
} if (str == NULL)
{
m_str = new char[];
m_str = '\0';
}
else
{
int len = strlen(str);
m_str = new char[len + ];
strcpy(m_str, str);
} return *this;
}
MyString::~MyString()
{
if (m_str != NULL)
{
delete[]m_str;
m_str = NULL;
}
}
char& MyString::operator[](unsigned int index)
{
unsigned int len = strlen(m_str);
if (index > len)
return m_str[len]; return m_str[index];
} MyString& MyString::operator+=(const MyString& other)
{
int len = strlen(m_str) + strlen(other.m_str);
char *newstr = new char[len + ];
strcpy(newstr, m_str);
strcat(newstr, other.m_str); if (m_str != NULL)
{
delete[]m_str;
}
m_str = newstr; return *this;
}
MyString operator+(const MyString& s1, const MyString& s2)
{
int len = strlen(s1.m_str) + strlen(s2.m_str);
char *newstr = new char[len + ];
strcpy(newstr, s1.m_str);
strcat(newstr, s2.m_str); MyString My(newstr); return My;
}
ostream& operator<<(ostream& os, const MyString& str)
{
os << str.m_str << endl;
return os;
}
istream& operator>>(istream& is, MyString& str)
{
char newstr[];
is >> newstr;
int len = strlen(newstr);
if (str.m_str != NULL)
{
delete []str.m_str;
}
str.m_str = new char[len + ];
strcpy(str.m_str, newstr); return is;
} //测试代码
void test11()
{
MyString m1;
MyString m2("nihao");
MyString m3(m2);
cout << m2;
cout << m3;
m1 = m2 + m3;
cout << m1;
m1 += m3;
cout << m1[-] << endl;
cin >> m3;
cout << m3; } int main()
{ test11(); system("pause");
return EXIT_SUCCESS;
}

自实现部分string类的功能的更多相关文章

  1. String类常用功能

    String类常用功能 判断: boolean equals(Object obj) boolean equalsIgnoreCase(String str) //忽略大小写 boolean star ...

  2. String类的功能

    String类              标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...

  3. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  4. 关于如何来构造一个String类

    今天帮着一位大二的学弟写了一个String的类,后来一想这个技术点,也许不是什么难点,但是还是简单的记录一些吧! 为那些还在路上爬行的行者,剖析一些基本的实现..... 内容写的过于简单,没有涉及到其 ...

  5. VC++ 标准C++中的string类的用法总结

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...

  6. c++中string类的详解

    ,<时返回-1,==时返回0  string的子串:string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串strin ...

  7. 标准C++中string类的用法

    转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...

  8. 标准C++中的string类的用法总结(转)

    http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的 ...

  9. C++中的string类(2)

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...

随机推荐

  1. hibernate双向ManyToMany映射

    工作需要一个双向多对多映射,照着李刚的书做了映射,碰到了一些问题,现就问题及解决方案进行总结归纳. 1.首先奉上最初代码 Person5.java @Entity @Table(name = &quo ...

  2. [Luogu1119]采蘑菇

    题目大意: 给你一个无向图,点i在时间t[i]之前是不存在的,有q组询问,问你时间为t时从x到y的最短路. 点的编号按出现的时间顺序给出,询问也按照时间顺序给出. 思路: Floyd. Floyd的本 ...

  3. CDOJ 1280 772002画马尾 每周一题 div1 矩阵快速幂 中二版

    "问题:众所周知772002很喜欢马尾,所以他决定画几幅马尾送给他的女朋友. 772002会画m种马尾,772002还有n张纸,n张纸分别编号1到n,每张纸上只能画一种马尾. 然而77200 ...

  4. VS 2003

    isreset /stop net user ASPNET /delete aspnet_regiis -i iisreset /start

  5. linux 端口占用查看 netstat -tunpl | grep 6379

    端口占用查看 netstat -tunpl | grep 6379 netstat -luntpu|grep fdfs

  6. DELPHI开发LINUX桌面程序

    DELPHI开发LINUX桌面程序 DELPHI官方目前为止尚不能开发LINUX桌面程序. 但三方控件FmxLinux(商业控件)是可以的.网上有破解版本.

  7. unity3d-地图制作之云彩飘动

    首先,我先声明,关于美工制作我是一点都不了解,甚至基本上没接触过,所以今天开始我就兼并这美工加程序来学习. 当然,我只是对unity中的美术比较感兴趣而已,仅此而已. 所以,如果我在文章中讲的不对的地 ...

  8. java反序列号漏洞分析资料汇总,待研究

    https://blog.chaitin.cn/2015-11-11_java_unserialize_rce/ http://blog.nsfocus.net/?s=fastjson http:// ...

  9. (转载)如何学好iphone游戏开发

    转自:http://www.cnblogs.com/zilongshanren/archive/2011/09/19/2181558.html 自从发布<如何学习iphone游戏开发>到 ...

  10. (转)Linux下内存映射文件的用法简介

    简介: 内存映射文件与虚拟内存有些类似,通过内存映射文件可以保留一个地址空间的区域,同时将物理存储器提交给此区域,只是内存文件映射的物理存储器来自一个已经存在于磁盘上的文件,而非系统的页文件,而且在对 ...