.    #include<iostream>
. #include<iomanip>
. using namespace std;
.
. class String{
. friend ostream& operator<< (ostream&,String&);//重载<<运算符
. friend istream& operator>> (istream&,String&);//重载>>运算符
. public:
. String(const char* str=NULL); //赋值构造兼默认构造函数(char)
. String(const String &other); //赋值构造函数(String)
. String& operator=(const String& other); //operator=
. String operator+(const String &other)const; //operator+
. bool operator==(const String&); //operator==
. char& operator[](unsigned int); //operator[]
. size_t size(){return strlen(m_data);};
. ~String(void) {delete[] m_data;}
. private:
. char *m_data; // 用于保存字符串
. };
.
. inline String::String(const char* str)
. {
. if(!str)m_data=; //声明为inline函数,则该函数在程序中被执行时是语句直接替换,而不是被调用
. else {
. m_data=new char[strlen(str)+];
. strcpy(m_data,str);
. }
. }
.
. inline String::String(const String &other)
. {
. if(!other.m_data)m_data=;//在类的成员函数内可以访问同种对象的私有成员(同种类则是友元关系)
. else
. {
. m_data=new char[strlen(other.m_data)+];
. strcpy(m_data,other.m_data);
. }
. }
.
. inline String& String::operator=(const String& other)
. {
. if (this!=&other)
. {
. delete[] m_data;
. if(!other.m_data) m_data=;
. else
. {
. m_data = new char[strlen(other.m_data)+];
. strcpy(m_data,other.m_data);
. }
. }
. return *this;
. }
. inline String String::operator+(const String &other)const
. {
. String newString;
. if(!other.m_data)
. newString = *this;
. else if(!m_data)
. newString = other;
. else
. {
. newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+];
. strcpy(newString.m_data,m_data);
. strcat(newString.m_data,other.m_data);
. }
. return newString;
. }
.
. inline bool String::operator==(const String &s)
. {
. if ( strlen(s.m_data) != strlen(m_data) )
. return false;
. return strcmp(m_data,s.m_data)?false:true;
. }
.
. inline char& String::operator[](unsigned int e)
. {
. if (e>=&&e<=strlen(m_data))
. return m_data[e];
. }
.
. ostream& operator<<(ostream& os,String& str)
. {
. os << str.m_data;
. return os;
. }
.
. istream &operator>>( istream &input, String &s )
. {
. char temp[ ]; //用于存储输入流
. input>>setw()>>temp;
. s = temp; //使用赋值运算符
. return input; //使用return可以支持连续使用>>运算符
. }

最新文章

  1. 安装rabbitMQ delayed-messaged
  2. shell中括号的使用
  3. IOS 跳转至AppStore的两种方式
  4. WinRAR命令行
  5. iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条
  6. Android中Base64的简单使用
  7. C++时间标准库时间time和系统时间的使用
  8. hdu 2842 Chinese Rings
  9. ViewCompat.animate(view) 动画的操作
  10. JSP入门 文件上传
  11. android:layout_weight属性详解
  12. Linux CentOS7.0 (04)systemctl vs chkconfig、service
  13. vmware 12中安装MAC OS X Lion 10.7
  14. linux下64位汇编的系统调用(4)
  15. Oracle数据库体系结构之进程结构(4)
  16. vue动态切换视图
  17. Git工具使用
  18. PHP从入门到精通(一)
  19. JAVA单链表的实现-不带头结点且没有尾指针
  20. JavaScript:变量提升和函数提升

热门文章

  1. 记录php日志
  2. Linux Shell系列教程之(十五) Shell函数简介
  3. Convert string to binary and binary to string in C#
  4. ajax跨子域请求的两种现代方法
  5. ASP.NET HttpRuntime.Cache缓存类使用总结
  6. android sdk无法更新或者更新缓慢的解决方案
  7. C# 线程同步
  8. 实验12:Problem C: 重载字符的加减法
  9. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不执行的问题
  10. 启动app时全屏显示Default.png