字符串类_运算符重载

来源:互联网 发布:卡通人物制作的软件 编辑:程序博客网 时间:2024/05/17 03:41


MyString.h


#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;//在这里定义类只写类的函数名,不写类的函数体//C中没有字符串,字符串(C风格字符串)//空串class MyString{public:MyString();MyString(const char *p );MyString(const MyString& s);~MyString();public://重载等号操作符//原型1  //s4= "s2222"MyString& MyString::operator=(const char *p);//原型2  //s4 = s2;MyString& MyString::operator=(const MyString &s);char& operator[](int index);//重载<<friend ostream& operator<<(ostream &out,MyString &s);//重载>>friend istream& operator>>(istream &in ,MyString &s);public://重载 == !=bool operator==(const char *p) const;bool operator==(const MyString& s) const;bool operator!=(const char *p)const;bool operator!=(const MyString& s) const ;public://重载 >  < int operator<(const char *p);int operator>(const char *p);int operator<(const MyString& s);int operator>(const MyString& s);public:char *c_str(){return m_p;}const char *c_str2(){return m_p;}int length(){return m_len;}protected:private:int m_len;char *m_p;};



MyString.cpp


#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 != NULL){delete [] m_p;m_len = 0;}//根据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);}return *this;}//原型2  //s4 = s2;MyString& MyString::operator=(const MyString &s){//旧内存释放掉if (m_p != NULL){delete [] m_p;m_len = 0;}//根据s分配内存m_len = s.m_len;m_p = new char[m_len + 1];strcpy(m_p ,s.m_p);return *this;}ostream& operator<<(ostream &out,MyString &s){out<<s.m_p;return out;}char& MyString::operator[](int index){return m_p[index];}//重载== != //按alt,向下滑//if (s2 == "s22222")bool MyString::operator==(const char *p) const{if (p == NULL){if (m_len == 0){return true;}else{return false;}}else{//strcmp比较两个字符串//若str1==str2,则返回零;//若str1<str2,则返回负数;//若str1>str2,则返回正数。if (m_len == strlen(p)){return !strcmp(m_p,p);}else{return false;}}}bool MyString::operator!=(const char *p) const{return !(*this == p);} bool MyString::operator==(const MyString& s) const{if (m_len != s.m_len){return false;}if (strcmp(m_p,s.m_p)){return true;}}bool MyString::operator!=(const MyString& s) const{return !(*this == s);}//重载  <  > //if (s3 < "bbbb")int  MyString::operator<(const char *p){return strcmp(this->m_p,p);}int  MyString::operator>(const char *p){return strcmp(p,this->m_p);}int  MyString::operator<(const MyString& s){return strcmp(this->m_p,s.m_p);}int  MyString::operator>(const MyString& s){return strcmp(s.m_p,m_p);}istream& operator>>(istream &in ,MyString &s){cin>>s.m_p;return in;}


MyString_Test.cpp


#include<iostream>using namespace std;#include "MyString.h"int main01(){MyString s1;MyString s2("s2");MyString s2_2 = NULL;MyString s3 = s2;MyString s4 = "s4444444";//测试运算符重载 和 重载 【】//  =  s4 = s2;s4= "s2222";s4[1] = '4';printf("%c",s4[1]);//第一步承认它是一个等号//原型1//MyString& operator=(const char *p)//原型2//MyString& operator=(const MyString &s)//s4[0];//char& operator[](int index)cout<< s4 <<endl;//ostream& operator<<(ostream &out,MyString &s)system("pause");return 0;}int main02(){MyString s1;MyString s2("s2");MyString s3 = s2;//bool operator==(const char *p);//bool operator==(const MyString& s);//bool operator!=(const char *p);//bool operator!=(const MyString& s);if (s2 == "s22222"){printf("相等\n");}else{printf("不相等\n");}if (s3 == s2){printf("相等\n");}else{printf("不相等\n");}system("pause");return 0;}int main03(){MyString s1;MyString s2("s2");MyString s3 = s2;s3 = "aaa";//int operator<(const char *p)//int operator>(const char *p)//int operator<(const Mystring& s)//int operator>(const  Mystring& s)if (s3 < "bbbb"){printf("小于\n");}else{printf("大于\n");}/*if (s3 < s2){printf("小于\n")}else{printf("大于\n");}*//*MyString s4 = "aaaaffff";strcmp(s4.c_str(),"aa111");cout<<s4<<endl;*/system("pause");return 0;}/*还是有问题*///int main()//{//MyString s1(128);//cin>>s1;//////istream& operator>>(istream &in ,MyString &s);////system("pause");//return 0;//}






0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 闲鱼退货卖家收到货不退款怎么办 没收到货申请退款卖家不同意怎么办 咸鱼买家不确认收货老退款怎么办 买家买了确认收货然后退款怎么办 买家收确认收货后还申请退款怎么办 淘宝收到货后买家故意说少货怎么办 对面商铺放很大声音乐怎么办 在店面住隔壁商铺一直放音乐怎么办 淘宝卖家把买家评论删了怎么办 淘宝店铺上当顾客说再看看怎么办 车辆摇号申请说手机号注册怎么办 如果在微商手上买到假东西怎么办? 登陆微信显示版本过低该怎么办 苹果手机登陆微信版本过低怎么办 微信版本过低无法登怎么办录 登入微信显示版本过低登不上怎么办 微信版本过低无法使用小程序怎么办 小米4s微信反应很慢怎么办 手机网页缓存的视频播放不了怎么办 ios微信占用内存太大了怎么办 爱奇艺离线视频显示暂无缓存怎么办 酷狗音乐不小心删了歌怎么办 苹果手机里系统占的空间太大怎么办 酷狗音乐歌单里有不同步的歌怎么办 一插耳机手机自带音乐就响怎么办 清理空间时不小心把图片删了怎么办 手机中清理误把照片删了怎么办 清理手机文件把照片给删了怎么办 清理手机不小心把照片删了怎么办 金牛不回微信我也不理他他会怎么办 华为手机微信文件自动删除怎么办 微信清理数据后不能登录了怎么办 k歌占内存又不想删除歌曲怎么办 把所有商品放在一起做链接怎么办 微信解冻短信验证总显示失败怎么办 淘金币能抵钱商家拿了淘金币怎么办 真实订单被系统判定虚假交易怎么办 淘宝买家号疑似虚假交易违规怎么办 货品交易一方收了定金违约了怎么办 饿了么店铺收到差评怎么办 淘宝顾客退款没成功给差评怎么办