C++(三十七) — 字符串的函数重载—案例
2024-10-21 16:29:46
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#的IPAddress IPEndPoint
- 浩瀚科技PDA移动开单|盘点机 数据采集器 条码扫描开单微POS软件 现场打印开单
- python中lambda表达式应用
- WPF Wonders: Transformations (and Robots!)
- 【C#】窗体动画效果
- EntityFramework5.0CodeFirst全面学习
- 转载:centos上yum安装apache+php+mysql等
- 双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计
- Java中类的初始化
- maven项目部署到Repository(Nexus)
- 设计比较好,有助于学习的Github上的iOS App源码 (中文)
- @EnableTransactionManagement注解理解
- 小程序视频播放组件video
- email program (客户端)演变过程有感
- React 入门学习笔记整理目录
- c# Newtonsoft.Json封装
- mysqlreport工具
- Tomcat服务器环境变量配置及在Eclipse中启动和配置
- 尚学堂java 参考答案 第七章
- js 压缩 预览 上传图片
热门文章
- [LeetCode] 280. Wiggle Sort 摆动排序
- kindeditor——开源的HTML可视化编辑器
- GIT 基础 &本地服务器搭建
- Java大厂笔试&;&;面试集合大全目录
- adb常用命令总结
- c++基础(五)——关联容器
- 少儿编程|Scratch编程教程系列合集,总有一款适合你
- 【转】使用Dockerfile构建镜像并push到私有仓库
- DataGrip&#160;License server
- 让table中的td不会被过长的文字撑开,并且自动出现省略号