1. h文件
#ifndef _STR_H_
#define _STR_H_
#include <iostream>
using namespace std;
class String
{
public:
String(); //默认构造函数
String(const char*s); //普通构造函数
String(const String& s); //复制构造函数
String& operator=(char* s); //重载=,实现字符串赋值
String& operator=(const String& s); //重载=,实现对象赋值
~String(); //析构函数
char& operator[](int i); //重载[],实现数组运算
const char& operator[](int i) const; // 重载[],实现数组运算,对象为常量
String&operator+(const String& s);
String& operator+=(const String& s); //重载+=,实现与对象相加
String& operator+=(const char* s); //重载+=,实现与字符串相加
friend ostream& operator<<(ostream& out, String& s); //重载<<
friend istream& operator>>(istream& in, String& s); //重载>>
friend bool operator<(const String& left, const String& right); //重载<
friend bool operator>(const String& left, const String& right); //重载>
friend bool operator==(const String& left, const String& right); //重载==
friend bool operator!=(const String& left, const String& right); //重载!=
char* getData();
private:
int size; //data表示的字符串长度
char* data; //指向字符串数据
};
#endif
2. cpp文件
#include "str.h"
String::String(){
data = new char[1];
*data = '\0';
size = 0;
}
String::String(const char* s){
if (s == NULL) {
data = new char[1];
*data = '\0';
size = 0;
}else{
size = strlen(s);
data = new char[size + 1];
strcpy(data,s);
}
}
String::String(const String& s){
size = s.size;
data = new char[s.size+ 1];
strcpy(data, s.data);
}
String& String::operator=(char *s)
{
if (data != NULL)
delete[] data;
if(s==NULL){
data = new char[1];
*data = '\0';
size = 0;
return *this;
}else{
size = strlen(s);
data = new char[size + 1];
strcpy(data, s);
return *this;
}
}
String& String::operator=(const String& s)
{
if (this == &s)
return *this;
if (data != NULL)
delete[] data;
size = s.size;
data = new char[size + 1];
strcpy(data, s.data);
return *this;
}
String::~String()
{
cout<<"deconstruct"<<endl;
if (data != NULL)
{
delete[] data;
data = NULL;
size = 0;
}
}
char& String::operator[](int i)
{
return data[i];
}
const char& String::operator[](int i) const
{
return data[i];
}
String& String::operator+(const String& s)
{
String *pstring=new String("");
pstring->data=new char[size+s.size+1];
pstring->size=size+s.size;
strcpy(pstring->data,data);
strcat(pstring->data,s.data);
return *pstring;
}
String& String::operator+=(const char* s)
{
if (s == NULL)
{
return *this;
}
int len = size + strlen(s) + 1;
char *temp = data;
data = new char[len];
strcpy(data, temp);
strcat(data,s);
delete[] temp;
size = len - 1;
return *this;
}
String& String::operator+=(const String& s)
{
int len=size+s.size+1;
char *temp=data;
data=new char[len];
strcpy(data,temp);
delete[]temp;
strcat(data,s.data);
size = len - 1;
return *this;
}
ostream& operator<<(ostream& out, String& s)
{
for (int i = 0; i < s.size; i++)
out << s[i] ;
return out;
}
istream& operator>>(istream& in, String& s)
{
char p[50];
in.getline(p, 50); //从输入流接受最多50个字符
s = p; //调用赋值函数
return in;
}
bool operator<(const String& left, const String& right) //重载<
{
int i = 0;
while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
i++;
return left[i] - right[i] < 0 ? true : false;
}
bool operator>(const String& left, const String& right) //重载<
{
int i = 0;
while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
i++;
return left[i] - right[i] > 0 ? true : false;
}
bool operator==(const String& left, const String& right)
{
if (left.size != right.size)
return false;
for (int i = 0; i < left.size; i++)
{
if (left.data[i] != right.data[i])
return false;
}
return true;
}
bool operator!=(const String& left, const String& right)
{
if (left.size != right.size)
return true;
for (int i = 0; i < left.size; i++)
{
if (left.data[i] != right.data[i])
return true;
}
return false;
}
3.main函数
#include "str.h"
int main()
{
String str1;//默认构造函数
String str2("asdf");//普通构造函数
String str4("hello world");//普通构造函数
String str3=str2;//复刻构造函数
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
cout << "str3: " << str3 << endl;
//str3 = NULL;//赋值函数特例
//cout << "str3: " << str3 << endl;
str3 = str4;//赋值函数
cout << "str3: " << str3 << endl;
cout << "str3[2]: " << str3[2] << endl;
str3 += "111";
cout << "str3: " << str3 << endl;
str3 += str2;
cout << "str3: " << str3 << endl;
cout<<"input start"<<endl;
cin >> str1;
cout << "str1: " << str1 << endl;
str4=str4+str1;
cout << "str4: " << str4 << endl;
String t1 = "1234";
String t2 = "1234";
String t3 = "12345";
String t4 = "12335";
cout << "t1 == t2 ? " << (t1 == t2) << endl;
cout << "t1 < t3 ? " << (t1 < t3) << endl;
cout << "t1 > t4 ? " << (t1>t4) << endl;
cout << "t1 != t4 ? " << (t1 != t4) << endl;
return 0;
}