功能
- 无参构造字符串
- 有参构造字符串
- 拷贝构造字符串
- =运算符重载,字符串之间进行赋值
- []运算符重载,通过下标寻找字符
- +运算符重载,字符串之间进行拼接
- 自定义追加成员函数,在原字符串上追加其他字符串
- get函数,返回私有属性指针
- 析构函数,先清理内存空间,后释放内存
- <<运算符重载,获取指向字符串首地址的指针输出字符串
抽象类简介
属性:
char* m_data;
指向字符串首地址的指针
成员函数:
- 无参构造函数:
MyString()
{
this->m_data=new char[1]();
this->m_data[0]='\0';
cout << "mystring void structure" << endl;
}
- 有参构造函数(在c++中字符串一定是常量类型,相当于c语言中的常量区):
MyString(const char* c_str)
{
int len=strlen(c_str);
this->m_data=new char[len+1];
memmove(this->m_data,c_str,len);
this->m_data[len]='\0';
cout << "mystring exit structure" << endl;
}
- 拷贝构造函数:
MyString(const MyString& other)
{
int len=strlen(other.m_data);
this->m_data=new char[len+1];
memmove(this->m_data,other.m_data,len);
this->m_data[len]='\0';
cout << "mystring copy function" << endl;
}
- =运算符重载函数
MyString& operator=(const MyString& other)
{
cout << "mystring operator= function" << endl;
if(this==&other){
return *this;
}
int len=strlen(other.m_data);
if(nullptr!=this->m_data)
{
delete [] m_data;
}
this->m_data=new char[len+1]();
memmove(this->m_data,other.m_data,len);
this->m_data[len]='\0';
return *this;
}
- []运算符重载函数
char operator[](int index)
{
cout << "mystring operator[] function" << endl;
if(index<0||index>=strlen(this->m_data))
{
cout << "cross the border" << endl;
}
return this->m_data[index];
}
- +运算符重载函数
MyString operator+(const MyString& other)
{
cout << "mystring operator+ function" << endl;
int my_len=strlen(this->m_data);
int other_len=strlen(other.m_data);
char* temp=new char[my_len+other_len+1]();
memmove(temp,this->m_data,my_len);
memmove(temp+my_len,other.m_data,other_len);
temp[my_len+other_len]='\0';
MyString temp_str=temp;
delete [] temp;
return temp_str;
}
- 自定义append追加成员函数
MyString& append(const MyString& other)
{
cout << "mystring append function" << endl;
int my_len=strlen(this->m_data);
int other_len=strlen(other.m_data);
char* temp=new char[my_len]();
memmove(temp,this->m_data,my_len);
delete []this->m_data;
this->m_data=new char[my_len+other_len+1]();
memmove(m_data,temp,my_len);
delete [] temp;
memmove(m_data+my_len,other.m_data,other_len);
this->m_data[my_len+other_len+1]='\0';
return *this;
}
- get函数
char* get_my_data()const
{
return this->m_data;
}
- 析构函数
~MyString()
{
delete []m_data;
this->m_data=nullptr;
cout << "mystring destruct" << endl;
}
>>
运算符重载函数
ostream& operator << (ostream& cout,const MyString& other)
{
cout << "operator << function" << endl;
cout << other.get_my_data();
return cout;
}
程序源码:
#include <iostream>
#include <string.h>
using namespace std;
class MyString
{
private:
char* m_data;
public:
MyString()
{
this->m_data=new char[1]();
this->m_data[0]='\0';
cout << "mystring void structure" << endl;
}
MyString(const char* c_str)
{
int len=strlen(c_str);
this->m_data=new char[len+1];
memmove(this->m_data,c_str,len);
this->m_data[len]='\0';
cout << "mystring exit structure" << endl;
}
MyString(const MyString& other)
{
int len=strlen(other.m_data);
this->m_data=new char[len+1];
memmove(this->m_data,other.m_data,len);
this->m_data[len]='\0';
cout << "mystring copy function" << endl;
}
MyString& operator=(const MyString& other)
{
cout << "mystring operator= function" << endl;
if(this==&other){
return *this;
}
int len=strlen(other.m_data);
if(nullptr!=this->m_data)
{
delete [] m_data;
}
this->m_data=new char[len+1]();
memmove(this->m_data,other.m_data,len);
this->m_data[len]='\0';
return *this;
}
char operator[](int index)
{
cout << "mystring operator[] function" << endl;
if(index<0||index>=strlen(this->m_data))
{
cout << "cross the border" << endl;
}
return this->m_data[index];
}
MyString operator+(const MyString& other)
{
cout << "mystring operator+ function" << endl;
int my_len=strlen(this->m_data);
int other_len=strlen(other.m_data);
char* temp=new char[my_len+other_len+1]();
memmove(temp,this->m_data,my_len);
memmove(temp+my_len,other.m_data,other_len);
temp[my_len+other_len]='\0';
MyString temp_str=temp;
delete [] temp;
return temp_str;
}
MyString& append(const MyString& other)
{
cout << "mystring append function" << endl;
int my_len=strlen(this->m_data);
int other_len=strlen(other.m_data);
char* temp=new char[my_len]();
memmove(temp,this->m_data,my_len);
delete []this->m_data;
this->m_data=new char[my_len+other_len+1]();
memmove(m_data,temp,my_len);
delete [] temp;
memmove(m_data+my_len,other.m_data,other_len);
this->m_data[my_len+other_len+1]='\0';
return *this;
}
char* get_my_data()const
{
return this->m_data;
}
~MyString()
{
delete []m_data;
this->m_data=nullptr;
cout << "mystring destruct" << endl;
}
};
ostream& operator << (ostream& cout,const MyString& other)
{
cout << "operator << function" << endl;
cout << other.get_my_data();
return cout;
}
int main()
{
MyString str1;//无参构造
str1="yao";//=运算符函数
MyString str2="liang";//隐式传参构造
MyString str3("hello");//显示传参构造
MyString str4=str3;//拷贝构造
cout << str3.get_my_data() << endl;//MyString类型的指针转化为char类型指针输出
cout << str3 << endl;//<<运算符函数
cout << str4[0] << endl;//[]运算符函数
cout << str1+str2 << endl;//+运算符函数&&<<运算符函数
cout << str1.append(str2) << endl;//追加成员函数&&<<运算符函数
cout << str1 << endl;//追加后结果发生改变&&<< 运算符函数
return 0;
}
运行结果分析: