.    #include<iostream>
. #include<iomanip>
. using namespace std;
.
. class String{
. friend ostream& operator<< (ostream&,String&);//重载<<运算符
. friend istream& operator>> (istream&,String&);//重载>>运算符
. public:
. String(const char* str=NULL); //赋值构造兼默认构造函数(char)
. String(const String &other); //赋值构造函数(String)
. String& operator=(const String& other); //operator=
. String operator+(const String &other)const; //operator+
. bool operator==(const String&); //operator==
. char& operator[](unsigned int); //operator[]
. size_t size(){return strlen(m_data);};
. ~String(void) {delete[] m_data;}
. private:
. char *m_data; // 用于保存字符串
. };
.
. inline String::String(const char* str)
. {
. if(!str)m_data=; //声明为inline函数,则该函数在程序中被执行时是语句直接替换,而不是被调用
. else {
. m_data=new char[strlen(str)+];
. strcpy(m_data,str);
. }
. }
.
. inline String::String(const String &other)
. {
. if(!other.m_data)m_data=;//在类的成员函数内可以访问同种对象的私有成员(同种类则是友元关系)
. else
. {
. m_data=new char[strlen(other.m_data)+];
. strcpy(m_data,other.m_data);
. }
. }
.
. inline String& String::operator=(const String& other)
. {
. if (this!=&other)
. {
. delete[] m_data;
. if(!other.m_data) m_data=;
. else
. {
. m_data = new char[strlen(other.m_data)+];
. strcpy(m_data,other.m_data);
. }
. }
. return *this;
. }
. inline String String::operator+(const String &other)const
. {
. String newString;
. if(!other.m_data)
. newString = *this;
. else if(!m_data)
. newString = other;
. else
. {
. newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+];
. strcpy(newString.m_data,m_data);
. strcat(newString.m_data,other.m_data);
. }
. return newString;
. }
.
. inline bool String::operator==(const String &s)
. {
. if ( strlen(s.m_data) != strlen(m_data) )
. return false;
. return strcmp(m_data,s.m_data)?false:true;
. }
.
. inline char& String::operator[](unsigned int e)
. {
. if (e>=&&e<=strlen(m_data))
. return m_data[e];
. }
.
. ostream& operator<<(ostream& os,String& str)
. {
. os << str.m_data;
. return os;
. }
.
. istream &operator>>( istream &input, String &s )
. {
. char temp[ ]; //用于存储输入流
. input>>setw()>>temp;
. s = temp; //使用赋值运算符
. return input; //使用return可以支持连续使用>>运算符
. }

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. 记录php日志

    1.记录PHP错误日志 display_errors与log_errors的区别 display_errors 错误回显,一般常用于开发模式,但是很多应用在正式环境中也忘记了关闭此选项.错误回显可以暴 ...

  2. 【GOF23设计模式】原型模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_原型模式.prototype.浅复制.深复制.Cloneable接口  浅复制 package com.test.prot ...

  3. 2013学习总结----JavaScript

    javascript面向对象,实现的几种方式 1:直接使用JSON对象 var o1={ "a":1, "b":2, "c":functio ...

  4. FIM 2010: Kerberos Authentication Setup

    The goal of this article is to provide some background information regarding the Kerberos related co ...

  5. 高性能JS笔记1——加载执行

    一.脚本位置 1.Script标签尽可能放到Body底部,以减少脚本文件下载对整个页面UI渲染的影响. 2.Script标签永远不要紧跟Link标签后面. 二.组织脚本 1.合并多个文件在一个Scri ...

  6. Android 6.0权限管理

    Android 6.0权限管理 关于权限管理 Android6.0 发布之后,Android 的权限系统被重新设计.在 23 之前 App 的权限只会在用户安装的时候询问一次,App一旦安装后就可以使 ...

  7. 【读书笔记】iOS-GCD-网络编程要不要使用GCD

    一,网络编程强烈推荐使用异步API. 二,对于网络编程可以断言“线程是魔鬼”.如果在网络编程中使用线程,就很可能会产生大量使用线程的倾向,会引发很多问题.例如每个连接都使用线程,很快就会用尽线程栈内存 ...

  8. Swift 设计指南之 编程规范

    基本准则 用法一目了然是你设计时最重要的目的. 方法和属性这样的实体只声明一次,却会被重复调用.因此你在设计 API 时应尽可能使其简单明了.当评估某个设计时,只阅读声明往往是不够的,有时还需要检查它 ...

  9. IOS之KVC和KVO(未完待续)

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  10. SQL Server智能感知如何更新

    经常用sql server发现一个问题,比如说我刚刚添加个表或者字段,这时候在sqlserver里面写sql语句时,没有智能提示,这个问题我以前一直不是太注意.今天好好找了下解决方法,这里做下分享. ...