本文主要是介绍C++ 基本MyString实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
介绍
MyString重载了+,+=,=,D,<<,>>.length,MyStrlen,MyStrcpy,MyStrcat的功能
源码
main.cpp
#include<iostream>
#include "MyString.h"using namespace std;void main()
{//构造测试MyString s;cout << s;MyString s1 = "一航";cout << s1 << endl;// + 运算符重载测试MyString str = "str";MyString str2 = "str2";str + str2;cout << str << endl;cout << str2 << endl;// += 运算符重载测试MyString temp;temp +=str2;cout << temp << endl;system("pause");
}
MyString.h
#pragma once
#include<iostream>using namespace std;
class MyString
{friend ostream& operator<<(ostream& out, MyString& data);friend istream& operator>>(istream& in, MyString& data);
public://返回字符串大小size_t length();//获取字符串长度size_t MyStrlen(const char* str);//拷贝字符串char MyStrcpy(char *dest, const char *src);
public:// + 运算符重载const MyString operator +(const MyString& Right)const;// = 运算符重载MyString& operator=(const MyString& Right);// += 运算符重载MyString& operator+=(const MyString& Right);// [] 运算符重载const char& operator[](unsigned int index) const;
public://构造函数MyString();//带参构造MyString(const char* str);//拷贝赋值构造MyString(const MyString&that);//析构函数~MyString();
private:char *m_str;//数组存储size_t m_size;//数组大小
};
/**********************************
PS: 输入 输出 的重载需要带下列代码
#include<iostream>
using namespace std;
**********************************/
// << 运算符重载
ostream& operator<<(ostream& out, MyString& data);
// >> 运算符重载
istream& operator>>(istream& in, MyString& data);
//末尾处追加字符串
char MyStrcat(char *dest, const char *src);
MyString.cpp
#include "MyString.h"
#include<iostream>using namespace std;//返回字符串大小
size_t MyString::length()
{return m_size;
}
//获取字符串长度
size_t MyString::MyStrlen(const char* str)
{size_t temp = 0;while (str[temp] != '\0'){temp++;}return temp;
}
//拷贝字符串
char MyString::MyStrcpy(char *dest, const char *src)
{//判断数组是否为空if (dest == NULL || src == NULL)return NULL;//copy *str2 到 *str1,采用单字符逐个拷贝while ((*dest++ = *src++) != '\0');//返回一个数组地址return *dest;
}
//末尾处追加字符串
char MyStrcat(char *dest, const char * src)
{//判断数组是否为空if (dest == NULL || src == NULL)return NULL;//第一步:先遍历str1到末尾while (*dest != '\0')*dest++;//第二步:将str2中的字符逐个拷贝到str1while ((*dest++ = *src++) != '\0');//第三步:返回str1一个数组地址return *dest;
}
//构造函数(构造函数无法实现缺省重载)
MyString::MyString() :m_size(0)//分配一个10字节大小的空间
{m_str = new char[m_size + 1];strcpy(m_str, "");
}
//带参构造
MyString::MyString(const char* str)
{if (str == nullptr)//如果为空则赋值一个{m_size = 0;m_str = new char[m_size];MyStrcpy(m_str, "");}else//如果不为空则copy过来{m_size = MyStrlen(str);//获取内存大小m_str = new char[m_size];//new一个新的内存空间MyStrcpy(m_str, str);//copy str中内容}
}
//拷贝构造函数
MyString::MyString(const MyString&that)
{//cout << "拷贝构造函数" << endl;m_str = new char[that.m_size + 1];//创建一个同样大小的空间MyStrcpy(m_str, that.m_str);//拷贝字符串m_size = that.m_size;//获取空间大小
}
//析构函数
MyString::~MyString()
{//cout << "析构函数" << endl;//销毁动态数组if (m_str){delete[] m_str;m_str = NULL;}
}// + 运算符重载
const MyString MyString::operator +(const MyString& Right)const
{//构造一个临时变量MyString temp;//new 一个同样大小的空间temp.m_size = this->m_size + Right.m_size;temp.m_str = new char[temp.m_size + 1];*temp.m_str = '\0';MyStrcat(temp.m_str, this->m_str);//追加temp.m_str字符串的结尾MyStrcat(temp.m_str, Right.m_str);//追加temp.m_str字符串的结尾return temp;
}
// = 运算符重载
MyString& MyString::operator=(const MyString&that)
{//基本方法拷贝赋值if (&that != this)//判断是否重复拷贝赋值自己{//防止new失败 先用临时变量来存储char*ptemp = new char[that.m_size];if (ptemp == nullptr)//new失败后的处理方式{cerr << "Bad Allocation" << endl;return *this;}MyStrcpy(ptemp, that.m_str);//拷贝字符串if (m_str)//判断是否为空,释放旧资源{delete[] m_str;m_str = NULL;}swap(ptemp, m_str);//如果new成功则交换数据delete[]ptemp;//以防万一手动释放一遍m_size = that.m_size;//获取空间大小}return *this;return *this;//返回操作对象
}
// += 运算符重载
MyString& MyString::operator+=(const MyString& Right)
{*this = *this + Right;return *this;
}
// [] 运算符重载
const char& MyString::operator[](unsigned int index) const
{if (index >= 0 && index < this->m_size)//判断是否超过下标{return m_str[index];}else{cout << "访问失败,没有此数据" << endl;return 0;}
}
// >> 运算符重载
ostream& operator<<(ostream& out, MyString& data)
{for (int i = 0; i < data.m_size; i++){//输出out << data.m_str[i];}out << endl;return out;
}
// >> 运算符重载
istream& operator>>(istream& in, MyString& data)
{for (int i = 0; i < data.m_size; i++){//输入in >> data.m_str[i];}return in;
}
运行结果
一航strstr2str2请按任意键继续. . .
这篇关于C++ 基本MyString实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!