My_String类案例(构造、析构、重载'=' '[]' '<<' '==' '!=' '>' '<' '>>' 运算、操作符以及其他技巧)(重载完结)
来源:互联网 发布:网络直播举报 编辑:程序博客网 时间:2024/06/06 15:21
这是一个字符串类的小案例
头文件 MyString.h
#ifndef MYSTRING_H_INCLUDED#define MYSTRING_H_INCLUDED#include <iostream>using namespace std;//c中没有字符串 (字符串类 c风格的字符串)//空串class MyString{private: int m_len; char *m_p; friend ostream& operator<<(ostream& out, const MyString &obj); friend istream& operator>>(istream& in, MyString &obj);public: MyString(); MyString(const char *p); MyString(const MyString &s); ~MyString(); MyString& operator=(const MyString &obj);//用另一个的对象去等 MyString& operator=(const char *p);//用字符串去等 char operator[](int x); bool operator==(const MyString &obj); bool operator!=(const MyString &obj); bool operator>(const MyString &obj);//与另一个对象去比较 bool operator>(const char *p);//与字符串去比较 bool operator<(const MyString &obj);//与另一个对象去比较 bool operator<(const char *p);//与字符串去比较public: //技巧:怎么样把类的私有属性露出来 char* c_str() { return m_p; } //假设露出来的指针变成只读属性 const char* c_str2() { return m_p; } int length() { return m_len; }};#endif // MYSTRING_H_INCLUDED
MyString.cpp
#include <string.h>#include "MyString.h"//无参构造函数//MyString s1;MyString::MyString(){ m_len = 0; m_p = new char [m_len+1]; strcpy(m_p, "");}//有参构造函数//MyString s2("demo");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 s("fire");//t = s;//t = "cap";MyString& MyString::operator=(const MyString &obj){ if(m_p != NULL){//旧内存释放 delete [] m_p; m_p = NULL; m_len = 0; } m_len = obj.m_len; m_p = new char[m_len+1];; strcpy(m_p, obj.m_p); return *this;}MyString& MyString::operator=(const char *p){ if(m_p != NULL){//旧内存释放 delete [] m_p; m_p = NULL; m_len = 0; } 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); } return *this;}//重载 [] 运算符//cout << "t[1]: " <<t[1] << endl;char MyString::operator[](int x){ if(x < 0 || x >= m_len) throw "询问越界"; else{ return m_p[x]; }}//重载 << 操作符// cout << s1 << s2 << s3 << t << endl;ostream& operator<<(ostream& out, const MyString &obj){ /* for(int i = 0; i < obj.m_len; i++) out << obj.m_p[i]; */ out << obj.m_p; return out;}//重载 == 和 != 逻辑运算符bool MyString::operator==(const MyString &obj){ if(m_len != obj.m_len) return false; else{ for(int i = 0; i < m_len; i++) if(m_p[i] != obj.m_p[i]) return false; } return true;}bool MyString::operator!=(const MyString &obj){ if((*this) == obj) return false; return true;}//重载 > < 运算符bool MyString::operator>(const MyString &obj){ for(int i = 0; i < m_len && i < obj.m_len; i++){ if(m_p[i] > obj.m_p[i]) return true; if(m_p[i] > obj.m_p[i]) return false; } if(m_len > obj.m_len) return true; else return false;}bool MyString::operator<(const MyString &obj){ if((*this) > obj || (*this) == obj) return false; return true;}bool MyString::operator>(const char *p){ int len = strlen(p); for(int i = 0; i < m_len && i < len; i++){ if(m_p[i] > p[i]) return true; if(m_p[i] > p[i]) return false; } if(m_len > len) return true; else return false;}bool MyString::operator<(const char *p){ if((*this) > p || (*this) == p) return false; return true;}//重载 >> 操作符istream& operator>>(istream &in, MyString &obj){ if(obj.m_p != NULL){ delete [] obj.m_p; obj.m_p = NULL; obj.m_len = 0; } obj.m_p = new char[1000]; in >> obj.m_p; obj.m_len = strlen(obj.m_p); return in;}main.cpp
#include <iostream>#include <string.h>#include "MyString.h"using namespace std;int main(){ //构造 and 析构 MyString s1; MyString s2("demo"); MyString s3 = s2; //重载 = 运算符 MyString t; MyString s("fire"); t = s; t = "cap"; //重载 [] 操作符 cout << "t[1]: " << t[1] << endl; //重载左移操作符 cout << "s:" << s << "\ns1:" << s1 << "\ns2: " << s2 << "\nt: " << t << endl; //重载 == 和 != 逻辑运算符 if(s1 == s2) cout << "s1 " << "==" << " s2" << endl; else cout << "s1 " << "!=" << " s2" << endl; if(s1 != s2) cout << "s1 " << "!=" << " s2" << endl; else cout << "s1 " << "==" << " s2" << endl; //重载 > 和 < 运算符 if(s1 > s2) cout << "s1 > s2" << endl; else{ if(s1 == s2) cout << "s1 = s2" << endl; else cout << "s1 < s2" << endl; } //其他技巧 //假设我要实现: strcpy(s.m_p, "aaa111") 苦于指针是私有变量而无法实现 //技巧:怎么样把类的私有属性露出来 strcpy(s.c_str(), "f");//MFC cout << "s:" << s << endl; //重载 >> 操作符 //首先,我们要分配好内存(在构造函数中进行) cout << "请用户输入 s : "; cin >> s; cout << "s:" << s << endl; return 0;}
下面是运行结果
0 0
- My_String类案例(构造、析构、重载'=' '[]' '<<' '==' '!=' '>' '<' '>>' 运算、操作符以及其他技巧)(重载完结)
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- 数组类小案例(重载 '[]' '=' '==' 和 '!=' 运算符)(进阶4)
- 重载=运算符
- +=运算符重载
- 重载“=”运算符
- 重载运算符=
- 【C++】复制构造函数(拷贝构造函数)和=运算符重载使用的区别
- C++重载赋值运算符(=)
- 关于拷贝构造函数和重载成员运算符=
- c++中拷贝构造函数与“=运算符重载”
- 复制构造函数和=运算符重载的区别
- C++语法,复制构造函数与=运算符重载
- 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
- 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
- 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
- C++中几个特殊运算符的重载(= 、++、--、下标运算符的重载)
- == 运算符重载
- 多线程
- js 各类型和bool型比较
- liferay学习2--用户体系及权限体系简单说明
- 基于注解的spring mvc controller
- GPCC修改默认登出时间
- My_String类案例(构造、析构、重载'=' '[]' '<<' '==' '!=' '>' '<' '>>' 运算、操作符以及其他技巧)(重载完结)
- JavaScript中如何严格的判断NaN
- java中实现数组转化为字符串
- PAT(B).1028. 人口普查(20)
- 黑盒测试的方法
- 最小费用最大流(板子)
- stm32控制2.4G芯片的应用
- Android 实现多个输入框的对话框
- HDU 1875 畅通工程再续 有限制的最小生成树