String类是应用框架中不可或缺的类
重载运算符实现字符串的操作

#idndef IOTECK_STRING_H_
#define IOTECK_STRING_H_
namespace iotek
{
class String
{
public:
String(const char*=NULL);
~String();
String(const String&); //拷贝构造函数
//String a; a=b;
String& operator=(const String &); //赋值运算符
//String a; a="hello";
String& operator=(const char*);

String& operator+=(const String&);
String operator+(const String&)const;

String& operator+=(const char*);
String operator+(const char*)const;

inline const char* data() const
{
return m_data;
}
private:
char *m_data;
}
}
#endif

.CPP文件

#include"iotekstring.h"
#include<iostream>
#include<string.h>
using namespace std;
using namespace iotek;
String::String(const char *str)
{
if(NULL==str)
{
m_data=new char[1];
*m_data='\0';
}
else{
int length=strlen(str);
m_data=new char[length+1];
strcpy(m_data,str);
}
}

String::~String()
{
delete [] m_data;
}

String::String(const String &other)
{
int length=strlen(other.m_data);
m_data=new char[length+1];
strcpy(m_data,other.m_data);
}

String& String::operator=(const String &other)
{
if(this==&other)
return *this;
delete [] m_data;
int length=strlen(other.m_data);
m_data=new char[length+1];
strcpy(m_data,other.m_data);
return *this;
}

String& String::operator=(const char *other)
{
delete[] m_data;
if(other==NULL)
{
m_data=new char[1];
*m_data='\0';
}
else
{
int length=strlen(other);
m_data=new char[length+1];
strcpy(m_data,other);
}
return *this;
}

String& String::operator+=(const String& other)
{
char* tmp=m_data;
int length=strlen(m_data)+strlen(other.m_data);
m_data=new char[length+1];
strcpy(m_data,tmp);
strcat(m_data,other.m_data);
delete [] tmp;

return *this;
}

String String::operator+(const String& other)const
{
String result;
result+=*this;
result+=other;
return result;
}

String& String::operator+=(const char* other)
{
String tmp(other);
*this+=tmp;

return *this;
}

String String::operator+(const char* other)const
{
String result=*this;
result+=other;

result result;
}
main.cpp

#include"iotekstring.h"
#include<iostream>
#include<string.h>
using namespace std;
using namespace iotek;

int main(int argc,const char *argv[])
{
String s1("hello");
String s2=s1;
String s3="world";

s1+=s3;

s3+="!";

String s4=s1+s2;
s4=s1+"hello";

system("pause");
return 0;
}

String类实现的更多相关文章

  1. 标准库String类

    下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...

  2. 自己实现简单的string类

    1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...

  3. C++ string类的实现

    c++中string类的实现 今天面试被考到了, 全给忘记了!!!   //string类的实现 #include <iostream> #include <string.h> ...

  4. String类的功能

    String类              标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...

  5. java基础复习:final,static,以及String类

    2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...

  6. String类和StringBuffer类的区别

    首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...

  7. 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

    Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...

  8. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  9. String类常用方法

    1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...

  10. 运用String类实现一个模拟用户登录程序

    package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...

随机推荐

  1. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ‘dubbo:application’. – schema_reference.4: Failed to read schema document

    解决办法: 1.直接百度下载一个dubbo.xsd文件 2.myeclispe-->window-->preferences-->xml catalog-->add 完美解决!

  2. uber 真是垃圾

    uber司机好几次都不认识路,态度也不好,最开始使用是因为它价格最便宜,随着滴滴/快的价格下调,已经没有再使用uber的必要,果断卸载.

  3. <c:if>标签判断是否为空

    <c:if test="${not empty feeType}">  注意:大括号外面不能为空. ${orderNo.ethdOriginalOrderNo} < ...

  4. openDatabase() chrome vivaldi Stylish

    located at /Users/ruili/Library/Application Support/Vivaldi/Default/databases/ Databases.db contains ...

  5. nginx 设置 fastcgi缓存

    #增加调试信息 add_header X-Cache-CFC "$upstream_cache_status - $upstream_response_time"; fastcgi ...

  6. Python Locust性能测试框架实践

    [本文出自天外归云的博客园] Locust的介绍 Locust是一个python的性能测试工具,你可以通过写python脚本的方式来对web接口进行负载测试. Locust的安装 首先你要安装pyth ...

  7. struct内存对齐1:gcc与VC的差别

    struct内存对齐:gcc与VC的差别 内存对齐是编译器为了便于CPU快速访问而采用的一项技术,对于不同的编译器有不同的处理方法. Win32平台下的微软VC编译器在默认情况下采用如下的对齐规则:  ...

  8. 【leetcode❤python】 396. Rotate Function

    #-*- coding: UTF-8 -*- #超时#        lenA=len(A)#        maxSum=[]#        count=0#        while count ...

  9. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序更新相关数据

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第八篇:为ASP.NET MVC应用程序 ...

  10. Kanzi Studio中的概念

    Kanzi Studio是Kanzi的UI编辑器,功能非常强大.在使用Kanzi Stadio之前,首先要先熟悉编辑器中的概念. Kanzi Studio中主要分project窗格,property窗 ...