参考string类完成my_string类
- #include
- #include
- using namespace std;
- class my_string
- {
- private:
- char *str;
- int len;
- public:
- //无参构造
- my_string()
- {
- len = 15;
- str = new char[len];
- cout<<"无参构造"<
- }
- //有参构造
- my_string(char *p)
- {
- len = 15;
- this->str = new char[len];
- memset(str,0,15);
- strcpy(str,p);
- cout<<"有参构造"<
- }
- //拷贝构造
- my_string(const my_string &other)
- {
- len=15;
- this->str=new char[len];
- memset(str,0,15);
- strcpy(str,other.str);
-
- }
- //拷贝赋值
- my_string &operator=(const my_string &other)
- {
- if(&other !=this)
- {
- len=15;
- this->str=new char[len];
- memset(str,0,15);
- strcpy(str,other.str);
- }
-
- }
- //析构函数
- ~my_string()
- {
- delete str;
- cout<<"析构函数"<<" "<<this<
- }
- void show()
- {
- cout<
- }
- //判空
- bool empty()
- {
- if(*str==0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //求总长度
- int size()
- {
- int l=0;
- char *q=str;
- while(*q!=0)
- {l++;
- q++;
- }
-
- return l;
- }
- //at()
- char &at(int pos){}
- //转c风格字符串函数
- char *c_str(){};
- };
- int main()
- {
- char * p ="hello" ;
- my_string s1;
- my_string s2(p);
- s2.show();
- my_string s4(s2);
- s4.show();
- my_string s5;
- s5=s4;
- s5.show();
- cout<
size()< -
-
- return 0;
- }

-
相关阅读:
JDK1.8新特性介绍+Lambda+函数接口+方法、构造方法和数组引用及Stream流
Rust入门基础
说说你对关键字this的认识?
【Pytorch Lighting】第 9 章:部署和评分模型
论文解读(USIB)《Towards Explanation for Unsupervised Graph-Level Representation Learning》
SSM+线上诊疗系统 毕业设计-附源码161711
腾讯云入侵
腾讯后端开发高频问题和答案
从“1L 小钢炮”到 “PC界变形金刚”——Tiny助力企业数智转型的十年进化之路
如何在 pyqt 中实现桌面歌词
-
原文地址:https://blog.csdn.net/ww1106/article/details/127098687