String.h

//字符串封装类名CString

class CString
{
    friend CString operator + (const char *pStr, const CString &r);                        //"string" + obj 声明为友元,提供一方的private数据
    friend istream& operator >> (istream &in, CString &r);                                     //cin >>运算符重载
    friend ostream& operator << (ostream &out, CString &r);                                //cout  <<运算符重载
public:
    CString();                        //无参构造
    CString(const char *pStr);               //类型转换构造,带一个参数的构造
    CString(const CString &r);
    ~CString();                      //析构函数

char *StrCpy(const char *pStrSrc);
    char *StrCpy(/*CString *const this,*/const CString &r);
    bool StrCmp(/* const CString *const this,*/const CString &r) const;
    bool StrCmp(const char *pStr) const;

//    operator int();
//    operator char *();

CString& operator = (const CString &r);
    CString& operator = (const char *pStr);
    bool operator == (const CString &r) const;
    bool operator == (const char *pStr) const;
    CString operator +(const CString &r) const; //obj + obj
    CString operator +(const char *pStr) const; // obj + "String"
    CString operator += (const CString &r);  // obj += obj
    CString operator += (const char *pStr);  // obj += "String"

int GetLength() const;
    const char *GetStr() const;
    
    char operator[](int nIndex);
    bool Empty();

private:  //受保护的成员函数,不需要校验,在外部已经校验
    int StrLen(const char *pStr) const;
    char *Copy(const char *pStrSrc);
    char *Add(const char *pStr, int nBufSize) const;
    void Release();
    bool Cmp(const char *pStr) const;
private:
    char *m_pStr;
    unsigned int m_nSize;
};

//string == obj
bool operator == (const char *pStr, const CString &r);

String.cpp

#include "stdafx.h"
#include "String.h"

//
// Construction/Destruction
//

CString::CString()  //无参构造
{
    m_pStr = NULL;
    m_nSize = 0;
}

CString::CString(const char *pStr)                //类型转换构造, 一个参数的构造
{
    m_pStr = NULL;
    m_nSize = 0;
    
    if(NULL != pStr)
    {
        StrCpy(pStr);
    }
}

CString::CString(const CString &r)               //深复制,分配两个内存
{
    m_pStr = NULL;
    m_nSize = 0;
    
    if(this == &r)
    {
        return ;
    }
    
    if(0 != r.m_nSize)
    {
        StrCpy(r.m_pStr);
    }
}

CString::~CString()  //析构函数,释放
{
    delete []m_pStr;
}

/*CString(const CString &r)   //浅复制,共享一个内存。浅复制和深复制不能同时存在一个类中,只能选择其一
{
m_pStr = NULL;
m_nSize = 0;
    }*/

//字符串复制string
char* CString::StrCpy(const char *pStrSrc)
{
    if(NULL != pStrSrc)
    {
        Release();
        
        m_nSize = StrLen(pStrSrc);
        m_pStr = new char[m_nSize + 1];
        
        Copy(pStrSrc);
    }
    return m_pStr;
}

//字符串复制obj
char* CString::StrCpy(const CString &r)
{
    Release();
    
    if(0 != r.m_nSize)
    {
        m_nSize = r.m_nSize;
        m_pStr = new char[m_nSize + 1];
        
        Copy(r.m_pStr);
    }
    
    return m_pStr;
}

/*//成员类型转换函数
CString::operator int()
{
    return m_nSize;
}

//成员类型转换函数
CString::operator char *()
{
    return m_pStr;
}*/

//赋值运算符重载
CString& CString::operator = (const CString &r)  //参数为CString对象
{
    if(this != &r) //若不是将自己给自己
    {
        if(0 != r.m_nSize)
        {
            StrCpy(r); //使用字符串函数重载函数
            //StrCpy(r.m_pStr);
        }
    }
    
    return *this;
}

//赋值运算符重载
CString& CString::operator = (const char *pStr)                             //参数为字符串,如果不写这个,系统自动调用默认字符串过程例子:                                            
{                                                                                                               //obj = "test" ->(1)临时对象 = CString("test"); (2)obj = 临时对象
    if(NULL != pStr)                                                                                //如果写了这个函数,就省去了上面的临时对象过程,称作以代码空间换取效率
    {
        StrCpy(pStr);
    }
    
    return *this;
}

// obj == obj
bool CString::operator == (const CString &r) const
{
    return StrCmp(r);
}

//obj == string
bool CString::operator == (const char *pStr) const
{
    return ((NULL != pStr)?StrCmp(pStr):false);
}

//string == obj
bool operator == (const char *pStr, const CString &r)
{
    if(NULL != pStr)
    {
        return r==pStr;
    }

return false;
}

//获得字符串大小
int CString::GetLength() const
{
    return m_nSize;
}

bool CString::Cmp(const char *pStr) const
{
    for(int i = 0; i < m_nSize; i++)
    {
        if(m_pStr[i] != pStr[i])
        {
            return false;
        }
    }
    return true;
}

//获得字符串
const char* CString::GetStr() const
{
    return m_pStr;
}

//下标运算符
char CString::operator[](int nIndex)
{
    return m_pStr[nIndex];
}

bool CString::Empty()
{
    if(NULL != m_pStr)
    {
        return false;
    }
    return true;
}

//受保护的成员函数,不需要校验,在外部已经校验
int CString::StrLen(const char *pStr)  const //获得字符串的长度
{
    int nSize = 0;
    
    for(; '\0' !=  *pStr++; nSize++);
    
    return nSize;
}

char* CString::Copy(const char *pStrSrc)   //字符串复制
{
    char *pStr = m_pStr;
    for(; '\0' != (*pStr++ = *pStrSrc++); );

return m_pStr;
    
}

void CString::Release()     //初始化函数
{
    if(NULL != m_pStr)
    {
        delete []m_pStr;
        m_pStr = NULL;
        m_nSize = 0;
    }
}

// string
bool CString::StrCmp(const char *pStr) const
{
    if(NULL == pStr && m_nSize != StrLen(pStr))
    {
        return false;
    }

return Cmp(pStr);
}

//obj
bool CString::StrCmp(const CString &r) const
{
    if(this != &r)
    {
        if(m_nSize != r.m_nSize || NULL == r.m_pStr || NULL == m_pStr)
        {
            return false;
        }

return Cmp(r.m_pStr);
    }
    return true;
}

char *CString::Add(const char *pStr, int nBufSize) const
{
    char *pBuf = new char[nBufSize];
    char *pTemp = pBuf;

for(int i = 0; i < m_nSize; i++)
    {
        pTemp[i] = m_pStr[i];
    }

if(NULL != pStr)
    {
        int j = 0;
        for(i = m_nSize; j < nBufSize - m_nSize; j++, i++)
        {
            pTemp[i] = pStr[j];
        }
    }

return pBuf;
}

CString CString::operator +(const CString &r) const //obj + obj
{
    CString obj;
    if(NULL != m_pStr && NULL != r.m_pStr)
    {
        char *pBuf = Add(r.m_pStr, m_nSize+r.m_nSize+1);
        obj = pBuf;
        delete []pBuf;
    }

return obj;
}

CString CString::operator +(const char *pStr) const                         // obj + "String"
{
    CString obj;
    if(NULL != m_pStr && NULL != pStr)
    {
        int nSize = (NULL != pStr)?StrLen(pStr):0;
        char *pBuf = Add(pStr, m_nSize+nSize+1);
        obj = pBuf;
        delete []pBuf;
    }
    return obj;
}

CString operator + (const char *pStr, const CString &r)                   //"String" + obj
{
    if(NULL != pStr && NULL != r.m_pStr)
    {
        CString temp = pStr;
        return temp+r;
    }

return NULL;
}

CString CString::operator += (const CString &r)                     // obj += obj
{
    *this = *this + r;

return *this;
}

CString CString::operator += (const char *pStr)                       // obj += "String"
{
    *this = *this + pStr;

return *this;
}

istream& operator >> (istream &in, CString &r)                               //cin
{
    char szBuf[4096] = "";
    in >> szBuf;
    r = szBuf;

return in;
}
 
ostream& operator << (ostream &out, CString &r)                     //cout
{
    if(!r.Empty())
    {
        out<<r.m_pStr;  //Cur:friend  || r.GetStr()
    }

return out;
}

#include "stdafx.h"
#include "String.h"

int main(int argc, char* argv[])
{
    CString obj = "This is a ";
    CString obj1 = obj + "test";
    
    cout<<obj<<endl;
    cout<<obj + "Test"<<endl;
    cout<<obj1<<endl;
    //CString obj3 = obj + obj1;
//    cout<<obj1.GetStr()<<endl;
//    obj =  "Test" + obj;
//    cout<<obj.GetStr()<<endl;
    
    return 0;
}

字符串C++的封装CString相关推荐

  1. Python类与对象技巧(1):字符串格式化、封装属性名、可管理的属性、调用父类方法

    1. 自定义字符串的格式化 _formats = {'ymd' : '{d.year}-{d.month}-{d.day}','mdy' : '{d.month}/{d.day}/{d.year}', ...

  2. c语言mac地址字符串转换成数组,CString类型的MAC地址转换为数组类型

    在最近完成计算机网络课程设计的过程中,您需要将mac地址转换为六个字节. 我在互联网上找到了信息. 基本思想是将mac地址分为六个部分,每个部分为十六个. 基数将转换为十进制数,并分配给六字节数组的每 ...

  3. C/C++中的字符串比较函数strcmp/memcmp/CString.Compare/CString:CompareNoCase

    memcmp和strncmp的区别 一.memcmp含义 Compare characters in two buffers. int memcmp( const void* buf1, const ...

  4. c语言中 compare函数,C/C++中的字符串比较函数strcmp/memcmp/CString.Compare/CStrin

    memcmp和strncmp的区别 一.memcmp含义 Compare characters in two buffers. int memcmp(    const void* buf1,    ...

  5. 前端与移动开发----JS高级----面向对象编程,类与实例对象,继承,严格模式,模板字符串,class封装tab栏

    JS高级01 回顾上阶段 Javascript组成 ECMAScript: 基础语法 (变量, 表达式, 循环, 判断, 函数, 对象等) DOM: document 操作标签(获取, 增加, 插入, ...

  6. c++实现字符串类的封装

    MyString.h文件 #define _CRT_SECURE_NO_WARNINGS#pragma once#include<iostream>#include<string&g ...

  7. Gson解析json字符串,并封装成ListT

    最近一个项目中,需要将从服务器上获取到的JSON字符串转换为对象,大概研究了一下,发现在项目中很多地方都要用到,而且有的时候需要返回的是一个集合,所以写了个方法,留着自用. public class ...

  8. MFC中CString类字符串与长整型、浮点型、字符数组char数据之间的相互转换

    一.长整型数据与CString类字符串相互转换 1.将长整型数据转换为CString字符串类 CString str; long ld; str.Format(_T("%ld"), ...

  9. 字符串获取类、封装检测数字的方法

    关于字符串,都知道它的属性有长度,而每一个字符串也是通过一个个数字编码形成的,想要通过字符串的属性来判断字符串里的数字的话,需要知道一下几种字符串的属性: var str = '你好'; str.le ...

最新文章

  1. 人均月薪 7.5 万,腾讯 Q2 成绩单来了,网友酸了?
  2. Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)
  3. 基于SRCNN的表情包超分辨率(附tensorflow实现)
  4. jQuery开发技巧
  5. 汇编语言--CMOS RAM芯片
  6. zookeeper windows 下安装
  7. STM32 DSP库的使用方法
  8. 使用SQL Server 2017 Docker容器在.NET Core中进行本地Web API开发
  9. python 下载文件-python实现从ftp服务器下载文件
  10. HCIE Security 流量型攻击防范 备考笔记(幕布)
  11. CentOS配置Nginx官方的Yum源 及yum安装php
  12. voip 客户端 android,Android基于OpenSL ES,Speex,RTMP的Voip客户端实现
  13. webservice 实例 创建与 调用
  14. unity物理引擎介绍
  15. 新人主播开播以后,碰到的各类问题和解决方法
  16. 北航超算运行matlab,计算性能超50万亿次破纪录,北航荣获ASC19世界大学生超算竞赛最高计算性能奖...
  17. windows xp系统本地连接提示受限制或无连接怎么办
  18. 0基础快速开发口袋网盘小程序
  19. pdf压缩文件怎么压缩最小,pdf大小超过上传大小不能上传怎么压缩?
  20. 数据结构课设_网页形式的景区导游

热门文章

  1. Flex Gumbo中如何通过fontWeight样式,给TextBox设置粗体文字
  2. java打字_java - 在线打字测试(dazi.kukuw.com)
  3. 计算机资源属于校内还是校外,KingCMS:运营地方门户站之推广总结篇(6)
  4. 如何用python将pdf转换为txt、docx、excel
  5. iphone 应用程序图标、启动画面、itune图标等设置
  6. aix 修改服务器时间,AIX修改系统时间
  7. 微信小程序超详细入门简介和使用
  8. Smart Pointe
  9. 求sum=d+dd+ddd+……+dd...d
  10. AIO - Cyberlink DVD