目录
1.mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
using namespace std;
class MyString
{
private:
char* str; // 保存C风格字符串地址
size_t len; // 保存字符串的实际长度
public:
// 无参构造函数
MyString();
// 有参构造函数
MyString(const char* s);
// 析构函数
~MyString();
// 拷贝函数
MyString(MyString &other);
// 运算符重载
// 拷贝赋值函数 =运算符重载
MyString &operator=(const MyString &other);
// 访问指定字符 []运算符重载
char &operator[](int sub);
// 连接两个字符串 +运算符重载
const MyString operator+(const MyString &other) const;
// 比较两个字符串 >和<运算符重载
bool operator<(const MyString &other) const;
bool operator>(const MyString &other) const;
// >>运算符重载设置为友元函数
friend istream &operator>>(istream &cin, MyString &s);
// <<运算符重载设置为友元函数
friend ostream &operator<<(ostream &cout, const MyString &s);
// 字符串判空函数
bool empty();
// 计算字符串实际长度函数
int size();
// 返回C风格字符串
char* &c_str();
// at函数
char &at(int sub);
};
/// 全局函数
// 输入 >>运算符重载
istream &operator>>(istream &cin, MyString &s);
// 输出 <<运算符重载
ostream &operator<<(ostream &cout, const MyString &s);
#endif // MYSTRING_H
2.mystring.cpp
#include "mystring.h"
#include <cstring>
// 无参构造函数
MyString::MyString() : len(32){ // 给定默认初始大小
str = new char[len]; // 堆区申请空间
strcpy(str, ""); // 字符串赋空值
cout << "无参构造" << endl;
}
// 有参构造函数
MyString::MyString(const char* s){
len = strlen(s); // 计算外部字符串大小,不包含\0
str = new char[len + 1]; // 堆区申请对应大小空间
strcpy(str, s); // 拷贝字符串
cout << "有参构造" << endl;
}
// 析构函数
MyString::~MyString(){
delete [] str; // 释放指针成员属性指向的堆空间
cout << "析构函数" << endl;
}
// 拷贝函数
MyString::MyString(MyString &other) : len(other.len){
str = new char[len + 1]; // 堆区申请对应大小空间
strcpy(str, other.str); // 拷贝字符串
cout << "拷贝构造函数" << endl;
}
// 运算符重载
// 拷贝赋值函数 =运算符重载
MyString &MyString::operator=(const MyString &other){
if (this != &other){ // 判断是否拷贝的自己
delete this->str; // 是否原有空间
this->len = other.len; // 赋值len
this->str = new char[this->len + 1]; // 申请对应大小空间
strcpy(this->str, other.str); // 拷贝字符串
}
return *this;
}
// 访问指定字符 []运算符重载
char &MyString::operator[](int sub){
if (sub >= 0 && sub < (int)this->len){ // 校验下标是否合法
return this->str[sub]; // 返回对于字符
} else {
// 抛书错误
}
}
// 连接两个字符串 +运算符重载
const MyString MyString::operator+(const MyString &other) const{
MyString tmp;
delete [] tmp.str;
tmp.len = this->len + other.len;
tmp.str = new char[tmp.len + 1];
strcat(tmp.str, this->str);
strcat(tmp.str, other.str);
return tmp;
}
// 比较两个字符串 >和<运算符重载
bool MyString::operator<(const MyString &other) const{
return strcmp(this->str, other.str) < 0 ? true : false;
}
bool MyString::operator>(const MyString &other) const{
return strcmp(this->str, other.str) > 0 ? true : false;
}
bool MyString::empty(){
return strlen(this->str) ? false : true;
}
int MyString::size(){
return strlen(this->str);
}
char* &MyString::c_str(){
return this->str;
}
char &MyString::at(int sub){
if (sub >= 0 && sub < size ()){
return this->str[sub];
} else {
// 抛出错误
}
}
/// 全局函数
// 输入 >>运算符重载
istream &operator>>(istream &cin, MyString &s){
cin >> s.str; // 存在问题,会存在越界
return cin;
}
// 输出 <<运算符重载
ostream &operator<<(ostream &cout, const MyString &s){
cout << s.str;
return cout;
}
3.main.cpp
#include <iostream>
#include "mystring.h"
using namespace std;
int main()
{
// 有参构造函数
MyString s1("hello");
cout << "s1 = " << s1.c_str() << endl;
// 拷贝构造函数
MyString s2(s1);
cout << "s2 = " << s2.c_str() << endl;
// 判空
if(s2.empty()){
cout << "s2空" << endl;
} else {
cout << "s2非空" << endl;
}
// 计算大小
cout << "s2大小= " << s2.size() << endl;
// at函数
s2.at(0) = 'H';
cout << "s2 = " << s2.c_str() << endl;
// =运算符重载
MyString s3 = "hello hqyj";
// <<运算符重载
cout << "s3 = " << s3 << endl;
// []运算符重载
s3[0] = 'H';
cout << "s3 = " << s3 << endl;
// +运算符重载
MyString s4 = s2 + s3;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s4 = " << s4 << endl;
// > < 运算符重载
if(s4 > s3){
cout << "s4 > s3 yes" << endl;
} else {
cout << "s4 > s3 no" << endl;
}
if(s4 < s3){
cout << "s4 < s3 yes" << endl;
} else {
cout << "s4 < s3 no" << endl;
}
// >>运算符重载
MyString s5;
cout << "请输入字符串:";
cin >> s5;
cout << "s5 = " << s5 << endl;
return 0;
}
4.运行结果