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.判断输入的用户名和密 ...
随机推荐
- Vagrant基本知识、基本操作
Vagrant基本知识.基本操作 一.介绍 二.安装Vagrant 三.安装到Windows 四.准备Boxes 五.基本操作 六.Vagrant常用命令 七.Vagrantfile 7.1 box ...
- Docker (error getsockopt: connection refused ,使用http无法使用 docker login 登录的问题)
因部署Harbor 镜像仓库,部署完了之后根据提示上传 images,需要使用docker login ip:port 进行登录, 登录的时候发现因为docker 默认是https,因为测试环 ...
- 使用Spring StateMachine框架实现状态机
spring statemachine刚出来不久,但是对于一些企业的大型应用的使用还是十分有借鉴意义的. 最近使用了下这个,感觉还是挺好的. 下面举个例子来说下吧: 创建一个Spring Boot的基 ...
- log4cplus安装测试
先介绍一下它的基本要素. Layouts :布局器,控制输出消息的格式. Appenders :挂接器,与布局器紧密配合,将特定格式的消息输出到所挂接的设备终端 (如屏幕,文件等等). Logge ...
- (24)bzip2命令:压缩文件(.bz2格式)&&bunzip2命令:bz2格式的解压缩命令
1.bzip2 命令同 gzip 命令类似,只能对文件进行压缩(或解压缩),对于目录只能压缩(或解压缩)该目录及子目录下的所有文件.当执行压缩任务完成后,会生成一个以".bz2"为 ...
- PHP-电脑搭建服务器
PHP-电脑搭建服务器 一 材料 花生壳 php mysql apache(我使用的是phpstudy) 二 实现 (一)phpstudy安装 (二)花生壳安装及认证 (二)相关设置 1 ...
- Java基础进阶
Java基础进阶J Object类 hashcode() toString() clone() getClass() notify() wait() equals() Random类 生成 随机数 U ...
- 2015 Multi-University Training Contest 10(9/11)
2015 Multi-University Training Contest 10 5406 CRB and Apple 1.排序之后费用流 spfa用stack才能过 //#pragma GCC o ...
- 【noi 2.7_7219】复杂的整数划分问题(算法效率)
题意:若干组数据,分别问 N划分成K个正整数之和的划分数目.N划分成若干个不同正整数之和的划分数目.N划分成若干个奇正整数之和的划分数目. 解法:请见我之前的一篇博文内的Article 2--[noi ...
- P1073 最优贸易(最短路)
题目描述 CC C国有n n n个大城市和m mm 条道路,每条道路连接这 nnn个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 mmm 条道路中有一部分为单向通行的道路,一部分为 ...