zoukankan      html  css  js  c++  java
  • 【面试题001-补充】C++ MyString类的封装

    【面试题001-补充】C++ MyString类的封装 

    一,C++ MyString类的封装

    String.h:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
    #ifndef _STRING_H_
    #define _STRING_H_
    #include <iostream>
    using namespace std;

    class String
    {
    public:
        String(const char *str = "");
        String(const String &other);
        String &operator=(const String &other);
        String &operator=(const char *str);

        bool operator!() const;
        char &operator[](unsigned int index);
        const char &operator[](unsigned int index) const;

        friend String operator+(const String &s1, const String &s2);
        String &operator+=(const String &other);

        friend ostream &operator<<(ostream &os, const String &str);
        friend istream &operator>>(istream &is, String &str);
        ~String(void);

        void Display() const;

    private:
        String &Assign(const char *str);
        char *AllocAndCpy(const char *str);
        char *str_;
    };

    #endif // _STRING_H_  



    String.cpp:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
     
    #pragma warning(disable:4996)
    #include "String.h"
    #include <string.h>
    //#include <iostream>
    //using namespace std;

    String::String(const char *str)
    {
        str_ = AllocAndCpy(str);
    }

    String::String(const String &other)
    {
        str_ = AllocAndCpy(other.str_);
    }

    String &String::operator=(const String &other)
    {
        if (this == &other)
            return *this;

        return Assign(other.str_);
    }

    String &String::operator=(const char *str)
    {
        return Assign(str);
    }

    String &String::Assign(const char *str)
    {
        delete[] str_;
        str_ = AllocAndCpy(str);
        return *this;
    }

    bool String::operator!() const
    {
        return strlen(str_) != 0;
    }

    char &String::operator[](unsigned int index)
    {
        //return str_[index];
        //non const 版本调用 const版本

        return const_cast<char &>(static_cast<const String &>(*this)[index]);
    }

    const char &String::operator[](unsigned int index) const
    {
        return str_[index];
    }

    String::~String()
    {
        delete[] str_;
    }

    char *String::AllocAndCpy(const char *str)
    {
        int len = strlen(str) + 1;
        char *newstr = new char[len];
        memset(newstr, 0, len);
        strcpy(newstr, str);

        return newstr;
    }

    void String::Display() const
    {
        cout << str_ << endl;
    }

    String operator+(const String &s1, const String &s2)
    {
        //int len = strlen(s1.str_) + strlen(s2.str_) + 1;
        //char* newstr = new char[len];
        //memset(newstr, 0, len);
        //strcpy(newstr, s1.str_);
        //strcat(newstr, s2.str_);
        //
        //String tmp(newstr);
        //delete newstr;
        String str = s1;
        str += s2;
        return str;
    }

    String &String::operator+=(const String &other)
    {
        int len = strlen(str_) + strlen(other.str_) + 1;
        char *newstr = new char[len];
        memset(newstr, 0, len);
        strcpy(newstr, str_);
        strcat(newstr, other.str_);

        delete[] str_;

        str_ = newstr;
        return *this;
    }

    ostream &operator<<(ostream &os, const String &str)
    {
        os << str.str_;
        return os;
    }

    istream &operator>>(istream &is, String &str)
    {
        char tmp[1024];
        cin >> tmp;
        str = tmp;
        return is;
    }

    main.cpp:

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
     
    #include "String.h"
    #include <iostream>
    using namespace std;

    int main(void)
    {
        String s1("abcdefg");

        char ch = s1[2];
        cout << ch << endl;

        s1[2] = 'A';
        s1.Display();

        const String s2("xyzabc");
        ch = s2[2];
        //s2[2] = 'M';
        s2.Display();


        String s3 = "xxx";
        String s4 = "yyy";

        String s5 = s3 + s4;
        s5.Display();

        String s6 = "aaa" + s3 + "sdfadfa" + "xxxx";
        s6.Display();

        s3 += s4;
        s3.Display();

        cout << s3 << endl;

        String s7;
        cin >> s7;
        cout << s7 << endl;
        return 0;
    }

     

    Makefile:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    .PHONY:clean  
    CPP=g++  
    CFLAGS=-Wall -g  
    BIN=test  
    OBJS=main.o String.o  
    LIBS=  
    $(BIN):$(OBJS)  
        $(CPP) $(CFLAGS) $^ -o $@ $(LIBS)  
    %.o:%.cpp  
        $(CPP) $(CFLAGS) -c $< -o $@  
    clean:  
        rm -f *.o $(BIN)  

    运行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    c
    abAdefg
    xyzabc
    xxxyyy
    aaaxxxsdfadfaxxxx
    xxxyyy
    xxxyyy
    adf
    adf
  • 相关阅读:

    修改 linux 时区时间和 php 时区
    夺命雷公狗—玩转SEO---21---域名购买技巧
    夺命雷公狗—玩转SEO---20---K站
    夺命雷公狗—玩转SEO---19---降权
    夺命雷公狗—玩转SEO---18---索引
    夺命雷公狗—玩转SEO---17---收录
    夺命雷公狗—玩转SEO---16---关键词词性和定位关键词
    夺命雷公狗—玩转SEO---15---域名
    夺命雷公狗—玩转SEO---14---空间和服务器的区别
  • 原文地址:https://www.cnblogs.com/codemylife/p/3653382.html
Copyright © 2011-2022 走看看