body, table{font-family: 微软雅黑; font-size: 10pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

#include <iostream>
#include<stdlib.h>
#include<stdio.h>
#include<sstream>
#include<string.h>
using namespace std;
class String
{
        private:
                char *pstr_;
        public:
                String();
                String(const char *);
                String(const String&);
                ~String();
                String & operator = (const String &);
                String & operator = (const char *);
                String & operator += (const String &);
                String & operator += (const char *);
                char & operator [] (std::size_t index);
                const char & operator [](std::size_t index)const;
                std::size_t size() const;
                const char* c_str() const;
                friend bool operator == (const String &,const String &);
                friend bool operator != (const String &,const String &);
                friend bool operator < (const String &,const String &);
                friend bool operator > (const String &,const String &);
                friend bool operator <= (const String &,const String &);
                friend bool operator >= (const String &,const String &);
                friend std::ostream & operator << (std::ostream & os,const String &s);
                friend std::istream & operator >> (std::istream & is,String & s);
};
String operator + (const String &,const String &);
String operator + (const String &,const char *);
String operator + (const char *,const String &);

String::String()
{
        cout<<"String()"<<endl;
        pstr_=new char[1];
        strcpy(pstr_,"");
}
String::~String()
{
        cout<<"~String()"<<endl;
        delete []pstr_;
}
String::String(const char *str)
{
        cout<<"String(const char *)"<<endl;
        pstr_=new char[strlen(str)+1];
        strcpy(pstr_,str);
}
String :: String(const String & s)
{
        cout<<"String(const String &)"<<endl;
        pstr_=new char[strlen(s.pstr_)+1];
        strcpy(pstr_,s.pstr_);
}
String & String::operator = (const String & s)
{
        cout<<"String & operator =(const String &)"<<endl;
        this->~String();
        pstr_=new char[strlen(s.pstr_)+1];
        strcpy(pstr_,s.pstr_);
        return *this;
}
String & String::operator =(const char *s)
{
        cout<<"String & operator=(const char *)"<<endl;
        this->~String();
        pstr_=new char[strlen(s)+1];
        strcpy(pstr_,s);
        return *this;
}
String & String::operator +=(const String &s)
{
        cout<<"String & operator += (const String &)"<<endl;
        int len1=strlen(this->pstr_);
        int len2=strlen(s.pstr_);
        char *a=(char*)calloc(len1,sizeof(char));
        strcpy(a,this->pstr_);
        this->~String();
        this->pstr_=new char[len1+len2+1];
        sprintf(this->pstr_,"%s%s",a,s.pstr_);
        return *this;
}
String & String::operator += (const char *s)
{
        cout<<"String & operator += (const char *)"<<endl;
        int len1=strlen(this->pstr_);
        int len2=strlen(s);
        char *a=(char*)calloc(len1,sizeof(char));
        strcpy(a,this->pstr_);
        this->~String();
        this->pstr_=new char[len1+len2+1];
        sprintf(this->pstr_,"%s%s",a,s);
        return *this;
}
std::ostream &  operator<<(std::ostream & os,const String &s)
{
        cout<<"std::ostream & operator << (std::ostream & ,const String &)"<<endl;
        os<<s.pstr_;
        return os;
}
char& String::operator[] (std::size_t index)
{
        cout<<"char & operator[] (std:: size_t index)"<<endl;
        int len=strlen(this->pstr_);
        char *a=(char*)calloc(len,sizeof(char));
        strcpy(a,pstr_);
        return a[index];
}

const char& String::operator[](std::size_t index)const
{
        cout<<"const char & operator[] (std:: size_t index)const"<<endl;
        int len=strlen(this->pstr_+1);      //这里错了,this->pstr_+1相当于指正偏移到下一个字符
        char *a=(char*)calloc(len,sizeof(char));
        strcpy(a,pstr_);
        return a[index];
}
std::size_t String::size()const
{
        cout<<"std::size_t size()const"<<endl;
        int len=strlen(this->pstr_);
    return len;
}
const char* String::c_str() const
{
        cout<<"const char* c_str() const"<<endl;
        int len=strlen(this->pstr_+1);
        char *a=(char*)calloc(len,sizeof(char));  
//这里不管申请多大的空间,后面拷贝函数如果不够他都会自动在申请空间所以结果没出现错误
        strcpy(a,pstr_);
        return a;
}
bool operator == (const String & a1,const String &a2)
{
        cout<<"bool operator == (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))==0)
                        return 1;
        return 0;
}
bool operator != (const String & a1,const String & a2)
{
        cout<<"bool operator != (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))!=0)
                        return 1;
        return 0;
}
bool operator < (const String &a1,const String &a2)
{
        cout<<"bool operator < (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))<0)
                        return 1;
        return 0;
}
bool operator > (const String &a1,const String &a2)
{
        cout<<"bool operator > (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))>0)
                        return 1;
        return 0;
}
bool operator <= (const String &a1,const String &a2)
{
        cout<<"bool operator <= (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))<0)
                        return 1;
        else if((strcmp(a1.c_str(),a2.c_str()))==0)
                        return 1;
        return 0;
}
bool operator >= (const String &a1,const String &a2)
{
        cout<<"bool operator >= (const String &,const String &)"<<endl;
        if((strcmp(a1.c_str(),a2.c_str()))>0)
                        return 1;
        else if((strcmp(a1.c_str(),a2.c_str()))==0)
                        return 1;
        return 0;
}
std::istream & operator >> (std::istream & is,String &s)
{
        cout<<"std::istream & operator >> (std::istream &m,String &)"<<endl;
        is>>s.pstr_;
        return is;
}
String operator + (const String &a1,const String &a2)
{
        cout<<"String operator + (const String &,const String &)"<<endl;
        String tmp=a1;
        tmp+=a2;
        return tmp;
}
String operator + (const String &a1,const char * a2)
{
        cout<<"String operator + (const String &,const char *)"<<endl;
        String tmp=a1;
        tmp+=a2;
        return tmp;
}
String operator + (const char *a1,const String & a2)
{
        cout<<"String operator + (const char *,const String &)"<<endl;
        String tmp;
        tmp+=a1;
        tmp+=a2;
        return tmp;
}


//测试代码
int main()
{
        String s;
        String s1("hello");
        String s2(s1);
        String s3="world";
        s1=s2;
        String s4;
        s4="wangdao";
        s+=s3;
        s4+="nihao";
        cout<<s4<<endl;
        cout<<s4[2]<<endl;
        cout<<s4.size()<<endl;
        cout<<s4.c_str()<<endl;
        bool bool1=(s1>s4);
        cout<<bool1<<endl;
        bool bool2=(s1>=s3);
        cout<<bool2<<endl;
        cin>>s;
        cout<<s<<endl;
        String s5=s1+s2;
        cout<<s5<<endl;
        String s6=s1+"shenzhen";
        cout<<s6<<endl;
        String s7="shenzhen1"+s1;
        cout<<s7<<endl;
        return 0;
}

String类运算符重载,自己实现的更多相关文章

  1. c++类运算符重载遇到的函数形参问题

    class A { public: A(int arg1, int arg2); ~A(); A &operator = ( A &other); A operator + ( A & ...

  2. 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载

    在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符. []运算符重载 +运算符重载 +=运算符重载 <<运算符重载 >>运算符重 ...

  3. 完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载

    在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符. []运算符重载 +运算符重载 +=运算符重载 <<运算符重载 >>运算符重 ...

  4. 04737_C++程序设计_第9章_运算符重载及流类库

    例9.1 完整实现str类的例子. #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> ...

  5. 【C++札记】实现C++的string类

    C++有了string类使得操作字符串变得很方便.有关string类,面试过程中也经常问到的就是自己实现一个sring类.下边实现个String类供大家参考: String.h #pragma onc ...

  6. 深入C++05:运算符重载

    运算符重载 1.复数类 运算符重载目的:使对象运算表现得和编译器内置类型一样: 复数类例子 #include<iostream> using namespace std; class CC ...

  7. C++:String类

    String类 1.使用String类必须在程序的开始包括头文件string,即要有如下语句:#include<string> 2.string类字符串对象的使用方法与其他对象一样stri ...

  8. C++中的运算符重载练习题

    1.RMB类 要求: 定义一个RMB类 Money,包含元.角.分三个数据成员,友元函数重载运算符‘+’(加)   和 ‘-’(减),实现货币的加减运算     例如:    请输入元.角 分:    ...

  9. 包装类、基本数据类型及String类之间的相互转换

    包装类:8种基本数据类型对应一个类,此类即为包装类 一.基本数据类型 包装类 及String之间的转换 1.基本数据类型转化为包装类:调用包装类的构造器      int i=10;     Inte ...

随机推荐

  1. 【android】夜间模式简单实现

    完整代码,请参考我的博客园客户端,git地址:http://git.oschina.net/yso/CNBlogs 关于阅读类的app,有个夜间模式真是太重要了. 那么有两种方式可以实现夜间模式 1: ...

  2. ABP官方文档翻译 1.5 多租户

    多租户 什么是多租户? 数据库和部署架构 多部署-多数据库 单部署-多数据库 单部署-单数据库 单部署-混合数据库 多部署-单/多/混合数据库 ABP的多租户 启用多租户 租主和租户 会话 决定当前租 ...

  3. Kafka学习之(一)了解一下Kafka及关键概念和处理机制

    Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模小打的网站中所有动作流数据.优势 高吞吐量:非常普通的硬件Kafka也可以支持每秒100W的消息,即使在非常廉价的商用机器上也能做 ...

  4. Python数据可视化:网易云音乐歌单

    通过Python对网易云音乐华语歌单数据的获取,对华语歌单数据进行可视化分析. 可视化库不采用pyecharts,来点新东西. 使用matplotlib可视化库,利用这个底层库来进行可视化展示. 推荐 ...

  5. spring定时器(注解的形式)

    最近有个需求,要在凌晨的时候,根据某几张表生成一张定时任务表里的数据,数据的状态为0(未整改),然后在当天晚上,再把这些数据的状态没改变的,改变状态为1(待整改),然后要用到定时器,百度了一下用注解形 ...

  6. LabVIEW之Vision基础 (一)之软件

    一.软件准备 NI LabVIEW软件视觉开发必备软件 1.开发平台:LabVIEW 2015Chinese 32位中文版 链接:http://pan.baidu.com/s/1eRGmFVc 2.N ...

  7. Linux命令awk

    1.简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大. 简单来说awk就是把文件逐行读入,默认以空格为分隔符将每行切片,切开的部 ...

  8. # fabirc 配置多组服务器 密码与密钥一起使用 key_filename的设置

    环境说明 myv myv2 是配置在/etc/hosts 的两台 虚拟机 虚拟机ip. 参考英文文档 官方文档的例子不是给的很详细.. http://docs.fabfile.org/en/1.13/ ...

  9. java高级特性(2)--循序渐进地培养面向对象的思维方式

    在我踏入软件行业后,一直苦于没有前辈指点.我常年困惑于两个问题:一是怎样培养面向对象设计的思维能力?二是怎样进行架构设计,有无方法? 因为我做了那么多年项目,却很少看到有漂亮的面向对象思维写出来的代码 ...

  10. monkey测试小记

    本篇中不记录环境搭建,只是介绍一些经验和小秘诀吧. 一.使用安卓模拟器进行测试. 在刚刚接触到monkey测试的时候,用的真机进行测试,点击几万次甚至更多的时候,发现系统变慢了.也许是错觉,但是系统经 ...