为提高自身编码能力,学习书写自定义string类。
直接上代码:
所有代码已测试
.h文件
#pragma once
#include <iostream>
#include <string.h>
using namespace std;
class MyString{
public:
MyString();//默认构造函数
MyString(const char *str);//使用字符串进行初始化
MyString(const MyString &str);//拷贝构造函数
~MyString();//析构函数
//重载运算符
MyString operator+(const MyString &str);//字符串拼接
MyString &operator+=(const MyString &str);//字符串延长
char operator[](int index);//返回特定位置字符
inline bool operator==(const MyString &str);
MyString &operator=(const MyString &str);
MyString &operator=(const char *str);
friend ostream &operator<<(ostream &os, const MyString &str);//重载输出符号
//基本功能函数
char &strOfIndex(int index);
int length() const {return len_;}
const char *getStr()const;
private:
int len_;
char *my_str_;
};
.cpp文件
#include "my_string.h"
MyString::MyString()
{
len_ = 0;
my_str_ = new char[1];
my_str_[0] = '\0';
}
MyString::MyString(const char *const str)
{
if(str){
len_ = strlen(str);
my_str_ = new char [len_ + 1];
strcpy(my_str_, str);
}else{
MyString();
}
}
MyString::MyString(const MyString &str)
{
cout<<"copy function !"<<endl;
len_ = str.length();
my_str_ = new char [len_ + 1];
strcpy(my_str_, str.getStr());
}
MyString::~MyString()
{
delete []my_str_;
len_ = 0;
}
MyString MyString::operator+(const MyString &str)
{
// MyString new_str;
// new_str.len_ = str.length() + len_;
// new_str.my_str_ = new char[new_str.len_ + 1];
// strcpy(new_str.my_str_, str.my_str_);
// strcat(new_str.my_str_, my_str_);
// return new_str;
int len = str.length() + len_;
char* new_str = new char[len + 1];
strcat(new_str, my_str_);
strcat(new_str, str.getStr());
MyString new_my_str(new_str);
delete[] new_str;
return new_my_str;
}
MyString &MyString::operator+=(const MyString &str)
{
if(str.length() <= 0)
return *this;
len_ += str.length();
char *new_my_str = new char[len_ + 1];
strcpy(new_my_str, my_str_);
strcat(new_my_str, str.getStr());
delete []my_str_;
my_str_ = new_my_str;
delete new_my_str;
return *this;
}
char MyString::operator[](int index)
{
if(index > len_){
std::cout<<"index out of range !!"<<std::endl;
std::cout<<"The last char is : ";
return my_str_[-1];
}
return my_str_[index-1];
}
bool MyString::operator==(const MyString &str)
{
if(str.length() != len_)
return false;
if(strcmp(my_str_, str.getStr()))
return true;
}
MyString &MyString::operator=(const MyString &str)
{
if(this == &str)
return *this;
delete []my_str_;
my_str_ = new char[str.length() + 1];
strcpy(my_str_, str.getStr());
return *this;
}
MyString &MyString::operator=(const char *str)
{
delete []my_str_;
this->len_ = strlen(str);
my_str_ = new char[len_ + 1];
strcpy(my_str_, str);
return *this;
}
char &MyString::strOfIndex(int index)
{
if(index > len_){
std::cout<<"index out of range !!"<<std::endl;
std::cout<<"The last char is : ";
return my_str_[-1];
}
return my_str_[index];
}
const char *MyString::getStr()const
{
return my_str_;
}
ostream &operator<<(ostream &os, const MyString &str)
{
os << str.getStr();
return os;
}
main.cpp测试文件
#include <iostream>
#include"my_string.h"
using namespace std;
void display(MyString str);
int main()
{
char str[] = "hello world";
char sstr[] = "hello every";
char sssre[] = "hello beautiful";
MyString m_string;
m_string = str;
cout<<"test_01: "<<m_string.getStr()<<" char's number: "<<m_string.length()<<endl;
MyString my_string(sstr);
cout<<"test_02: "<<my_string.getStr()<<endl;
MyString plus_string = my_string + m_string;
cout<<"test_03: "<<plus_string<<endl;
MyString myy_string(sssre);
cout<<"test_04: "<<myy_string<<endl;
myy_string = my_string;
char id = myy_string[7];
cout<<"test_05: "<<myy_string<<" index 7 :"<<id<<endl;
display(myy_string);
return 0;
}
void display(MyString str)
{
cout<<str<<endl;
}