您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus的string类的简单实现

2014-05-14 11:05 656 查看
重载构造函数,特别是重载复制构造函数。

为什么要重载复制构造函数? 在给string变量声明的时候,如 string str = str2; 编译器会自动生成复制构造函数,从而不会调用其他的构造函数,导致调用析构函数的次数增多。

重载运算符 =

友元重载运算符 > < = >> <<

//
//  stringBad.h
//  Class_String
//
//  Created by Ben_22 on 14-5-14.
//  Copyright (c) 2014年 Ben_22. All rights reserved.
//

#ifndef __Class_String__stringBad__
#define __Class_String__stringBad__

#include <iostream>
using std::ostream;
using std::istream;

#endif /* defined(__Class_String__stringBad__) */

class String{
private:
    char *str;
    size_t len;
    static int num_strings;

public:
    static const int CINLIM = 80;
    String(const char *s);
    String();
    String(const String &);
    ~String();
    
    size_t length()const {return len;}
    
    String & operator=(const String &);
    String & operator=(const char *);

    
    char & operator[](int i);
    const char & operator[](int i )const;
    
    friend bool operator<(const String &st, const String &st2);
    friend bool operator>(const String &st, const String &st2);
    friend bool operator==(const String &st, const String &st2);

    friend ostream &operator<<(ostream &os, const String &st);
    friend istream &operator>>(istream &is, const String &st);
    
    static int HowMany();
};


//
//  stringBad.cpp
//  Class_String
//
//  Created by Ben_22 on 14-5-14.
//  Copyright (c) 2014年 Ben_22. All rights reserved.
//
#include <cstring>
#include "stringBad.h"

using std::cout;
using std::cin;

int String::num_strings = 0;

int String::HowMany(){
    return num_strings;
}

String::String(const char*s){
    len = std::strlen(s);
    str = new char[len+1];
    std::strcpy(str, s);
    num_strings++;
}

String::String(){
    len = 0;
    str = new char[1];
    str[0] = '\0';
    num_strings++;
}

String::String(const String &st){
    num_strings ++;
    len = st.len;
    str = new char [len+1];
    std::strcpy(str, st.str);
}

String::~String(){
    --num_strings;
    delete []str;
}

String &String::operator=(const String &st){
    if (this == &st) {
        return *this;
    }
    delete []str;
    len = st.len;
    str = new char[len+1];
    std::strcpy(str, st.str);
    return *this;
}

String &String::operator=(const char* s){
    delete []str;
    len = std::strlen(s);
    str = new char[len+1];
    std::strcpy(str, s);
    return *this;
}

char &String::operator[](int i){
    return str[i];
}

const char & String::operator[](int i )const {
    return str[i];
}

bool operator<(const String &st1, const String &st2){
    return (std::strcmp(st1.str, st2.str) < 0 );
}

bool operator>(const String &st1, const String &st2){
    return (std::strcmp(st1.str, st2.str) > 0 );
}

bool operator==(const String &st1, const String &st2){
    return (std::strcmp(st1.str, st2.str) == 0);
}

ostream &operator<<(ostream &os, const String &st){
    os<< st.str;
    return os;
}
istream & operator>>(istream &is, String &st){
    char temp[String::CINLIM];
    is.get(temp, String::CINLIM);
    if (is){
        st = temp;
    }
    while (is && is.get()!='\n') {
        continue;
    }
    
    return is;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: