1、MyString.h 头文件

#pragma once

#include <iostream>
using namespace std; class MyString
{
public:
MyString();
MyString(const char *p);
MyString(const MyString& s);
~MyString(); public:
MyString& operator=(const char* p);
MyString& operator=(const MyString &s);
char& operator[](int index);
// 重载左移操作符、右移操作符,注意区别
friend ostream& operator<<(ostream &out, MyString &s);
friend istream& operator>>(istream &in, MyString &s);
//重载双等号和不等号
bool operator==(const char* p);
bool operator!=(const char* p);
bool operator==(const MyString& s );
bool operator!=(const MyString& s);
//重载大于和小于操作符
int operator<(const char* p);
int operator>(const char* p);
int operator<(const MyString &s);
int operator>(const MyString &s); // 把类的指针 单独使用,通过 s1.c_str() 来调用
char *c_str()
{
return m_p;
} private:
int m_len;
char *m_p;
};

2、MyString.cpp 函数实现文件

#define  _CRT_SECURE_NO_WARNINGS
#include "MyString.h" MyString::MyString()
{
m_len = ;
m_p = new char[m_len + ];
strcpy(m_p, "");
} MyString::MyString(const char *p)
{
if (p == NULL)
{
m_len = ;
m_p = new char[m_len + ];
strcpy(m_p, "");
}
else
{
m_len = strlen(p);
m_p = new char[m_len + ];
strcpy(m_p, p);
}
} //MyString s3 = s2;
MyString::MyString(const MyString& s)
{
m_len = s.m_len;
m_p = new char[m_len + ];
strcpy(m_p, s.m_p);
} MyString::~MyString()
{
if (m_p != NULL)
{
delete[]m_p;
m_p = NULL;
m_len = ;
}
} MyString& MyString::operator=(const char* p)
{
if (m_p != nullptr)//释放旧的内存
{
delete[] m_p;
m_len = ;
}
if (p == NULL) //根据p分配新的内存
{
m_len = ;
m_p = new char[m_len + ];
strcpy(m_p, "");
}
else
{
m_len = strlen(p);
m_p = new char[m_len + ];
strcpy(m_p, p);
}
return *this;
}
MyString& MyString::operator=(const MyString &s)
{
if (m_p != nullptr)//释放旧的内存
{
delete[] m_p;
m_len = ;
}
m_len = strlen(s.m_p);
m_p = new char[s.m_len + ];
strcpy(m_p, s.m_p);
return *this;
} char& MyString::operator[](int index)
{
return m_p[index];
} ostream& operator<<(ostream &out, MyString &s)
{
out << s.m_p;
return out;
}
istream& operator>>(istream &in, MyString &s) {
cin >> s.m_p;
return in;
} bool MyString::operator==(const char* p)
{
if (p == nullptr)
{
if (m_len != )
{
return false;
}
else
return true;
}
else
{
if (m_len == strlen(p))
{
return !strcmp(m_p, p);
}
else
return false;
}
}
bool MyString::operator!=(const char *p)
{
return !(*this == p);
} bool MyString::operator==(const MyString& s)
{
if (m_len != s.m_len)
return false;
return strcmp(m_p,s.m_p);
}
bool MyString::operator!=(const MyString& s)
{
return !(*this == s);
}
//if(s3<"bb"")
int MyString::operator<(const char* p){
return strcmp(m_p, p);
}
int MyString::operator>(const char* p) {
return strcmp(p, m_p);
}
int MyString::operator<(const MyString &s) {
return strcmp(m_p, s.m_p);
}
int MyString::operator>(const MyString &s) {
return strcmp(s.m_p, m_p);
}

3、test.cpp 测试文件

#include <iostream>
using namespace std;
#include "MyString.h" //void main01()
//{
// MyString s1;
// MyString s2("s2");
// MyString s2_2 = NULL;
// MyString s3 = s2;
//
// MyString s4 = "s4";
// s4 = s2;
// s4[1] = '9';
// cout << s4[1] << endl;
// cout << s4 << endl;//左移操作符的重载
//
// system("pause");
//}
void main()
{
MyString s1;
MyString s2("s2");
MyString s3 = NULL;
cout << (s2 > "s") << endl;
cin >> s2;
cout << s2 << endl;
system("pause");
}

C++(三十七) — 字符串的函数重载—案例的更多相关文章

  1. php 之 类,对象(三)多态性,函数重载,克隆

    一.三大特性之三 多态性(在php中表象不明显)1.概念:当父类引用指向子类实例时,由于子类对父类函数进行了重写,导致我们在使用该引用去调用相应的方法显示出的不同.2.发生条件:1.必须有继承 2. ...

  2. python的基本用法(三)字符串常用函数

    字符串常用函数 # s='.abcd.'# new_s=s.strip('.')#默认去掉字符串两边的空格和换行符,想去掉什么括号中就写什么# print('s',s)# print('new_s', ...

  3. SQLServer截取字符串常用函数

    SQL Server中一共提供了三个字符串截取函数:LEFT().RIGHT().SUBSTRING(). 一.LEFT()函数 函数说明如下: 语法:LEFT(character,integer). ...

  4. Sql server函数的学习2(游标函数、日期函数、字符串操纵函数)

    一.游标函数与变量 游标可以处理多行数据,在过程循环中一次访问一行.和基于集合的高效操作相比,这个功能对系统资源的消耗更大. 可以用一个函数和两个全局变量来管理游标操作 1.CURSOR_STATUS ...

  5. PHP视频教程 字符串处理函数(三)

    字符串替换函数: str_replace() 替换字符串或数组元素,区分大小,第四个参数可选用于统计替换次数. str_ireplace() 不区分大小写替换 字符串函数比较 strcmp()比较字符 ...

  6. Javascript函数重载,存在呢—还是存在呢?

    1.What's is 函数重载? );//Here is int 10 print("ten");//Here is string ten } 可以发现在C++中会根据参数的类型 ...

  7. JS魔法堂:函数重载 之 获取变量的数据类型

    Brief 有时我们需要根据入参的数据类型来决定调用哪个函数实现,就是说所谓的函数重载(function overloading).因为JS没有内置函数重载的特性,正好给机会我们思考和实现一套这样的机 ...

  8. C++模板元编程 - 函数重载决议选择工具(不知道起什么好名)完成

    这个还是基于之前实现的那个MultiState,为了实现三种类型“大类”的函数重载决议:所有整数.所有浮点数.字符串,分别将这三种“大类”的数据分配到对应的Converter上. 为此实现了一些方便的 ...

  9. c++函数重载---2

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 写在前面: 函数重载的重要性不言而明,但是你知道C++中函数重载是如何实现的呢(虽然本文谈的是C++中函 ...

随机推荐

  1. Laya自定义组件

    laya2.1.1.1 参考: 预设使用 一 没有自定义组件 教程翻了几遍,没有自定义组件,论坛搜了下,说是不能使用. 二 预置件做自定义组件 预置件无法右键创建. 又去翻教程.终于知道预置件怎么创建 ...

  2. php_mvc实现步骤八

    shop34-10-框架类 框架类(框架初始化类) 将原来入口文件中功能,放在该类中完成,入口文件变得简单,轻量! 将入口文件中的各个功能,由框架类的各个方法,完成: 为了简单化,使用纯静态的类.(看 ...

  3. setInterval定时器停止后,再重新启动

    1.数据自动滚动显示(动态添加) <li> <div class="FULeTi"> <div class="SLeName"&g ...

  4. [转帖]说一说JVM双亲委派机制与Tomcat

    说一说JVM双亲委派机制与Tomcat https://www.cnblogs.com/dengchengchao/p/11844022.html 讲个故事: 以前,爱捣鼓的小明突然灵机一动,写出了下 ...

  5. 1.JVM前奏篇(看官网怎么说)

    JVM(Java Virtual Machine) 前奏篇(看官网规范怎么说) 1.The relation of JDK/JRE/JVM 在下图中,我们所接触的,最熟悉,也是经常打交道的 最顶层 J ...

  6. CF991E Bus Number

    题意翻译 给你一个数字序列A(长度不超过18位),问有多少个序列B满足①A中所有数字都一定要在B中出现过:②B中所有数字也一定要在A中出现过:③序列B不能以0开头 输入 #1 97 输出 #1 2 解 ...

  7. bzoj 4500 矩阵 题解

    题意: 有一个 $ n * m $ 的矩阵,初始每个格子的权值都为 $ 0 $,可以对矩阵执行两种操作: 选择一行,该行每个格子的权值加1或减1. 选择一列,该列每个格子的权值加1或减1. 现在有 $ ...

  8. template模板语言

    模板渲染 通过views视图函数对html页面进行渲染 标签{{ 变量 }}/标签 {% 逻辑 %} -- 标签 万能的点 <h1>91李业网</h1> <h2>{ ...

  9. docker深入学习二

    dicker:数据管理 数据管理机制 docker使用union file system来管理数据,docker构建image和container也是采用了同样的技术. image层次 iamge由多 ...

  10. Modelsim——do脚本、bat命令

    一.do脚本实现自动化仿真 Modelsim是支持命令的,我们可以用 .do 文件将这些命令先写好然后在Modelsim上调用.因为我的编辑器不支持.do的语法,所以这里改用 .tcl文件,它和 .d ...