mystring.h

来自「一本语言类编程书籍」· C头文件 代码 · 共 29 行

H
29
字号
// Exercise 14.1 MyString.h
// Definition of the MyString class representing strings

#ifndef MYSTRING_H
#define MYSTRING_H
namespace mySpace {

class MyString {
  public:
    MyString();                                // Default constructor
    MyString(const char* pString);             // Construct from a C-style string
    MyString(char ch, int n);                  // Construct from repeated character
    MyString(int number);                      // Construct string representation of integer
    MyString(const MyString& rString);         // Copy constructor
    ~MyString();                               // Destructor

    int length() const{ return strLength; }    // Return length excluding terminating null
    int find(char ch) const;                   // Find the occurrence of a character
    int find(const char* pString) const;       // Find the occurrence of a C-style string 
    int find(const MyString& rString) const;   // Find the occurrence of a MyString  as a sub-string
    void show() const;                         // Output the string
    MyString& operator=(const MyString& rhs);  // Assignment operator
  private:
    char* pStr;                                // Pointer to the string
    size_t strLength;                          // number of characters excluding terminating null
};

}  // End of namespace mySpace
#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?