C++(三十七) — 字符串的函数重载—案例
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"); }