String类实现
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类实现的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- 通过setDB2Client*来方便的使用TRACE调优jdbc程序
一般来说通过TRACE的report来分析DDF的性能问题的话,基本对DBA都是噩梦一样.因为所有的Thread都是通过DDF一个类型的Thread,所以不管你怎么分类,看Accounting rep ...
- WLST 命令和变量
下列部分将详细描述 WLST 命令和变量.主题包括: WSLT 命令类别概述 浏览命令 控制命令 部署命令 诊断命令 编辑命令 信息命令 生命周期命令 节点管理器命令 树命令 W ...
- javaWeb 数据库连接池连接数据库
需要的公共jar包 mysql-connector-java-5.0.8-bin.jar(mysql数据库) ojdbc14.jar(oracle数据库) A.DBCP 需要jar包: commo ...
- MYSQL #1064错误
你的给出的代码里option为MYSQL关键字,不能直接写,需要用`包括起来(它为数字键1左边的键上的字符),为: `option` varchar(50) NOT NULL default ''
- Viking Village维京村落demo中的粒子距离消隐
Custom/DistanceFade shader 粒子雾似乎可以使用.尝试给面片套用该效果,但由于有顶点变形,效果不太好,要做些改动
- iOS 枚举的巧用
前言 在之前的一篇文章中简单的提到了这个问题, 但是自己写的不详细, 并且自己深入了解的也不是特别多, 在开发中也没怎么用到,所以经过阅读者的反馈对这个问题很是疑惑! 本篇文章会分析之前的不足之处, ...
- [问题2014A06] 复旦高等代数 I(14级)每周一题(第八教学周)
[问题2014A06] 若 \(n\) 阶实方阵 \(A\) 满足 \(AA'=I_n\), 则称为正交矩阵. 证明: 不存在 \(n\) 阶正交矩阵 \(A,B\) 满足 \(A^2=cAB+B^ ...
- cas的http配置和rmi远程调用
1.cas配置http请求(服务端) 1) 解压cas-server-3.4.4-release.zip将modules目录下的cas-server-webapp-3.4.4.war改名称为cas.w ...
- 【leetcode❤python】 67. Add Binary
class Solution(object): def addBinary(self, a, b): """ :type a: str ...
- VBA中练习ADO:ActiveX Data Object
前期绑定,要先添加引用---"Microsoft ActiveX Data Objects 6.1" ADO学习的权威参考可点击:w3school ADO简单理解:是几个Activ ...