参考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;
- }

-
相关阅读:
VB.net:VB.net编程语言学习之ADO.net基本名称空间与类的简介、案例应用(实现与SQL数据库编程案例)之详细攻略
WPF 项目开发入门(五)ListView列表组件 与 Expander组件
(121)Verilog HDL:设计一个加法功能之Module addsub
23种设计模式(六)原型模式 (阁瑞钛伦特软件-九耶实训)
代码随想录总结
十大java应用服务器(web server)总结
Docker与低代码跨平台开发:实现高效跨平台开发的新范式
智慧校园管理系统全套源码 智慧学校源码(小程序端、电子班牌、人脸识别系统)
查询两张表的信息 可能用到两张实体类 所以可以创建一个新的实体类来装 新的实体类可以有两个表的属性,或者继承复用父类。
使用Jedis和Sentinel完成秒杀功能
-
原文地址:https://blog.csdn.net/ww1106/article/details/127098687