C++(三十八) — 字符串的函数重载—案例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++(三十八) — 字符串的函数重载—案例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3181字,纯文字阅读大概需要5分钟。
内容图文
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 = 0; m_p = new char[m_len + 1]; strcpy(m_p, ""); } MyString::MyString(const char *p) { if (p == NULL) { m_len = 0; m_p = new char[m_len + 1]; strcpy(m_p, ""); } else { m_len = strlen(p); m_p = new char[m_len + 1]; strcpy(m_p, p); } } //MyString s3 = s2; MyString::MyString(const MyString& s) { m_len = s.m_len; m_p = new char[m_len + 1]; strcpy(m_p, s.m_p); } MyString::~MyString() { if (m_p != NULL) { delete[]m_p; m_p = NULL; m_len = 0; } } MyString& MyString::operator=(const char* p) { if (m_p != nullptr)//释放旧的内存 { delete[] m_p; m_len = 0; } if (p == NULL) //根据p分配新的内存 { m_len = 0; m_p = new char[m_len + 1]; strcpy(m_p, ""); } else { m_len = strlen(p); m_p = new char[m_len + 1]; strcpy(m_p, p); } return *this; } MyString& MyString::operator=(const MyString &s) { if (m_p != nullptr)//释放旧的内存 { delete[] m_p; m_len = 0; } m_len = strlen(s.m_p); m_p = new char[s.m_len + 1]; 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 != 0) { 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++(三十八) — 字符串的函数重载—案例全部内容,希望文章能够帮你解决C++(三十八) — 字符串的函数重载—案例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。