Mystring.h

#ifndef MYSTRING_H
#define MYSTRING_H
#include<iostream>
using namespace std;class Mystring
{
public:Mystring();Mystring(const char *s);Mystring(const Mystring &other);     //拷贝构造bool empty();                        //判空函数int  str_size();string c_str();char &at(int pos);Mystring & operator = (const Mystring &R);char &operator[](int n);Mystring operator+(const Mystring &R);Mystring& operator+=(const Mystring &R);bool operator==(const Mystring &R);bool operator!=(const Mystring &R);bool operator>(const Mystring &R);bool operator<(const Mystring &R);bool operator>=(const Mystring &R);bool operator<=(const Mystring &R);friend istream &operator>>(istream &L,const Mystring &R);friend ostream &operator<<(ostream &L ,const Mystring &R);~Mystring();
private:char *str;int size;
};

Mystring.cpp

#include "mystring.h"
#include<iostream>
#include<cstring>
using namespace std;
Mystring::Mystring():size(10)
{str = new char[size];strcpy(str," ");
}Mystring::Mystring(const char *s)
{size = strlen(s);str = new char[size+1];strcpy(str,s);
}Mystring::Mystring(const Mystring &other):size(other.size)
{str = new char[other.size];strcpy(str,other.str);
}bool Mystring::empty()
{return !strlen(str);
}int Mystring::str_size()
{return size;
}string Mystring::c_str()
{return this->str;
}char & Mystring::at(int pos)
{if(pos < 0 || pos >= size){throw std::out_of_range("Index out of range");}char &s = str[pos];return s;
}Mystring& Mystring::operator=(const Mystring &R)
{// 判断自我赋值if (this == &R)return *this;delete []str;Mystring temp;this->size = R.size;this->str = new char[size+1];strcpy(str,R.str);return *this;
}char & Mystring::operator[](int n)
{return str[n] ;
}Mystring Mystring::operator+(const Mystring &R)
{Mystring temp;temp.size = this->size + R.size -1;temp.str = new char[temp.size+1];strcpy(temp.str,this->str);strcat(temp.str,R.str);return temp;
}Mystring& Mystring::operator+=(const Mystring &R)
{size += R.size;Mystring temp;strcpy(temp.str,this->str);delete []this->str;this->str = new char[size+1];strcpy(this->str,temp.str);strcat(this->str,R.str);return *this;
}bool Mystring::operator==(const Mystring &R)
{return !strcmp(this->str,R.str);
}bool Mystring::operator!=(const Mystring &R)
{return strcmp(this->str,R.str);
}bool Mystring::operator>(const Mystring &R)
{return strcmp(this->str,R.str) > 0;
}
bool Mystring::operator<(const Mystring &R)
{return strcmp(this->str,R.str) < 0;
}bool Mystring::operator>=(const Mystring &R)
{return strcmp(this->str,R.str) >= 0;
}
bool Mystring::operator<=(const Mystring &R)
{return strcmp(this->str,R.str) <= 0;
}Mystring::~Mystring()
{delete []str;
}

main.cpp

#include <iostream>
#include "mystring.h"
#include<cstring>
using namespace std;
istream &operator>>(istream &L,const Mystring &R)
{L >> R.str;return L;
}
ostream &operator<<(ostream &L ,const Mystring &R)
{L << R.str << endl;return L;
}
int main()
{Mystring s1;Mystring s2("hello");Mystring s3(s2);cout << "s2 = " << s2.c_str() << endl;cout << "s3 = " << s3.c_str() << endl;cout << "s2.size = " << s2.str_size() << endl;for(int i = 0; i < s2.str_size(); i++){cout << s2.at(i) << endl;;}if(!s1.empty()){s1.at(0) = 'w';s1.at(1) = 'o';s1.at(2) = 'r';s1.at(3) = 'l';s1.at(4) = 'd';cout << "s1 = " << s1.c_str()<< endl;}cout << "s1 > s2 :" << (s1>s2) << endl;cout << "s1 < s2 :" << (s1<s2) << endl;cout << "s1 == s2 :" << (s1==s2) << endl;cout << "s2 == s3 :" << (s2 == s3) << endl;cout << "s2 != s3 :" << (s2 != s3) << endl;cout << "s2<= s3 :" << (s2 <= s3) << endl;cout << "s1>= s2 :" << (s1 >= s2) << endl;s3 = s2+s1;cout << "s3 :" << s3 << endl;cout << "s1 = :" << s1  << endl;s2 += s1;cout << "s2 :" << s2 << endl;cout << "s1 = :" << s1  << endl;s1 = s3;cout << "s1 = :" << s1  << endl;cout << "请输入s1的值:" << endl;cin >> s1;cout << "s1 = :" << s1 << endl;return 0;
}

Mystring类实现运算符重载相关推荐

  1. 4-1 复数类的运算符重载

    4-1 复数类的运算符重载 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 通过本题目的练习 ...

  2. [YTU]_2617(B C++时间类的运算符重载)

    题目描述 C++时间类的运算符重载 定义一个时间类Time,其数据成员为表示时间的小时(hour).分(minute),秒(second). 重载运算符"+",使之能用于时间对象的 ...

  3. [YTU]_2617( B C++时间类的运算符重载)

    C++时间类的运算符重载 定义一个时间类Time,其数据成员为表示时间的小时(hour).分(minute),秒(second). 重载运算符"+",使之能用于时间对象的加法运算: ...

  4. C++时间类的运算符重载

     Description C++时间类的运算符重载 定义一个时间类Time,其数据成员为表示时间的小时(hour).分(minute),秒(second). 重载运算符"+", ...

  5. Complex类与运算符重载

    Complex Class Operator Overloading: 写在开篇:分文件编写的实现运算符的重载,以经典的Complex类为模板,如题~ 正文开始@Assassin 目录: Comple ...

  6. C++模板类的运算符重载

    问题背景: 最近在写数据结构的上机作业时,遇到了模板类的运算符重载,修改了很多次都是在编译时报错.对比网上的许多文章,终于改对了,于是写一篇文章记录一下. 问题描述 数据结构上机作业,要求将数据结构中 ...

  7. C++实现日期类(运算符重载)

    日期类的实现 经历前期C语言的学习,C语言的编程思路是面向过程的编程,将所需要实现的功能封装为每一个功能函数,在主函数中进行调用 C++编程思想是面向对象的编程,相比较于C语言的编程,它更具有更高的安 ...

  8. 定义分数类中和运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

    /* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:main.cpp *作    者:李德彪 *完成时间:2016年6月15日 * ...

  9. [YTU]_2384 ( 矩形类中运算符重载【C++】)

    题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数.输入坐标的函数,实现矩形加法,以及计算并输出矩形面积的函数.要求使用提示中给出的测试函数并不得改动. 两个矩 ...

最新文章

  1. 告别 Navicat ,同事安利的这个IDEA的兄弟,真香!
  2. Python_面试题
  3. css超出隐藏显示省略号
  4. 让 WPF 的 RadioButton 支持再次点击取消选中的功能
  5. 怎么打开网络访问 计算机共享,电脑只要打开共享提示“无法启用共享访问”如何解决...
  6. 服务器系统架构的评估,系统架构师:性能评估
  7. 分光计游标盘ab两个游标作用_汽车防撞梁的作用究竟有多大?没有后防撞梁的汽车真的不安全吗?...
  8. vb6 串口同时读取写入数据怎么避免冲突_分布式场景下的数据复制究竟怎么做...
  9. Git——添加文件【git add / git commit】
  10. MVC 中通用导出页面数据到Excel
  11. 苹果mac思维导图软件:mindmanager
  12. Symmetric Tree
  13. NIO server client
  14. LINUX编译ARM64/AARCH64版本的jogamp(gluegen/jogl)注意事项
  15. 【15】 数学建模 | 典型相关分析 | 内附具体实现流程(清风课程,有版权问题,私聊删除)
  16. windows自带黑体_win10字体设置黑体|win10系统文字如何设置黑体字体
  17. 多媒体计算机软件系统课件,《多媒体计算机系统》PPT课件.ppt
  18. Hadoop数据开发笔试题(一)
  19. MFC设置应用程序图标
  20. 微信小程序 界面从右边滑出_微信小程序页面溢出左右滑动问题

热门文章

  1. itas109的开源项目汇总
  2. Microsoft Windows恶意软件删除工具
  3. Unity打包安卓如何存储本地游戏数据?
  4. 数据中心高架地板及通风静压箱计算 - 應用案例分享
  5. 每日新闻丨我国完成太阳帆在轨关键技术验证;美银看好微软云计算领域增长...
  6. win11 win和alt键失灵
  7. DDOS攻击应急响应指南
  8. unicode 中 CW2A CA2W两个宏的含义
  9. 淘宝天猫月销量接口(精准到具体数值)
  10. 又一行业遭遇滑铁卢 “AI算命”能取代算命大师么