C++实现String类
1 #include<iostream>
2 #include<cstring>
3
4 class String
5 {
6 public:
7 String();
8 String(const char *str);
9 String(const String &rhs);
10 ~String();
11
12 String &operator=(const String &rhs);
13 String operator+(const String &rhs);
14 char operator[](const unsigned int index);
15 bool operator==(const String &rhs);
16 friend std::ostream &operator<<(std::ostream &out, const String &rhs);
17 private:
18 char *m_data;
19 };
20
21 String::String()
22 {
23 std::cout << "default constructor" << std::endl;
24 m_data = new char[1];
25 m_data[0] = '\0';
26 }
27
28 String::String(const char *str)
29 {
30 std::cout << "non-default constructor" << std::endl;
31 if (NULL == str)
32 {
33 m_data = new char[1];
34 m_data[0] = '\0';
35 }
36 else
37 {
38 m_data = new char[strlen(str)+1];
39 strcpy(m_data, str);
40 }
41 }
42
43 String::String(const String &another)
44 {
45 std::cout << "copy constructor" << std::endl;
46 m_data = new char[strlen(another.m_data)+1];
47 strcpy(m_data, another.m_data);
48 }
49
50 bool String::operator==(const String &rhs)
51 {
52 std::cout << "bool == " << std::endl;
53 int result = strcmp(m_data, rhs.m_data);
54 if (0 == result)
55 return true;
56 else
57 return false;
58 }
59
60 String &String::operator=(const String &rhs)
61 {
62 std::cout << "assign constructor" << std::endl;
63 if (this == &rhs)
64 return *this;
65 delete []m_data;
66 m_data = new char[strlen(rhs.m_data)+1];
67 strcpy(m_data, rhs.m_data);
68 return *this;
69 }
70
71 String String::operator+(const String &rhs)
72 {
73 std::cout << "+" << std::endl;
74 String newString;
75 if (NULL == rhs.m_data)
76 newString = *this;
77 else if(NULL == m_data)
78 newString = rhs;
79 else
80 {
81 newString.m_data = new char[strlen(rhs.m_data)+strlen(m_data)+1];
82 strcpy(newString.m_data, m_data);
83 strcat(newString.m_data, rhs.m_data);
84 }
85 return newString;
86 }
87
88 char String::operator[](const unsigned int index)
89 {
90 std::cout << "[]" << std::endl;
91 return m_data[index];
92 }
93
94 std::ostream &operator<<(std::ostream &out, const String &rhs)
95 {
96 out << rhs.m_data;
97 return out;
98 }
99
100 String::~String()
101 {
102 std::cout << "destructor" << std::endl;
103 delete []m_data;
104 }
105
106 int main(void)
107 {
108 const char *p = "hello, world";
109 String s = "hello, world"; // 构造函数隐式转换 调用非默认构造函数
110 String s1(p); // 调用非默认构造函数
111 String s2 = s1; // 调用非默认构造函数
112 String s3; // 调用默认构造函数
113 s3 = s1; // 调用赋值构造函数
114 String s4 = s3 + s1; // 调用+运算符,同时调用默认构造函数
115 bool flag(s1 == s2); // 调用==运算符
116 std::cout << s << std::endl;
117 std::cout << s1 << std::endl;
118 std::cout << s2 << std::endl;
119 std::cout << s3 << std::endl;
120 std::cout << flag << std::endl;
121 char result = s3[1]; // 调用[]运算符
122 std::cout << result << std::endl;
123 std::cout << s4 << std::endl;
124
125 return 0;
126 }
C++实现String类的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- (19)ln命令:在文件之间建立链接(硬链接和软链接)
1.ext 文件系统(Linux 文件系统)是如何工作的. 我们在前面讲解了分区的格式化就是写入文件系统,而 Linux 目前使用的是 ext4 文件系统.如果用一张示意图来描述 ext4 文件系统 ...
- nginx教程<一>
2020最新Nginx教程全面讲解教程,感觉讲的很不错但是需要有docker基础,因为是基于docker快速搭建的nginx. 1.为什么要学习Nginx 肯定是工作和业务需求催生的学习需要哈哈,不过 ...
- 整理我的Git常见问题和命令
整理我的Git常见问题和命令 目录 整理我的Git常见问题和命令 提交注释规范 合并分支 clone & 切换分支 支持中文路径显示 账户及密码 基于远程分支创建本地分支 提交注释规范 举例: ...
- spring源码学习笔记之容器的基本实现(一)
前言 最近学习了<<Spring源码深度解析>>受益匪浅,本博客是对学习内容的一个总结.分享,方便日后自己复习或与一同学习的小伙伴一起探讨之用. 建议与源码配合使用,效果更嘉, ...
- 用鸿蒙开发AI应用(八)JS框架访问内核层
目录:前言JS应用开发框架原理内置模块实现ace模块开发界面程序 前言上回说到,用C++来写UI界面的开发效率不如JS+HTML来的高,但设备开发又免不了要通过内核态来操作硬件,这里我们就要先打通从J ...
- HDU-4773 Problem of Apollonius (圆的反演)
参考: https://oi-wiki.org/geometry/inverse/ https://blog.csdn.net/acdreamers/article/details/16966369 ...
- 2019牛客国庆集训派对day2
A(模拟): #include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double ...
- P3384 [模板] 树链剖分
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, m, rt, mod, cnt, to ...
- Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)
题目链接:https://codeforces.com/contest/1362/problem/C 题意 计算从 $0$ 到 $n$ 相邻的数二进制下共有多少位不同,考虑二进制下的前导 $0$ .( ...
- P1091 合唱队形(LIS)
题目描述 NNN位同学站成一排,音乐老师要请其中的(N−KN-KN−K)位同学出列,使得剩下的KKK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,-,K1,2, ...