#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:
char *str; //记录c风格的字符串
int size; //记录字符串的实际长度
public:
//无参构造
myString():size(10),str(new char[32])
{
str = new char[size]; //构造出一个长度为10的字符串
strcpy(str,"");
}
//有参构造
myString(const char *s)
{
size = strlen(s);
str = new char[size+1];
strcpy(str, s);
}
//拷贝构造
myString(myString&other)
{
size=other.size;
str=new char[size+1];
strcpy(str,other.str);
}
//拷贝赋值函数
myString &operator=(myString &other)
{
if(this != &other)
{
delete[]this->str;
this->str = new char[size+1];
strcpy(this->str,other.str);
return *this;
}
return *this;
}
//析构函数
~myString()
{
delete []str;
}
//判空函数
bool myempty();
//size函数
long mysize();
//c_str函数
char *cstr();
//at函数
char &at(int pos);
//[ ]重载
char operator[](int index)
{
for(int i = 0;i < size;i++)
{
if(i == index)
return str[index];
}
cout << "异常" << endl;
return 0;
}
//重载'+'
myString &operator+(const myString &s)
{
strcat(this->str,s.str);
return *this;
}
//重载'=='
bool operator==(const myString &s)const
{
if(0 == strcmp(this->str,s.str))
return 1;
else
return 0;
}
//重载'>'
bool operator>(const myString &s)const
{
if(0 < strcmp(this->str,s.str))
return 1;
else if(0 > strcmp(this->str,s.str))
return 0;
}
//重载'<'
bool operator<(const myString &s)const
{
if(0 > strcmp(this->str,s.str))
return 1;
else if(0 < strcmp(this->str,s.str))
return 0;
}
friend ostream &operator<<(ostream &out,const myString &s);
friend istream &operator>>(istream &in,const myString &s);
};
//c_str函数
char *myString::cstr()
{
return str;
}
//判空函数
bool myString::myempty()
{
if(0 == strlen(str))
{
return 1;
}
else
{
return 0;
}
}
//size函数
long myString::mysize()
{
return strlen(str);
}
//at函数
char &myString::at(int pos)
{
int length = strlen(str);
if(pos >= length || pos < 0)
{
cout << "error" << endl;
return str[pos];
}
else
return str[pos];
}
//全局实现<<重载
ostream &operator<<(ostream &out,const myString &s)
{
out<<s.str;
return out;
}
//全局实现>>重载
istream &operator>>(istream &in,const myString &s)
{
in>>s.str;
return in;
}
int main()
{
myString s1("love");//调用有参构造
cout << "s1 = " << s1.cstr() << endl; //love
myString s2(s1); //调用拷贝构造
cout << "s2 = " << s2.cstr() << endl;//love
if(s2.myempty())//调用判空函数
cout <<"empty" << endl;
else
cout << "not empty" << endl;
cout << "sizeof(s2) = " << s2.mysize() << endl; //调用size函数
cout << "s2[1] = " << s2.at(1) << endl;//调用at函数//o
//拷贝赋值
myString s3;
s3 = s1;
cout << "s3 = " << s3.cstr() << endl;//love
//[ ]重载
cout << "s3[1] = " << s3[1] << endl;//o
//重载'+'
myString s4;
s4 = s1 + s3;
cout << "s4 = " << s4.cstr() << endl; //lovelove
//重载'=='
if(s4 == s1)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
//重载'>'
if((s4 > s1) > 0)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
//重载'<'
if((s4 < s1) > 0)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
//重载'<<'
cout<<s1.cstr()<<endl;
//重载'>>'
myString s5;
cin>>s5.cstr();
cout<<"s5 = "<<s5.cstr()<<endl;
return 0;
}
C++中的封装
于 2023-04-21 20:16:48 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)