- #include
- #include
- using namespace std;
- class myString
- {
- private:
- char *str;
- int size;
- public:
- //无参构造
- myString():size(10)
- {
- str = new char[size]; //构造出一个长度为10的字符串
- strcpy(str,""); //赋值为空串
- }
- //有参构造
- myString(const char *s)
- {
- size = strlen(s);
- str = new char[size+1];
- strcpy(str,s);
- }
-
- //拷贝构造
- myString(const myString &other):size(other.size)
- {
- str = new char [size];
- strcpy(this->str,other.str);
- cout<<"拷贝构造函数"<
- }
-
- //析构函数
- ~myString()
- {
- delete[] this->str;
- cout<<"析构函数"<
- }
- //拷贝赋值函数
- myString &operator = (const myString &other)
- {
- size = other.size;
- strcpy(str,other.str);
- cout<<"拷贝赋值函数"<
- return *this;
-
- }
-
- //判空函数
- bool str_empty(const char *str) const
- {
- if(str ==NULL||*str=='\0')
- {
-
- return true;
- }
- else
- return false;
- }
- //size函数
- int str_size(const char *str)const
- {
- return sizeof(str);
- }
- //c_str函数
- const char *c_str() const
- {
- return str;
- }
- //at函数
- char &at(int pos)
- {
-
-
- return str[pos];
-
- }
- //成员函数版实现加号运算符重载
- myString operator+(const myString &R)const
- {
- myString new_string = *this;
- delete[] new_string.str;
- int len =strlen(this->str)+strlen(R.str)+1;
- new_string.str = new char[len];
- strcpy(new_string.str,this->str);
- strcat(new_string.str,R.str);
- return new_string;
- }
- //成员函数版实现加等于运算符重载
- myString &operator+=(const myString &R)
- {
- int len = strlen(str)+strlen(R.str)+1;
- char *s =this->str;
- str = nullptr;
- delete [] str;
- str = new char [len];
- strcpy(this->str,s);
- strcat(this->str,R.str);
- return *this;
- }
-
- //关系运算符重载
- bool operator>(const myString &R)const
- {
- //先求出长度
- int len1 = strlen(this->str);
- int len2 = strlen(R.str);
- int minlen =(len1
- for(int i=0;i
- {
- if(this->str[i]>R.str[i])
- {
- return true;
- }
- else if(this->str[i]
- {
- return false;
- }
- }
- return len1>len2;
- }
-
- //成员函数版实现中括号运算符重载
- char & operator[](int index)
- {
- if(index>=0&&index
- {
- return str[index];
- }
- }
- //展示函数
- void show()
- {
- cout<<"str = "<
" size = "< - }
- };
- int main()
- {
- myString s1("hello");
- s1[0]='H';
- s1.show();
- myString s2("world");
- s2.show();
- myString s6 =s1;
- s6.show();
- myString s3;
- s3 = s1 + s2;
- cout<
c_str()< - myString s4("hahaha");
- s4+=s1;
- cout<
c_str()< - if(s3>s2)
- {
- cout<<"yes"<
- }
- else
- cout<<"no"<
- myString s5("daydayup");
- s5.show();
- return 0;
- }
效果图

-
相关阅读:
Docker网络通信原理
《实现领域驱动设计》
pclpy 可视化点云(多窗口可视化、单窗口多点云可视化)
中科创达邹鹏程:与openEuler的结缘并非偶然,操作系统的未来离不开创新
Pod详解
NoSQL之redis集群(未完待续)
【GitHub 个人主页】适应于初学者的自定义个人主页设置
LVS集群(Linux Virtual server)
SSM的整合与使用
二分查找详解(while条件判断+转换值判断)
-
原文地址:https://blog.csdn.net/cscssacd/article/details/133616978
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU