header.h
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
public:
//无参构造
myString();
//有参构造
myString(const char *s);
//拷贝构造
myString(const myString & other);
//析构函数
~myString();
//判空函数
bool isEmpty();
//size函数
int mySize();
//c_str函数
void c_str();
//at函数
char &at(int pos);
//拷贝赋值
myString operator=(const myString &other);
// << 重载
friend ostream &operator<<(ostream &l ,const myString &r);
// >> 重载
friend istream &operator>>(istream &l , myString &r);
// + 重载
myString operator+(const myString &other);
// == 重载
bool operator==(const myString &other);
// != 重载
bool operator!=(const myString &other);
// < 重载
bool operator<(const myString &other);
// > 重载
bool operator>(const myString &other);
// <= 重载
bool operator<=(const myString &other);
// >= 重载
bool operator>=(const myString &other);
// [] 重载
char &operator[](int pos);
private:
char *str; //字符串
int size; //字符串长度
};
#endif // HEADER_H
test.cpp
#include "header.h"
#include <cstring>
//无参构造
myString::myString() : size(10)
{
str = new char[size];
strcpy(str, "");
}
//有参构造
myString::myString(const char *s)
{
size = strlen(s);
str = new char[size + 1];
strcpy(str, s);
}
//拷贝构造
myString::myString(const myString & other)
{
size = other.size;
str = new char[size + 1];
strcpy(str, other.str);
}
//析构函数
myString::~myString()
{
delete (str);
}
//判空
bool myString::isEmpty()
{
return (size == 0) ? true : false;
}
//获取长度
int myString::mySize()
{
return size;
}
//c_str
void myString::c_str()
{
cout<< str << endl;
}
//at
char &myString::at(int pos)
{
char &ch = str[pos];
return ch;
}
//拷贝赋值
myString myString::operator=(const myString &other)
{
this->size = other.size;
strcpy(this->str,other.str);
return *this;
}
// << 重载
ostream &operator<<(ostream &l ,const myString &r)
{
l<<r.str;
return l;
}
// >> 重载
istream &operator>>(istream &l , myString &r)
{
l>>r.str;
return l;
}
// + 重载
myString myString::operator+(const myString &other)
{
strcat(this->str,other.str);
return *this;
}
// == 重载
bool myString::operator==(const myString &other)
{
if(strcmp(this->str,other.str) == 0)
{
return 1;
}
return 0;
}
// != 重载
bool myString::operator!=(const myString &other)
{
if(strcmp(this->str,other.str) != 0)
{
return 1;
}
return 0;
}
// < 运算符重载
bool myString::operator<(const myString &other)
{
if(strcmp(this->str,other.str) < 0)
{
return 1;
}
return 0;
}
// > 运算符重载
bool myString::operator>(const myString &other)
{
if(strcmp(this->str,other.str) > 0)
{
return 1;
}
return 0;
}
// < 运算符重载
bool myString::operator<=(const myString &other)
{
if(strcmp(this->str,other.str) <= 0)
{
return 1;
}
return 0;
}
// > 运算符重载
bool myString::operator>=(const myString &other)
{
if(strcmp(this->str,other.str) >= 0)
{
return 1;
}
return 0;
}
// []运算符重载
char &myString::operator[](int pos)
{
return str[pos];
}
main.cpp
#include<header.h>
using namespace std;
int main()
{
//无参
myString s1;
//判空
s1.isEmpty();
//有参
myString s2("aaaAAA");
//判空
s2.isEmpty();
//c_str
s2.c_str();
//at
cout << "s2.at(1): " << s2.at(1) << endl;
myString s3("mmmMMMM");
//at
cout << "s3.at(3): " << s3.at(3) << endl;
//拷贝构造
myString s4 = s3;
//c_str
s4.c_str();
//at
cout << "s4.at(1): " << s4.at(3) << endl;
// ==
cout << "(s1 == s2): " << (s1 == s2) << endl;
// +
myString s5 = s3 + s1;
cout << "s5: " << endl;
s5.c_str();
// >
cout << "(s4 > s5): " << (s4 > s5) << endl;
// <=
cout << "(s3 <= s4): " << (s3 <= s4) << endl;
//[]
cout << "s3[2]: " << s3[2] << endl;
return 0;
}