在My_string类的基础上,完成运算符重载
算术运算符:+
赋值运算符:+=
下标运算符:[]
关系运算符:>、=、
插入提取运算符:>
要求:注意数据的保护(const)
header.h:
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
using namespace std;
class My_string
{
private:
char *data;
int Capacity;
public:
//无参构造函数,默认长度为15
My_string(); //当前字符串变量的最大容量(capacity())
//有参构造函数
My_string(const char *str); //string s("hello world");
My_string(int n, char ch); //string s1(3,'A');
//拷贝构造函数
My_string(const My_string &other); //string s2 = s;
//拷贝赋值函数
My_string & operator=(const My_string &other); //string s3; s3 = s1;
//获取当前最大容量
int capacity();
//c_str函数
const char *c_str();
//size函数
int size();
//empty函数
bool empty();
//at函数
char& at(int i);
//实现+运算符重载
const My_string operator+(const My_string &R)const;
//实现+=运算符重载
My_string & operator+=(const My_string &R);
//实现[]运算符重载
char & operator[](int index);
//实现>运算符重载
bool operator>(const My_string &R)const;
//实现<运算符重载
bool operator<(const My_string &R)const;
//实现==运算符重载
bool operator==(const My_string &R)const;
//实现!=运算符重载
bool operator!=(const My_string &R)const;
//实现>=运算符重载
bool operator>=(const My_string &R)const;
//实现<=运算符重载
bool operator<=(const My_string &R)const;
//析构函数
~My_string();
//将插入运算符重载作为友元函数
friend ostream &operator<<(ostream &L, const My_string &R);
//将提取运算符重载作为友元函数
friend istream &operator>>(istream &L, My_string &R);
};
#endif // HEADER_H
my_string.cpp:
#include <iostream>
#include <cstring>
#include <header.h>
using namespace std;
//无参构造函数,默认长度为15
My_string::My_string() //当前字符串变量的最大容量(capacity())
{
Capacity = 15;
data = new char[Capacity];
data[0] = '\0';
}
//有参构造函数
My_string::My_string(const char *str) //string s("hello world");
{
Capacity = 15;
while((int)strlen(str) > Capacity)
Capacity = strlen(str) * 2;
data = new char[Capacity];
strcpy(data, str);
data[strlen(str)] = '\0';
}
My_string::My_string(int n, char ch) //string s1(3,'A');
{
Capacity = 15;
while(n > Capacity)
Capacity = n * 2;
data = new char[Capacity];
for(int i = 0; i < n; i++)
data[i] = ch;
data[n] = '\0';
}
//拷贝构造函数
My_string::My_string(const My_string &other) //string s2 = s;
{
this->Capacity = other.Capacity;
this->data = new char(other.Capacity);
strcpy(this->data, other.data);
}
//拷贝赋值函数
My_string & My_string::operator=(const My_string &other) //string s3; s3 = s1;
{
delete (this->data);
this->Capacity = other.Capacity;
this->data = new char[Capacity];
strcpy(this->data, other.data);
return *this;
}
//获取当前最大容量
int My_string::capacity()
{
return Capacity;
}
//c_str函数
const char *My_string::c_str()
{
return data;
}
//size函数
int My_string::size()
{
return strlen(data);
}
//empty函数
bool My_string::empty()
{
//cout << strlen(data) << endl;
return strlen(data);
}
//at函数
char& My_string::at(int i)
{
if(i<0 || i>(int)(strlen(data)-1))
cout << "数组越界" << endl;
return data[i];
}
//析构函数
My_string::~My_string()
{
//cout << "S::析构函数" << endl;
delete data;
}
//实现+运算符重载
const My_string My_string::operator+(const My_string &R)const
{
My_string temp; //定义一个临时类
char s_temp[strlen(this->data)+strlen(R.data)]; //定义一个临时变量
strcpy(s_temp,this->data);
strcat(s_temp,R.data);
temp = s_temp;
return temp;
}
//实现+=运算符重载
My_string & My_string::operator+=(const My_string &R)
{
char temp[strlen(this->data)+strlen(R.data)];
strcpy(temp,this->data);
strcat(temp,R.data);
this->operator=(temp);
return *this;
}
//实现[]运算符重载
char & My_string::operator[](int index)
{
return data[index];
}
//实现>运算符重载
bool My_string::operator>(const My_string &R)const
{
return strcmp(this->data,R.data)<0;
}
//实现<运算符重载
bool My_string::operator<(const My_string &R)const
{
return strcmp(this->data,R.data)>0;
}
//实现==运算符重载
bool My_string::operator==(const My_string &R)const
{
return strcmp(this->data,R.data)==0;
}
//实现!=运算符重载
bool My_string::operator!=(const My_string &R)const
{
return strcmp(this->data,R.data)!=0;
}
//实现>=运算符重载
bool My_string::operator>=(const My_string &R)const
{
return strcmp(this->data,R.data)<=0;
}
//实现<=运算符重载
bool My_string::operator<=(const My_string &R)const
{
return strcmp(this->data,R.data)>=0;
}
//实现插入运算符重载
ostream &operator<<(ostream &L, const My_string &R)
{
L << R.data;
return L;
}
//实现提取运算符重载
istream &operator>>(istream &L, My_string &R)
{
char temp[] = ""; //定义一个临时变量用于接收终端输入
L >> temp;
char temp1[strlen(temp)]; //定义一个临时变量赋值给 My_string &R
strcpy(temp1,temp);
R = temp1;
return L;
}
main.cpp:
#include <iostream>
#include <cstring>
#include <header.h>
using namespace std;
int main()
{
My_string s = "hello";
cout << "s = " << s << endl;
cout << "s 当前最大容量:" << s.capacity() << endl;
My_string s1(" world");
cout << "s1 = " << s1 << endl;
My_string s2 = s+s1;
cout << "s2 = " << s2 << endl;
s2 += s1;
cout << "s2 = " << s2 << endl;
cout << "s2 当前最大容量:" << s2.capacity() << endl;
cout << "s2[1] = " << s2[1] << endl;
s2[0] = 'H';
cout << "s2 = " << s2 << endl;
My_string s3;
cout << "请输入s3 = ";
cin >> s3;
if(s3 == s)
cout << "s与s3相等" << endl;
else
cout << "s与s3不等" << endl;
return 0;
}
测试结果如下: