题目:仿照string类,封装一个My_string类,并实现相关功能
header.h
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
using namespace std;
class My_string
{
private:
char *data;
int size;
public:
My_string();
My_string(const char *str);
My_string(int n, char ch);
//析构函数
~My_string();
//拷贝构造函数
My_string(const My_string &other);
//拷贝赋值函数
My_string & operator=(const My_string &other);
//c_str函数
char *C_str();
//size函数
int my_size();
//empty函数
bool my_empty();
//at函数
char &my_at(int n);
void show();
//实现+运算符重载
const My_string operator+(const My_string &R)const;
//实现+=运算符的重载:
My_string &operator+=(const My_string &R);
//实现[]运算符的重载
char &operator[](int n);
// 实现>运算符重载函数:
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, My_string &R);
};
#endif // HEADER_H
my_string.cpp
#include <iostream>
#include<cstring>
#include<header.h>
using namespace std;
//无参构造默认长度为15
My_string::My_string():size(15)
{
data = new char[size];
data[0] = '\0';
}
//有参构造
My_string::My_string(const char *str)
{
size=strlen(str)+1;
data=new char[size];
strcpy(data,str);
cout<<"有参构造函数"<<endl;
}
//有参函数
My_string::My_string(int n, char ch)
{
data=new char[n+1];
for(int i=0;i<n;i++)
{
data[i]=ch;
}
}
//析构函数
My_string::~My_string()
{
cout<<"my_string::析构函数"<<" this = "<<this<<endl;
delete []data;
data=nullptr;
}
//拷贝构造函数
My_string::My_string(const My_string &other):data(new char[other.size]),size(other.size)
{
strcpy(this->data,other.data);
this->size=other.size;
cout<<"Stu::拷贝构造函数"<<endl;
}
//拷贝赋值函数
My_string & My_string::operator=(const My_string &other)
{
delete []data;
data=nullptr;
data=new char[other.size];
this->size = other.size;
strcpy(this->data,other.data); //深拷贝
cout<<"Stu::拷贝赋值函数"<<endl;
return *this;
}
//c_str函数
char *My_string::C_str()
{
return data;
}
//size函数
int My_string:: my_size()
{
return size;
}
//empty函数
bool My_string::my_empty()
{
return data[0]==0;
}
//at函数
char &My_string::my_at(int n)
{
return data[n-1];
}
void My_string::show()
{
cout<<"data:"<<data<<" ";
size=strlen(data);
cout<<"size:"<<size<<endl;
}
//实现+运算符重载
const My_string My_string::operator+(const My_string &R)const
{
My_string temp; //定义一个临时的类对象
temp.data = strcat(this->data,R.data);
temp.size = strlen(temp.data)+1;
return temp;
}
//实现+=运算符的重载:
My_string &My_string::operator+=(const My_string &R)
{
this->data=strcat(this->data,R.data);
this->size=strlen(this->data)+1;
return *this; //为了级联使用,连等于
}
//实现[]运算符的重载
char & My_string::operator[](int n)
{
return this->data[n-1];
}
// 实现>运算符重载函数:
bool My_string::operator>(const My_string &R)const
{
return this->data>R.data;
}
// 实现<运算符重载函数:
bool My_string::operator<(const My_string &R)const
{
return this->data<R.data;
}
// 实现==运算符重载函数:
bool My_string::operator==(const My_string &R)const
{
return this->data==R.data;
}
// 实现!=运算符重载函数:
bool My_string::operator!=(const My_string &R)const
{
return this->data!=R.data;
}
// 实现>=运算符重载函数:
bool My_string::operator>=(const My_string &R)const
{
return this->data>=R.data;
}
// 实现<=运算符重载函数:
bool My_string::operator<=(const My_string &R)const
{
return this->data<=R.data;
}
//实现插入运算符的重载:
ostream &operator<<(ostream &L, const My_string &R)
{
L << R.data;
return L;
}
//实现提取运算符的重载:
istream &operator>>(istream &L, My_string &R)
{
L>>R.data;
return L;
}
main.cpp
#include <iostream>
#include<cstring>
#include<header.h>
using namespace std;
int main()
{
//有参构造函数
My_string s1("hello");
//拷贝构造函数
My_string s2=s1;
s2.show();
//无参构造函数
My_string s3;
//拷贝赋值函数
s3=s1;
s3.show();
//c++字符串转c
cout<<s3.C_str()<<endl;
//size函数
cout<<"s3的容量为:"<<s3.my_size()<<endl;
//empty
My_string s4;
cout<<"s4:";
s4.my_empty();
cout<<"s1第二个元素:"<<s1.my_at(2)<<endl;
//调用+=运算符的重载函数:
My_string s5("world");
s5+=s1;
cout<<"s5:";
s5.show();
//调用实现+运算符重载函数
My_string s6;
s6=s1+s5;
cout<<"s6:";
s6.show();
//调用>运算符重载函数
if((s1>s5)==1)
{
cout<<"s1>s5"<<endl;
}
else
{
cout<<"s1<s5"<<endl;
}
//调用[]运算符重载函数
cout<<"s1第二个元素为:"<<s1[2]<<endl;
//调用插入<<运算符重载函数
cout<<"s5:";
cout<<s5<<endl;
//调用提取>>运算符重载函数
My_string s7;
cout<<"请输入s1的值:";
cin>>s1;
s1.show();
return 0;
}
运行效果: