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.判断输入的用户名和密 ...
随机推荐
- springboot注解开发
可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使用 SpringBoot 来开发项 ...
- EF6.2加载速度慢的解决方案
最近的项目中一直有反馈,EF在第一次启动之后调用的话,加载速度很慢,在网上搜索了一下,基本就是三种解决方案. 在程序启动的时候将映射视图缓存下来. 使用Ngen生成EF的本地镜像. IIS8内置功能 ...
- SQL(replace)替换字段中指定的字符
语法:update 表名 set 字段名=REPLACE(字段名,'修改前的字符','修改后的字符') 例 Product商品表中Name 名字字段中描述中将'AAA' 修改成 'BBB' SQL语句 ...
- P3381 [模板] 最小费用最大流
EK + dijkstra (2246ms) 开氧气(586ms) dijkstra的势 可以处理负权 https://www.luogu.org/blog/28007/solution-p3381 ...
- 【noi 2.6_1808】最长公共子序列(DP)
题意:给2个字符串求其最大公共子序列的长度.解法:这个和一般的状态定义有点不一样,f[i][j]表示 str 前i位和 str2 前j的最大公共子序列的长度,而不是选 str 的第i位和 str2 的 ...
- P1268 树的重量(板子)
题目: 题目描述 树可以用来表示物种之间的进化关系.一棵"进化树"是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题是,根据物种之 ...
- - Visible Trees HDU - 2841 容斥原理
题意: 给你一个n*m的矩形,在1到m行,和1到n列上都有一棵树,问你站在(0,0)位置能看到多少棵树 题解: 用(x,y)表示某棵树的位置,那么只要x与y互质,那么这棵树就能被看到.不互质的话说明前 ...
- POJ - 1654 利用叉积求三角形面积 去 间接求多边形面积
题意:在一个平面直角坐标系,一个点总是从原点出发,但是每次移动只能移动8个方向的中的一个并且每次移动距离只有1和√2这两种情况,最后一定会回到原点(以字母5结束),请你计算这个点所画出图形的面积 题解 ...
- Linux入门详解
Linux基础知识 Linux&Unix 说起Linux,就不得不提Unix操作系统. Unix系统号称世界上最稳定的系统,就连苹果公司也从中获取灵感开发出了移动端大名鼎鼎的IOS. Unix ...
- 字节笔试题 leetcode 69. x 的平方根
更多精彩文章请关注公众号:TanLiuYi00 题目 解题思路 题目要求非负整数 x 的平方根,相当于求函数 y = √x 中 y 的值. 函数 y = √x 图像如下: 从上图中,可以看出函数是单 ...