在My_string类的基础上,完成运算符重载
算术运算符:+
赋值运算符:+=
下标运算符:[]
关系运算符:>、=、
插入提取运算符:>
要求:注意数据的保护(const)
头文件
#ifndef HEAD_H
#define HEAD_H
#include <iostream>
using namespace std;
class My_string
{
private:
char *data;
int size;
public:
//无参构造默认长度为15
My_string();
//有参构造1
My_string(const char *str);
//有参构造2
My_string(int n, char ch);
//析构函数
~My_string();
//拷贝构造函数
My_string(const My_string &other);
//拷贝赋值函数
My_string &operator=(const My_string &other);
//c_str函数
char* My_str();
//size函数
int My_size();
//empty函数
bool My_empty();
//at函数
char &M_at(int n);
//+运算符
const My_string operator+(const My_string &R)const;
//+=运算符
My_string &operator+=(const My_string &R);
//[]运算符
char &operator[](int num)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;
//<=运算符
bool operator<=(const My_string &R)const;
//<<插入运算符
friend ostream &operator<<(ostream &L,const My_string &R);
//>>提取运算符
friend istream &operator>>(istream &L,const My_string &R);
};
#endif // HEAD_H
源文件
#include <iostream>
#include <cstring>
#include <head.h>
using namespace std;
//无参构造默认长度为15
My_string::My_string():size(15)
{
data = new char[size];
data[0] = '\0';
}
//有参构造1
My_string::My_string(const char *str):data(new char[strlen(str)+1]),size(strlen(str))
{
strcpy(data,str);
}
//有参构造2
My_string::My_string(int n, char ch):data(new char[n+1]),size(n)
{
for(int i=0;i<n;i++){
data[i] = ch;
}
data[n] = '\0';
}
//析构函数
My_string::~My_string(){
delete []data;
}
//拷贝构造函数
My_string::My_string(const My_string &other):data(new char[other.size+1]),size(other.size){
strcpy(data,other.data);
}
//拷贝赋值函数
My_string & My_string::operator=(const My_string &other){
delete []data;
data = new char[other.size+1];
strcpy(this->data,other.data);
this->size = other.size;
return *this;
}
//c_str函数
char* My_string::My_str(){
return this->data;
}
//size函数
int My_string::My_size(){
return strlen(this->data);
}
//empty函数
bool My_string::My_empty(){
return data[0] == '\0';
}
//at函数
char &My_string::M_at(int n){
return this->data[n];
}
//+运算符
const My_string My_string::operator+(const My_string &R)const{
My_string temp;
delete []temp.data;
temp.size = this->size + R.size;
temp.data = new char[temp.size+1];
strcpy(temp.data,this->data);
strcat(temp.data,R.data);
temp.data[temp.size] = '\0';
return temp;
}
//+=运算符
My_string &My_string::operator+=(const My_string &R){
this->size = strlen(this->data) + strlen(R.data);
char *temp = new char [this->size+1];
strcpy(temp,this->data);
strcat(temp,R.data);
temp[this->size] = '\0';
delete []this->data;
this->data = temp;
return *this;
}
//>运算符
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;
}
//[]运算符
char &My_string::operator[](int num)const{
if(num < 0 || num > this->size){
cout << "数组下标访问越界" << endl;
}
return this->data[num];
}
//<<插入运算符
ostream &operator<<(ostream &L,const My_string &R){
L << R.data ;
return L;
}
//>>提取运算符
istream &operator>>(istream &L,const My_string &R){
L >> R.data;
return L;
}
主函数文件
#include <iostream>
#include <cstring>
#include <head.h>
using namespace std;
int main()
{
My_string s1("hello world");
cout << "s1 = " << s1 << endl;
My_string s2("China No.1");
cout << "s2 = " << s2 << endl;
My_string s3;
s3 = s1+s2;
cout << "+运算符: " <<"s3 = " << s3 << endl;
My_string s4="figthting";
s4+=s3;
cout << "+=运算符: " << "s4 = " << s4 << endl;
cout << "[]运算符: " <<"s4[6] = " << s4[6] << endl;
if(s1 > s2){
cout << ">运算符:" <<"s1大" << endl;
}else{
cout << ">运算符:" <<"s2大" << endl;
}
if(s1 < s2){
cout << "<运算符:" <<"s1小" << endl;
}else{
cout << "<运算符:" <<"s2小" << endl;
}
if(s1 == s2){
cout << "==运算符:" <<"相等" << endl;
}else{
cout << "==运算符:" <<"不相等" << endl;
}
if(s1 != s2){
cout << "!=运算符:" <<"不相等" << endl;
}else{
cout << "!=运算符:" <<"相等" << endl;
}
if(s1 >= s2){
cout << ">=运算符:" <<"s1大于等于" << endl;
}else{
cout << ">=运算符:" <<"s2大于等于" << endl;
}
if(s1 <= s2){
cout << "<=运算符:" <<"s1小于等于" << endl;
}else{
cout << "<=运算符:" <<"s2小于等于" << endl;
}
My_string s5;
cout << "提取运算符>>s5: ";
cin >> s5;
cout << "s5 = " << s5 << endl;
return 0;
}
运行结果