自己模拟写C++中的String类型实例讲解-创新互联

下面是模拟实现字符串的相关功能,它包括一下功能:

创新互联主营资兴网站建设的网络公司,主营网站建设方案,重庆APP开发公司,资兴h5重庆小程序开发搭建,资兴网站营销推广欢迎资兴等地区企业咨询

String(const char * s);//利用字符串来初始化对象
String();      //默认构造函数
String(const String & s);//复制构造函数,利用String类型来初始化对象
~String();      //析构函数
int length();      //返回String类型中字符串的长度
String & operator=(const String & s);//重载=运算符。
String & operator=(const char *);
char & operator[](int i);  //重载【】运算符
const char & operator[](int i) const;
friend bool operator<(const String & st,const String & st2);//重载<运算符,用来比较String类型中字符串的大小。
friend bool operator>(const String & st,const String & st2);
friend bool operator==(const String & st,const String & st2);//重载==运算符,判断两个String对象是否相等
friend ostream & operator<<(ostream & os,const String & st2);//重载输出函数
friend istream & operator>>(istream & is,String & st2);//重载输入函数
static int HowMang()//返回总共生成的String类对象的数目。

String.h:

#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED
#include"iostream"
#include<string.h>
using std::ostream;
using std::istream;
class String{
private:
  char * str;
  int len;
public:
  static int num_strings;
  static const int CINLM=80;
  String(const char * s);
  String();
  String(const String & s);
  ~String();
  int length();
  String & operator=(const String & s);
  String & operator=(const char *);
  char & operator[](int i);
  const char & operator[](int i) const;
  friend bool operator<(const String & st,const String & st2);
  friend bool operator>(const String & st,const String & st2);
  friend bool operator==(const String & st,const String & st2);
  friend ostream & operator<<(ostream & os,const String & st2);
  friend istream & operator>>(istream & is,String & st2);
  static int HowMang()
  {
    return num_strings;

  }
};


#endif // STRING_H_INCLUDED

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

当前文章:自己模拟写C++中的String类型实例讲解-创新互联
本文网址:https://www.cdcxhl.com/article44/phihe.html

成都网站建设公司_创新互联,为您提供ChatGPT关键词优化移动网站建设云服务器动态网站建站公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设