1. 1 #include<iostream>
  2. 2 #include<cstring>
  3. 3
  4. 4 class String
  5. 5 {
  6. 6 public:
  7. 7 String();
  8. 8 String(const char *str);
  9. 9 String(const String &rhs);
  10. 10 ~String();
  11. 11
  12. 12 String &operator=(const String &rhs);
  13. 13 String operator+(const String &rhs);
  14. 14 char operator[](const unsigned int index);
  15. 15 bool operator==(const String &rhs);
  16. 16 friend std::ostream &operator<<(std::ostream &out, const String &rhs);
  17. 17 private:
  18. 18 char *m_data;
  19. 19 };
  20. 20
  21. 21 String::String()
  22. 22 {
  23. 23 std::cout << "default constructor" << std::endl;
  24. 24 m_data = new char[1];
  25. 25 m_data[0] = '\0';
  26. 26 }
  27. 27
  28. 28 String::String(const char *str)
  29. 29 {
  30. 30 std::cout << "non-default constructor" << std::endl;
  31. 31 if (NULL == str)
  32. 32 {
  33. 33 m_data = new char[1];
  34. 34 m_data[0] = '\0';
  35. 35 }
  36. 36 else
  37. 37 {
  38. 38 m_data = new char[strlen(str)+1];
  39. 39 strcpy(m_data, str);
  40. 40 }
  41. 41 }
  42. 42
  43. 43 String::String(const String &another)
  44. 44 {
  45. 45 std::cout << "copy constructor" << std::endl;
  46. 46 m_data = new char[strlen(another.m_data)+1];
  47. 47 strcpy(m_data, another.m_data);
  48. 48 }
  49. 49
  50. 50 bool String::operator==(const String &rhs)
  51. 51 {
  52. 52 std::cout << "bool == " << std::endl;
  53. 53 int result = strcmp(m_data, rhs.m_data);
  54. 54 if (0 == result)
  55. 55 return true;
  56. 56 else
  57. 57 return false;
  58. 58 }
  59. 59
  60. 60 String &String::operator=(const String &rhs)
  61. 61 {
  62. 62 std::cout << "assign constructor" << std::endl;
  63. 63 if (this == &rhs)
  64. 64 return *this;
  65. 65 delete []m_data;
  66. 66 m_data = new char[strlen(rhs.m_data)+1];
  67. 67 strcpy(m_data, rhs.m_data);
  68. 68 return *this;
  69. 69 }
  70. 70
  71. 71 String String::operator+(const String &rhs)
  72. 72 {
  73. 73 std::cout << "+" << std::endl;
  74. 74 String newString;
  75. 75 if (NULL == rhs.m_data)
  76. 76 newString = *this;
  77. 77 else if(NULL == m_data)
  78. 78 newString = rhs;
  79. 79 else
  80. 80 {
  81. 81 newString.m_data = new char[strlen(rhs.m_data)+strlen(m_data)+1];
  82. 82 strcpy(newString.m_data, m_data);
  83. 83 strcat(newString.m_data, rhs.m_data);
  84. 84 }
  85. 85 return newString;
  86. 86 }
  87. 87
  88. 88 char String::operator[](const unsigned int index)
  89. 89 {
  90. 90 std::cout << "[]" << std::endl;
  91. 91 return m_data[index];
  92. 92 }
  93. 93
  94. 94 std::ostream &operator<<(std::ostream &out, const String &rhs)
  95. 95 {
  96. 96 out << rhs.m_data;
  97. 97 return out;
  98. 98 }
  99. 99
  100. 100 String::~String()
  101. 101 {
  102. 102 std::cout << "destructor" << std::endl;
  103. 103 delete []m_data;
  104. 104 }
  105. 105
  106. 106 int main(void)
  107. 107 {
  108. 108 const char *p = "hello, world";
  109. 109 String s = "hello, world"; // 构造函数隐式转换 调用非默认构造函数
  110. 110 String s1(p); // 调用非默认构造函数
  111. 111 String s2 = s1; // 调用非默认构造函数
  112. 112 String s3; // 调用默认构造函数
  113. 113 s3 = s1; // 调用赋值构造函数
  114. 114 String s4 = s3 + s1; // 调用+运算符,同时调用默认构造函数
  115. 115 bool flag(s1 == s2); // 调用==运算符
  116. 116 std::cout << s << std::endl;
  117. 117 std::cout << s1 << std::endl;
  118. 118 std::cout << s2 << std::endl;
  119. 119 std::cout << s3 << std::endl;
  120. 120 std::cout << flag << std::endl;
  121. 121 char result = s3[1]; // 调用[]运算符
  122. 122 std::cout << result << std::endl;
  123. 123 std::cout << s4 << std::endl;
  124. 124
  125. 125 return 0;
  126. 126 }

C++实现String类的更多相关文章

  1. 标准库String类

    下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...

  2. 自己实现简单的string类

    1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...

  3. C++ string类的实现

    c++中string类的实现 今天面试被考到了, 全给忘记了!!!   //string类的实现 #include <iostream> #include <string.h> ...

  4. String类的功能

    String类              标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...

  5. java基础复习:final,static,以及String类

    2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...

  6. String类和StringBuffer类的区别

    首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...

  7. 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

    Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...

  8. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  9. String类常用方法

    1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...

  10. 运用String类实现一个模拟用户登录程序

    package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...

随机推荐

  1. Spring框架——JDBC方式搭建项目

    学习Spring框架,使用JDBC的方式配置方式搭建一个项目,进行项目总结. 首先,采用MVC设计模式思想,搭建项目目录. 然后各个目录文件下面的相关源码附上: controller目录: impor ...

  2. Hadoop伪分布式环境搭建+Ubuntu:16.04+hadoop-2.6.0

    Hello,大家好 !下面就让我带大家一起来搭建hadoop伪分布式的环境吧!不足的地方请大家多交流.谢谢大家的支持 准备环境: 1, ubuntu系统,(我在16.04测试通过.其他版本请自行测试, ...

  3. 读取固定ResourceBundle

    private static ResourceBundle ssoBundle = ResourceBundle.getBundle("CASHHSSO");//默认根目录 pub ...

  4. 关于RabbitMQ的简单理解

    说明:想要理解RabbitMQ,需要先理解MQ是什么?能做什么?然后根据基础知识去理解RabbitMQ是什么.提供了什么功能. 一.MQ的简单理解 1. 什么是MQ? 消息队列(Message Que ...

  5. Codeforces Global Round 8 C. Even Picture(构造)

    题目链接:https://codeforces.com/contest/1368/problem/C 题意 构造一个只含有灰.白块的网格,要求: 所有灰块为一个连通块 每个灰块与偶数个灰块相邻 恰有 ...

  6. FZU - 1901 Period II (kmp)

    传送门:FZU - 1901 题意:给你个字符串,让你求有多少个p可以使S[i]==S[i+P] (0<=i<len-p-1). 题解:这个题是真的坑,一开始怎么都觉得自己不可能错,然后看 ...

  7. Codeforces Round #653 (Div. 3) B. Multiply by 2, divide by 6 (数学)

    题意:有一个数\(n\),每次操作可以使\(n*=2\)或\(n/=6\)(如果能被整除),求最少操作次数使得\(n=1\),如果不满足,输出\(-1\). 题解:我们只要看\(n\)的质因子即可,如 ...

  8. C++实现邻接表

    对于无向图(V0,V1),(V1,V2),(V2,V3),(V0,V2)对应的邻接表表示就是 在代码中,你要单独对V1.V2.V3创建一种结构体类型.在对后面的节点0,1,2,3创建一种结构体类型 代 ...

  9. 谈到云原生, 绕不开"容器化"

    传送门 什么是云原生? 云原生设计理念 .NET微服务 Containers 现在谈到云原生, 绕不开"容器". 在<Cloud Native Patterns>一书中 ...

  10. hive+postgres安装部署过程

    master节点安装元数据库,采用postgres:#useradd postgres#password postgressu - postgreswget https://ftp.postgresq ...