简单版的String类,旨在说明>> <<重载

#include <iostream>
//#include <cstring>//包含char*的字符串处理函数
using namespace std;class String
{
public:String(){p=NULL;}String(char *str);void diaplay();friend bool operator>(String &str1,String &str2);//重载>操作符friend ostream & operator <<(ostream&,String &str);friend istream & operator >>(istream&,String &str);
private:char *p;
};
String::String(char *str)
{p=str;
}
void String::diaplay()
{cout<<p;
}
bool operator>(String &str1,String &str2)
{if (strcmp(str1.p,str2.p)>0){return true;}else return false;
}
ostream& operator <<(ostream& output,String &str)
{output<<str.p;return output;
}
istream& operator >>(istream& input,String &str)
{//input>>str.p;//没有分配空间,无法读入。str.p=new char[256];input>>str.p;return input;//
//     char q[256];
//     input>>q;
//     //p.length =strlen(q);
//     str.p=new char[strlen(q)+1];
//     strcpy(str.p,q);
//     return input;

}
int main()
{String str1("Hello,pig!"),str2;cin>>str2;str1.diaplay();bool b=str1>str2;cout<<'\n'<<b<<endl;cout<<str2<<endl;
}

重载>> <<函数只能作为类的类的友元函数,其形式如下:

istream& operator >>(istream& ,自定义类 &);

ostream& operator <<(ostream& ,自定义类 &);

重载运算法作为类成员函数还是类友元函数区别:

1 作为类成员函数必须满足运算表达式第一个参数是一个类对象,而且返回值与该对象同类型。

故一般将单目操作符重载为成员函数,双目操作符重载为友元函数。

String 类较完整实现:

#include <iostream>
//#include <cstring>//包含char*的字符串处理函数
using namespace std;class String
{
public:String(){p=NULL;len=0;}String(int);String(char *str);String (String&);~String(){delete[] p;}void Clear();//清空本串int mystrlen();bool IsEmpty();//判断本串是否为空String Substr(int index,int count);//从index开始提取count个字符的字串int Find(char c,int start);//从下标start开始找到c后,返回字符c 在本串的下标char & operator [](int i);//重载[]操作符operator char *();//将String类对象转换为普通字符串
friend bool operator>(String &str1,String &str2);//重载>操作符friend ostream & operator <<(ostream&,String &str);friend istream & operator >>(istream&,String &str);
private:char *p;//字符串指针int len;//字符串长度,不包含最后的\0
};String::String(int length)
{len=length;p=new char[length+1];
}
String::String(char *str)
{if (str==NULL){len=0;p=NULL;}else{len=strlen(str);p=new char[len+1];strcpy(p,str);//深拷贝
    }//p=str;//只写这个是浅拷贝,只拷贝了指针
}
String::String(String &other)
{len=other.len;p=new char[len+1];strcpy(p,other.p);
}
bool String::IsEmpty()
{return (!this->len);
}void String::Clear()
{if (!IsEmpty()){delete[]p;len=0;}
}
int String::mystrlen()
{return len;
}
int String::Find(char c,int start)
{int i;if (start>len) cout<<"超出范围"<<endl;//return NULL;else{for (i =start;i<len;++i){if (p[i]==c) break;}return i;}
}
String String::Substr(int index,int count)
{if (index+count>len) cout<<"超出范围"<<endl;else{String str(count);str.len=count;for (int i=0;i<count;i++,index++)str.p[i]=p[index];str.p[count]='\0';return str;}}
char & String::operator[](int i)
{if (i<0||i>len-1){cout<<"越界"<<endl;}else{return p[i];}
}
//类型转换
String::operator char *()
{return (char *)p;
}bool operator>(String &str1,String &str2)
{if (strcmp(str1.p,str2.p)>0){return true;}else return false;
}
ostream& operator <<(ostream& output,String &str)
{output<<str.p;return output;
}
istream& operator >>(istream& input,String &str)
{//input>>str.p;//没有分配空间,无法读入。str.p=new char[256];input>>str.p;return input;////或者:
//     char q[256];
//     input>>q;
//     str.p=new char[strlen(q)+1];
//     strcpy(str.p,q);
//     return input;

}
int main()
{String str3("hello");  int pos;  cout<<"\n测试Find功能"<<endl;  pos = str3.Find('e',0);  cout<<str3<<endl;  cout<<pos<<endl;  cout<<"\n测试Substr功能"<<endl;  cout<<str3.Substr(1,2)<<endl;  cout<<"\n测试重载<< >>功能"<<endl;  String c;  cout<<"请输入一段字符串"<<endl;  cin>>c;  cout<<c<<endl;  cout<<"测试字符串C函数的应用"<<endl;  String f(40);  char *e = " this is a test";  char g[50]="hahahhah";strcpy(f,e); //隐含执行了默认类型转换(char *)f; cout<<f<<endl;  strcat(g,f);  cout<<g<<endl; cout<<"\n测试IsEmpty _strlen功能"<<endl;  String d("tihs is a test");  if(d.IsEmpty())  cout<<"d 是空字符串"<<endl;  else  cout<<"d 非空字符串 \t长度:"<<d.mystrlen()<<endl;  return 0;  }

注意:C++标准库中string类构造函数是浅拷贝,

string a="hello";
 string b(a);
 cout<<(void *)a[2]<<endl;
 cout<<(void *)b[2]<<endl; 地址形同

注意:operator char *();//将String类对象转换为普通字符串

是类型转换函数的定义,即该类型可以自动转换为const char*类型。

像是隐式类型转换

不同于重载*,重载*应写为 char operator * ();

因为运算符重载中有几个运算符的返回值是有格式的(约定),如operator * 在重载时通常返回值是classType&或者const classType& 。
operator const char*() const是类型转换函数的定义,即该类型可以自动转换为const char*类型。至于最后一个const,那个大家都知道是对类成员的限制(不允许更改对象的状态)
比如我们现在自定一个一个整型(MyInt),它允许在需要使用C++语言中的int类型时将MyInt类型转换为int类型:
class MyInt {
     public:
          operator int () const;
     private:
          int elem;
};
MyInt::operator int () const
{
    return elem;
}
就可以在需要使用int类型时使用MyInt。
需要记住,C++中没有返回类型的函数有3个,构造函数、析构函数、类型转换函数。

前两个是不写返回类型函数实现中也不允许出现return语句
最后一个则是不写返回类型,但是必须返回对应类型的值,即必须出现return语句。

类型转换中返回类型在operator后面在括号前面,且没有参数。

函数运算符中是类型在operator 前面

转载于:https://www.cnblogs.com/Yogurshine/p/3677799.html

String 类实现 以及 流插入/流提取运算符重载相关推荐

  1. [C++]分数类的定义(成员函数、运算符重载)

    [C++]分数类的定义(成员函数.运算符重载) 1 分数类成员和成员函数 1.1 分数类成员 1.2 分数类的成员函数 1.3 分数类的io操作 1.4 分数类的预定义 2 成员函数定义 2.1 化简 ...

  2. C++类与对象笔记十二:运算符重载二:左移运算符重载

    左移运算符重载:可以打印输出自定义数据类型. 为了输出重载,我们先看看现有的输出函数.输出类型为std下的ostream类型的引用. 标准输出流(全局只能有一个). 返回值类型为ostream,函数名 ...

  3. 冰冰学习笔记:string类的简单模拟

    欢迎各位大佬光临本文章!!! 还请各位大佬提出宝贵的意见,如发现文章错误请联系冰冰,冰冰一定会虚心接受,及时改正. 本系列文章为冰冰学习编程的学习笔记,如果对您也有帮助,还请各位大佬.帅哥.美女点点支 ...

  4. C++ string类(包括深浅拷贝)

    目录 一.字符码表 一.为什么用string类 二.使用标准库中的string类 1.string类 2.string中的常用接口说明 (1)string类对象的常见构造 (2)string类对象访问 ...

  5. 【C++】string类@STL

    string类 0. string类 1. 构造&析构&赋值重载 2. Capacity 容量操作 2.1 size vs length 2.2 capacity 2.3 resize ...

  6. 流和流库[给初学者非常有用]

    流和流库 概 述        在C语言中,输入/输出系统的特点是缺乏类型检查机制.如printf函数,在格式控制字符串后的参数,即使类型和个数与其不匹配,编译是不会出错,但运行时会得到错误的结果.C ...

  7. 第 16 章 string类和标准模板库

    第 16 章 string类和标准模板库 16.1 string类 C语言在 string.h(C++中为cstring)提供了一系列的字符串函数. 16.1.1 构造字符串 string 实际上是模 ...

  8. STL之string类:知其然,需知其所以然

    目录 前言 一,构造及初始化 1.1constuct类函数 1.2构造函数及其模拟实现 1.3拷贝构造及其模拟实现 1.4赋值操作符 1.5string类的swap接口 二,迭代器 2.1初识迭代器即 ...

  9. 《C++中STL引入和string类常用接口+自我实现-》

    前言 在这篇博客里将详细说说C++中的STL,通过这篇我们可以学习到什么是STL,以及STL的六大组件,STL具有的缺陷,最后看看string类及面试会让模拟实现string类的操作. 文章目录 前言 ...

  10. C++运算符重载(类内、外重载)

    1.概念   运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数).   用函数的 ...

最新文章

  1. autofac 用法总结
  2. java常见证书类型和密钥库类型
  3. SAP UI5 Label related stuff and accessibility研究
  4. Scala与Java混编译:java日志不打印的问题
  5. Android SQLite用法
  6. 信息收集--空间搜索引擎/网盘
  7. Retrofit完美封装
  8. 在centos下安装使用busybox工具箱
  9. 【BJOI2019】勘破神机(下降幂转自然幂)(第一类斯特林数)(特征方程)
  10. AVFoundation–简介
  11. Python 打造基于有道翻译的命令行翻译工具(命令行爱好者必备)
  12. 诚之和:双11首战薇娅为什么输给了李佳琦?
  13. shiro认证时出现报错Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken -
  14. C语言实现简易金山打字通
  15. AndroidOTA升级流程
  16. BackgroundWorker 实现多线程操作
  17. 如何用算法把一个十进制数转为十六进制数-C语言基础
  18. leetcode572.另一棵树的子树
  19. vscode自动换行快捷键(附常用快捷键)
  20. 【五校联考2day2】WYF的盒子

热门文章

  1. 小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_18、SpringBoot测试进阶高级篇之MockMvc讲解...
  2. 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第3节 两种获取Stream流的方式_1_两种获取Stream流的方式...
  3. 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第6节 Lambda表达式_7_Lambda表达式有参数有返回值的练习...
  4. 重拾《 两周自制脚本语言 》- 支持中文标识符
  5. poj2914无向图的最小割
  6. seajs的使用--主要了解模块化
  7. [深入Maven源代码]maven绑定命令行参数到具体插件
  8. postman本地访问https
  9. CentOS 7 配置DHCP服务器
  10. AWS免费云服务套餐申请步骤及常见问题