资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

自己模拟写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
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类型实例讲解-创新互联
网站地址:http://cdkjz.cn/article/phihe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220