博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现string
阅读量:4116 次
发布时间:2019-05-25

本文共 2284 字,大约阅读时间需要 7 分钟。

//实现string类,构造函数,析构函数、拷贝构造函数、赋值运算符、重载 + 、 ==#include "my_string.h"#include
using namespace std;MyString::MyString()//默认构造函数 //:s(new char(1)) //, length(1) :s(NULL) , length(0){ }MyString::MyString(const char* str)//构造函数{ if (str == NULL) { return; } length = strlen(str); s = new char[length + 1]; memset(s, 0, length + 1); strcpy(s, str);}MyString::MyString(const MyString& str1)//拷贝构造函数 :s(NULL) , length(0){ if (0 == str1.length) { return; } length = strlen(str1.s); s = new char[length + 1]; strcpy(s, str1.s);}MyString::~MyString(){ if (NULL != s) { delete[] s; s = NULL; }}MyString& MyString::operator=(const MyString& str1){ if (0 == strlen(str1.s)) { exit(-1); } int len_str1 = std::strlen(str1.s); if (this == &str1) { return *this; } if (length < len_str1) { delete[] s; s = NULL; length = len_str1 + 1; s = new char[length]; memset(s, 0, length); strcpy(s, str1.s); } else { memset(s, 0, length); strcpy(s, str1.s); } return *this;}MyString operator + (const MyString& str1, const MyString& str2){ if ((NULL == &str1) && (NULL == &str2)) { return NULL; } MyString total;//默认构造函数 int len_str1 = std::strlen(str1.s); int len_str2 = std::strlen(str2.s); total.s = new char[len_str1 + len_str2 + 2]; memset(total.s, 0, len_str1 + len_str2 + 2); strcpy(total.s, str1.s); strcat(total.s, str2.s); return total;//拷贝构造,默认构造,拷贝构造(重新拷贝一个中间的对象),析构函数(释放total)}bool operator==(const MyString& str1, const MyString& str2){ int ret = strcmp(str1.s, str2.s); if (0 == ret) { return true; } else { return false; }}int main(){ MyString str1;//默认构造函数 MyString str2("abc");//构造函数,默认构造函数 MyString str3(str2);//拷贝构造函数,默认构造函数 str1 = str2;//赋值 MyString str4("hello");//构造函数,默认构造函数 MyString str5("word");//构造函数,默认构造函数 MyString total = str4 + str5;//重载加 bool Isequal(str1 == str4);//重载 return 0;//6次析构}
#ifndef _MY_STRING_H_#define _MY_STRING_H_#include 
#include
#include
class MyString{private: char* s = NULL; int length; public: MyString();//默认构造函数 MyString(const char* str);//构造函数,赋给成员的值 MyString(const MyString& str1);//拷贝构造函数 ~MyString();//析构函数 MyString& operator=(const MyString& str1); friend MyString operator+(const MyString& str1, const MyString& str2); friend bool operator==(const MyString& str1, const MyString& str2);};#endif

 

转载地址:http://ulypi.baihongyu.com/

你可能感兴趣的文章
对话周鸿袆:从程序员创业谈起
查看>>
web.py 0.3 新手指南 - 如何用Gmail发送邮件
查看>>
web.py 0.3 新手指南 - RESTful doctesting using app.request
查看>>
web.py 0.3 新手指南 - 使用db.query进行高级数据库查询
查看>>
web.py 0.3 新手指南 - 多数据库使用
查看>>
一步步开发 Spring MVC 应用
查看>>
python: extend (扩展) 与 append (追加) 的差别
查看>>
「译」在 python 中,如果 x 是 list,为什么 x += "ha" 可以运行,而 x = x + "ha" 却抛出异常呢?...
查看>>
浅谈JavaScript的语言特性
查看>>
LeetCode第39题思悟——组合总和(combination-sum)
查看>>
LeetCode第43题思悟——字符串相乘(multiply-strings)
查看>>
LeetCode第44题思悟——通配符匹配(wildcard-matching)
查看>>
LeetCode第45题思悟——跳跃游戏(jump-game-ii)
查看>>
LeetCode第46题思悟——全排列(permutations)
查看>>
LeetCode第47题思悟—— 全排列 II(permutations-ii)
查看>>
LeetCode第48题思悟——旋转图像(rotate-image)
查看>>
驱动力3.0,动力全开~
查看>>
记CSDN访问量10万+
查看>>
Linux下Oracle数据库账户被锁:the account is locked问题的解决
查看>>
记CSDN访问20万+
查看>>