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.判断输入的用户名和密 ...
随机推荐
- maven+springMVC+mybatis+mysql详细过程
1.工程目录 a.在src/main/java文件夹中,新建包cn.springmvc.model(存放javabean), cn.springmvc.dao(存放spring与mybatis连接接口 ...
- ECStore在Win环境下如何运行CMD命令
大多数程序员使用windows开发环境来做ECStore二次开发,经常需要使用 ECStore自带的cmd命令进行一些系统操作,如清除缓存(cacheclean),升级程序(update),创建新的a ...
- Python之检查URL
# -*- coding: utf-8 -*- import os,sys import time import sys import pycurl #URL="http://www.bai ...
- python(八)内置模块logging/os/time/sys/json/pickle
模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少 ...
- 两个有序数组中查找第K大数
题目:两个数组A.B,长度分别为m.n,即A(m).B(n),分别是递增数组.求第K大的数字. 方法一: 简单的办法,使用Merge Sort,首先将两个数组合并,然后在枚举查找.这个算法的时间复 ...
- "旋转的风车"----windows(GDI)绘图
这正是秋季将尽, 冬季未到的时节. 黄土高坡上已滚起了漫天黄沙, 而这里却是万里晴空如练! 风, 丝丝入骨! 未央柳即将枯死的枝条,仍在挣扎. 街道两旁清一色的银杏树叶, 金灿耀眼. 耀的令人感动, ...
- FireFox中iframe的返回上页问题
-----已测试,可以使用----- 原文地址链接:http://fansofjava.iteye.com/blog/1019852 在写静态页面过程中遇到了FF的history.back()和his ...
- 原生Ajax讲解
典型的http通信:浏览器向服务器发出请求,服务器向客户端返回响应,浏览器重新加载页面,这种不连续的页面加载方式导致用户的体验变得杂乱,缺乏连贯性. 如: 在一般的web应用程序中,用户填写表单字段然 ...
- python中的@
一.函数修饰符 '@' 用做函数的修饰符,可以在模块或者类的定义层内对函数进行修饰, 出现在函数定义的前一行,不允许和函数定义在同一行 一个修饰符就是一个函数,它将被修饰的函数作为参数,并返回修饰后的 ...
- c.BIO连接器与NIO连接器的对比
前面两节,我们分别看了BIO和NIO的两种模式Tomcat的实现方式. BIO的方式,就是传统的一线程,一请求的模式,也就是说,当同时又1000个请求过来,如果Tomcat设置了最大Accept线程数 ...