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类的更多相关文章

  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. js部分知识整理,google浏览器的代码调试

    整理一些学过的js知识点,包括js中3个括号的含义,this的使用,递归,google浏览器的代码调试.Location的属性及常用方法,window对象常用方法,open方法等. js括号 在js中 ...

  2. Java泛型中的通配符T,E,K,V

    Java泛型中的通配符T,E,K,V 1.泛型的好处 2.泛型中的通配符 2.1 T,E,K,V,? 2.2 ?无界通配符 2.3 上界通配符 < ? extends E> 2.4 下界通 ...

  3. hadoop(集群)完全分布式环境搭建

    一,环境 主节点一台: ubuntu desktop 16.04 zhoujun      172.16.12.1 从节点(slave)两台:ubuntu server 16.04 hadoop2  ...

  4. PA防火墙抓包结果显示重传(re-transmission)

    问题起因: 部分内网服务器调用外网站点抓取图片时出现缓慢及超时现象. 由于是由内向外方向的访问,且通过的应用层设备只有防火墙:而且用其他网段测试机测试的时候发现并没有上述访问缓慢或超时. 从防火墙抓包 ...

  5. C - Oulipo

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  6. 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)

    题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...

  7. 【2020杭电多校】 Lead of Wisdom、The Oculus

    题目链接:Lead of Wisdom 题意:有n个物品,这些物品有k种类型.每种物品有对应的类型ti,其他值ai,bi,ci,di 你可以选择一些物品,但是这些物品要保证它们任意两者之间类型不能相同 ...

  8. vs2019激活码

    Visual Studio 2019 Enterprise BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio 2019 Professional NYWVH-HT ...

  9. Linux 搭建网站

    wget http://dl.wdlinux.cn/lanmp_laster.tar.gz tar zxvf lanmp_laster.tar.gz sh lanmp.sh https://www.w ...

  10. mysql(二)--mysql索引剖析

    1.1. 索引是什么 1.1.1.索引图解 维基百科对数据库索引的定义: 数据库索引,是数据库管理系统(DBMS)中一个排序的数据结构,以协助快速查询.更新数据库表中数据. 怎么理解这个定义呢?  首 ...