修身养性,止于至善___leiming32的博客

自己写的String类实现

头文件String.h
 1#ifndef STRING_H
 2#define STRING_H
 3#include <iostream>
 4#include <cstddef> // size_t
 5#include <cstring>
 6using namespace std;
 7
 8class String {
 9    friend ostream& operator<<(ostream&const String&); //1.友元   必须是两个参数
10    friend istream& operator>>(istream&, String&);
11public:
12    //String();  //2.放在cpp中实现
13    String(const char* str = "");//3默认实参
14    
15    // copy control
16    String(const String&); //复制构函
17    String& operator=(const String&);
18    String& operator=(const char*); 
19    ~String();
20
21    //算术运算
22    String operator+(const String&) const; // 一个参数!返回值不能是引用 +操作不能改变本身的值  
23    String& operator+=(const String&);
24    //String operator+(const char*); 
25    //String& operator+=(const char*&);
26
27    // 关系操作 < == (有了<就有了>,就有了>=和<=)
28    bool operator<(const String&const;
29    bool operator==(const String&const ;
30
31    //bool operator>(const String&);
32    bool operator>(const String& rhs) const return rhs < *this; } // rhs为const左操作数,若无const版本的<则出错
33    bool operator>=(const String& rhs) const return !(*this < rhs); }
34    bool operator<=(const String& rhs) const return !(rhs < *this); }
35    bool operator!=(const String& rhs) const    return !(*this == rhs); }
36    // 下标操作
37    char& operator[](size_t); // 注意参数类型
38    const char& operator[](size_t) const// 参考primer p443
39    // 得到子串,返回长度
40    size_t size() const// 尽量为const,因为const对象只能调用const版本,非const对象也能调用const版本
41
42
43private:
44    char *m_data;
45    void setString(const char*);
46}
;
47
48inline String    // 注意,若要内联,则要在头文件中实现此内联
49//String
50String::operator +(const String& rhs) const{
51    String ret(*this); // 执行之后ret的m_data的长度与lhs的相等,不能再cat
52    ////strcat(ret.m_data, rhs.m_data);
53    ret += rhs; // 使用+=来实现+更有效
54    return ret;
55}

56
57#endif

源文件String.cpp
  1#include <iostream>
  2#include "String.h"
  3
  4ostream& operator<<(ostream& outconst String& str) {
  5    out << str.m_data;
  6    return out;
  7}

  8istream& operator>>(istream& in, String& str) {
  9    in >> str.m_data;
 10    if (!in{
 11        str = String();
 12    }

 13    return in;
 14}

 15
 16//String::String(const char* str = "") {
 17String::String(const char* str) {
 18    setString(str);
 19}

 20
 21String::String(const String &other) {
 22    setString(other.m_data);
 23}

 24
 25String&
 26String::operator =(const char *str) {
 27    if (strcmp(m_data, str) == 0// 
 28        return *this;
 29    }

 30    delete[] m_data; // 先释放
 31    setString(str);
 32    return *this;
 33}

 34
 35String&
 36String::operator =(const String&rhs) {
 37    if (this == &rhs) // *this == rhs
 38        return *this;
 39    }
 else {
 40        delete[] m_data; // 先释放
 41        setString(rhs.m_data);
 42    }

 43    return *this;
 44}

 45
 46String::~String() {
 47    delete m_data;
 48}

 49
 50
 51 String&
 52String::operator +=(const String& rhs) {
 53    //strcat(m_data, rhs.m_data); // 不能随便strcat
 54    int n = size() + rhs.size(); // rhs找不到const版本的size()函数,但非const对象可以调用const版本的函数
 55    char *tmp = new char[n+1];
 56    strcpy(tmp, m_data);
 57    strcat(tmp, rhs.m_data);
 58    delete m_data;
 59    m_data = tmp;
 60
 61    return *this;
 62}

 63
 64 bool
 65String::operator<(const String& rhs) const {
 66    int ret = strcmp(m_data, rhs.m_data);
 67    if (ret < 0return true;
 68    else return false;
 69}

 70
 71//inline bool
 72//String::operator>(const String& rhs) {
 73//    int ret = strcmp(m_data, rhs.m_data);
 74//    if (ret > 0) return true;
 75//    else return false;
 76//}
 77
 78 bool
 79String::operator ==(const String& rhs) const {
 80    int ret = strcmp(m_data, rhs.m_data);
 81    if (ret == 0return true;
 82    else return false;
 83}

 84
 85 char&
 86String::operator [](size_t ix) {
 87    size_t n = strlen(m_data);
 88    if (ix < 0 || ix > n) // ix can be n, [0n]
 89        cerr << "Error: "<< ix << "out of range" << endl;
 90        exit(1); 
 91    }

 92    return m_data[ix];
 93}

 94
 95 const char&
 96String::operator [](size_t ix) const {
 97    size_t n = strlen(m_data);
 98    if (ix < 0 || ix > n) // ix can be n, [0n]
 99        cerr << "Error: " << ix << " out of range" << endl;
100        exit(1); 
101    }

102    return m_data[ix];
103}

104
105inline size_t
106String::size() const {
107    return strlen(m_data);
108}

109
110void 
111String::setString(const char* str) {
112    size_t n = strlen(str);
113    m_data = new char[n+1];
114
115    if (n > 0{
116        strcpy(m_data, str);
117    }
 else {
118        m_data[0= '\0';
119    }

120}

121

posted on 2010-10-20 20:57 狼在北方 阅读(831) 评论(0)  编辑 收藏 引用 所属分类: C++编程语言