码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++——string的封装


    参考string类完成my_string类

    1. #include
    2. #include
    3. using namespace std;
    4. class my_string
    5. {
    6. private:
    7. char *str;
    8. int len;
    9. public:
    10. //无参构造
    11. my_string()
    12. {
    13. len = 15;
    14. str = new char[len];
    15. cout<<"无参构造"<
    16. }
    17. //有参构造
    18. my_string(char *p)
    19. {
    20. len = 15;
    21. this->str = new char[len];
    22. memset(str,0,15);
    23. strcpy(str,p);
    24. cout<<"有参构造"<
    25. }
    26. //拷贝构造
    27. my_string(const my_string &other)
    28. {
    29. len=15;
    30. this->str=new char[len];
    31. memset(str,0,15);
    32. strcpy(str,other.str);
    33. }
    34. //拷贝赋值
    35. my_string &operator=(const my_string &other)
    36. {
    37. if(&other !=this)
    38. {
    39. len=15;
    40. this->str=new char[len];
    41. memset(str,0,15);
    42. strcpy(str,other.str);
    43. }
    44. }
    45. //析构函数
    46. ~my_string()
    47. {
    48. delete str;
    49. cout<<"析构函数"<<" "<<this<
    50. }
    51. void show()
    52. {
    53. cout<
    54. }
    55. //判空
    56. bool empty()
    57. {
    58. if(*str==0)
    59. {
    60. return true;
    61. }
    62. else
    63. {
    64. return false;
    65. }
    66. }
    67. //求总长度
    68. int size()
    69. {
    70. int l=0;
    71. char *q=str;
    72. while(*q!=0)
    73. {l++;
    74. q++;
    75. }
    76. return l;
    77. }
    78. //at()
    79. char &at(int pos){}
    80. //转c风格字符串函数
    81. char *c_str(){};
    82. };
    83. int main()
    84. {
    85. char * p ="hello" ;
    86. my_string s1;
    87. my_string s2(p);
    88. s2.show();
    89. my_string s4(s2);
    90. s4.show();
    91. my_string s5;
    92. s5=s4;
    93. s5.show();
    94. cout<size()<
    95. return 0;
    96. }

     

  • 相关阅读:
    【SCI征稿】2区数字技术、供应链网络、人工智能、工业物联网智能传感器等领域,进展超顺,仅3个月左右录用
    ChinaSkills技能大赛网络系统管理Debian模块(样题一)||RSERVER TASK配置
    浅谈快速开发平台:突破系统开发边界,赋能企业数字化!
    LeetCode刷题复盘笔记——46. 全排列(一文搞懂回溯解决全排列问题上篇)
    国产操作系统之凝思磐石安装
    DTLS、ICE--用janus举例
    【Netty】七、Netty自定义编码和解码器
    学校档案管理系统软件-学校数字档案室解决方案
    单片机——通过对P3口地址的操作流水点亮8位LED
    完全背包问题
  • 原文地址:https://blog.csdn.net/ww1106/article/details/127098687
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    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
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号