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. tomcat常见报错解决方法汇总

    报错一:内存泄漏,字眼This is very likely to create a memory leak. 解决方法:修改tomcat内存. 在tomcat/bin目录下,修改catalina.s ...

  2. mysql之各版本rpm包安装

    发现每次想用mysql的rpm包直接安装的时候,都会出现找不到对应的rpm包的情况,故记录一下查找过程 进入官网->downloads->community->mysql commu ...

  3. PDF阅读器关闭“使用手型工具阅读文章”功能

    1.问题描述 某些PDF文件打开时,光标显示的手型工具里面有个箭头,一点击鼠标左键,就跳转到下一页了.给阅读带来很多不便. 2.原因 因为这类PDF文档中带有"文章"(articl ...

  4. SpringBoot常用注解(一)

    控制层:controller 1.@RestController注解相当于@ResponseBody + @Controller合在一起的作用.即这样写每一个方法返回的是 JSON 类型的数据. 2. ...

  5. K8S从入门到放弃系列-(15)Kubernetes集群Ingress部署

    Ingress是kubernetes集群对外提供服务的一种方式.ingress部署相对比较简单,官方把相关资源配置文件,都已经集合到一个yml文件中(mandatory.yaml),镜像地址也修改为q ...

  6. 什么是REST 、RESTful 、RESTful API?

    介绍 自从Roy Fielding博士在2000年他的博士论文中提出Rest(Representational State Transfer)风格的软件架构模式后,REST就基本上迅速取代了复杂而笨重 ...

  7. python 之 网络编程(基于TCP协议的套接字通信操作)

    第八章网络编程 8.1 基于TCP协议的套接字通信 服务端套接字函数 s.bind() 绑定(主机,端口号)到套接字 s.listen() 开始TCP监听 s.accept() 被动接受TCP客户的连 ...

  8. tensorflow-简单的神经网络

    本次笔记是关于tensorflow1的代码,由于接触不久没有跟上2.0版本,这个代码是通过简单的神经网络做一个非线性回归任务,(如果用GPU版本的话第一次出错就重启) import tensorflo ...

  9. ARM中断深入分析几点

    ARM中断深入分析几点 1.程序发生中断后,是如何跳转到中断程序里面的? 2.执行完中断后,如何返回到原来被打断的地方接着执行呢? 3.ARM处理器的流水线结构对中断返回地址的计算有什么影响? 4.A ...

  10. Linux 下随机启动自己的应用 -请使用while(true) 不要Console.ReadKey()

    Linux 下随机启动 自己的应用 -请使用while(true) 不要Console.ReadKey() 开机启动脚本启动,某些程序无法启动 原因 例如写了一个服务,不能停止程序运行,所以主线程成不 ...