#include <iostream>
#include <cstring>
using namespace std;
class myString{
private:
//string
char *str;//字符串
int size;//字符串长度
public:
//无参构造
myString();
//有参构造
myString(const char *s);
//拷贝构造
myString(myString &other);
//析构函数
~myString();
//判空函数
bool myEmpty();
//size函数
size_t mySize();
//c_str函数
char* myC_str();
//at函数
char myAt(int pos);
//为字符串赋值
const myString & operator=(const myString &C){
delete []str;
this->size = C.size;
this->str = new char[size+1];
strcpy(this->str,C.str);
cout<<"为字符串赋值函数"<<endl;
return *this;
}
//访问指定字符
char operator[](int index){
cout<<"访问指定字符函数"<<endl;
if(index <= 0 || index > size)
cout<<"越界"<<endl;
else
return str[index];
}
//链接两个str
const myString & operator+(const myString &C){
this->size += C.size;
strcat(this->str, C.str);
cout<<"链接字符函数"<<endl;
return *this;
}
//比较字符串相等
bool operator==(const myString &C){
if(this->str == C.str && this->size == C.size)
return true;
else
return false;
}
//比较字符串大于
bool operator>(const myString &C)const{
if(this->str > C.str)
return true;
else
return false;
}
//比较字符串小于
bool operator<(const myString &C)const{
if(this->str < C.str)
return true;
else
return false;
}
//输入
friend istream & operator>>(istream &cin, myString &C );
//输出
friend ostream & operator<<(ostream &cout, myString &C);
//展示string
void show(){
cout<<this->str<<endl;
}
};
//无参构造
myString::myString():size(10){
str = new char[size];
strcpy(str, "");
cout<<"调用无参构造"<<endl;
}
//有参构造
myString::myString(const char *s){
size = strlen(s);
str = new char[size+1];
strcpy(str, s);
cout<<"调用有参构造"<<endl;
}
//拷贝构造
myString::myString(myString &other){
size = strlen(other.str);
str = new char[size+1];
strcpy(str, other.str);
cout<<"调用拷贝函数"<<endl;
}
//析构函数
myString::~myString(){
delete []str;
str = nullptr;
cout<<"调用析构函数"<<endl;
}
//判空函数
bool myString::myEmpty(){
if(0 == strlen(str)+1)
return true;
else
return false;
}
//size函数
size_t myString::mySize(){
return size+1;
}
//c_str函数
char* myString::myC_str(){
return str;
}
//at函数
char myString::myAt(int pos){
if(pos <= 0 || pos > size)
cout<<"越界"<<endl;
else
return str[pos];
}
//输入
istream & operator>>(istream &cin, myString &C ){
string s;
cin>>s;
delete []C.str;
strcpy(C.str, s.c_str());
C.size = s.size();
return cin;
}
//输出
ostream & operator<<(ostream &cout, myString &C){
return cout<<C.str<<endl;
}
int main()
{
//有参构造
myString s1("abcdefg");
s1.show();
//拷贝构造
myString s2(s1);
s2.show();
//判空函数
if(s1.myEmpty())
cout<<"空"<<endl;
else
cout<<"不空"<<endl;
//size函数
cout<<s1.mySize()<<endl;
//c_str函数
cout<<s1.myC_str()<<endl;
//at函数
cout<<s1.myAt(3)<<endl;
//赋值运算符
myString s3;
s3 = s1;
s3.show();
//访问字符串
myString s4("higklmn");
cout<<s4[3]<<endl;
//链接两个字符串
s1+s4;
s1.show();
//比较字符串相等
if(s3 == s1)
cout<<"相等"<<endl;
else
cout<<"不相等"<<endl;
//比较字符串大于
if(s3 > s1)
cout<<"s3 > s1"<<endl;
else
cout<<"s3 < s1"<<endl;
//比较字符串小于
if(s3 < s1)
cout<<"s3 < s1"<<endl;
else
cout<<"s3 > s1"<<endl;
//输入
cout<<"请输入字符串"<<endl;
cin>>s4;
s4.show();
//输出
cout<<s4;
return 0;
}
C++day4
最新推荐文章于 2024-09-07 05:15:02 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)