
- #include
- #include
-
- using namespace std;
-
- class Mystring{
- public:
- //无参构造函数
- Mystring():size(10){
- str=new char[size];
- strcpy(str,"");
- cout<<"无参构造函数"<
- }
- //有参构造函数
- Mystring(const char *s){
- size=strlen(s);
- str=new char[size+1];
- strcpy(str,s);
- cout<<"有参构造函数"<
- }
- //拷贝构造函数
- Mystring(const Mystring &other){
- this->size=other.size;
- this->str=new char[this->size];
- strcpy(this->str,other.str);
- cout<<"拷贝构造函数"<
- }
- //析构函数
- ~Mystring(){
- delete []str;
- cout<<"析构函数"<
- }
- //拷贝赋值函数
- Mystring &operator=(const Mystring &other){
- if(this!=&other){
- this->size=other.size;
- strcpy(this->str,other.str);
- }
- cout<<"拷贝赋值函数"<
- return *this;
- }
- //判空函数
- bool empty()const{
- return !strlen(this->str);
- }
- //size函数
- int strsize()const{
- return strlen(this->str);
- }
- //c_str函数
- char *c_str(){
- return this->str;
- }
- //at函数
- char &at(int pos){
- return *(this->str+pos-1);
- }
- //加号运算符重载
- Mystring operator+(const Mystring &R)const{
- Mystring temp;
- strcat(temp.str,this->str);
- strcat(temp.str,R.str);
- return temp;
- }
- //加等于运算符重载
- Mystring &operator+=(const Mystring &R){
- strcat(this->str,R.str);
- return *this;
- }
- //关系运算符重载(>)
- bool operator>(const Mystring &R)const{
- if(strcmp(this->str,R.str)>0){
- return true;
- }
- else{
- return false;
- }
- }
- //中括号运算符重载
- char &operator[](int pos)const{
- return *(this->str+pos-1);
- }
- //展示函数
- void show(){
- cout<
- }
- private:
- char *str; //字符串首地址
- int size; //字符串大小
- };
-
- int main()
- {
- Mystring str1("hello");
- str1.show();
- Mystring str2("world");
- str2.show();
- Mystring str3;
- if(str3.empty()){
- cout<<"str3现在为空,字符串长度为"<
strsize()< - }
- str3=str1;
- str3.show();
- if(!str3.empty()){
- cout<<"str3现在不为空,字符串长度为"<
strsize()< -
- }
- Mystring str4=str2;
- str4.show();
- str4+=str3;
- str4.show();
- cout<<"str4字符串第7位是"<
at(7)<<",str4字符串第13位是"<13]< - cout<
c_str()< - if(str3>str2){
- cout<<"str3>str2"<
- }
- else{
- cout<<"str3
< - }
-
- return 0;
- }
-
相关阅读:
初识C++多态
【Python脚本进阶】1.2、python脚本基础知识(中)
mongodb简单部署
GraphicsMagick之实践出真知
springboot 添加项目依赖
关于离子色谱仪的结构和应用原理分析
IntelliJ IDEA 使用 Maven 时不加载本地私服的最新版本快照(snapshot)JAR 包
贷齐乐系统最新版SQL注入(无需登录绕过WAF可union select跨表查询)
Apisix的ext-plugin-pre-req 中的trace 关联zipkin插件的trace
强化学习案例复现(2)--- MountainCar基于DQN
-
原文地址:https://blog.csdn.net/qq_53268516/article/details/132816519