MyString类的实现--C++ primer plus 读书笔记

来源:互联网 发布:梦幻西游淘宝卖祥瑞 编辑:程序博客网 时间:2024/06/01 08:41

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">本文实现一个简单的string类,参考自c++ primer plus这本书,源码如下:</span>

//======================================================// FileName: MyString.h// Author: sjhuangx// Date: 2016-03-03// Desc: a single string class//=======================================================#ifndef MYSTRING_H_INCLUDED#define MYSTRING_H_INCLUDED#define _CRT_SECURE_NO_WARNINGS#include <iostream>using std::ostream;using std::istream;class MyString{private:char*str;// pointer to stringintlen;// the length of stringstatic intnum_strings;// the number of objectsstatic const int CINLIM = 80;// the limit of cin numberpublic:MyString(const char* st);// constructor from C stringMyString();// default construtorMyString(const MyString& st);// copy constructor~MyString();// destructorint Length() const { return len; }// return the length of string// overload operator methodsMyString& operator=(const MyString& st);MyString& operator=(const char* s);char& operator[](int i);const char& operator[](int i) const;// overload friend operatorfriend bool operator<(const MyString& str1, const MyString& str2);friend bool operator>(const MyString& str1, const MyString& str2);friend bool operator==(const MyString& str1, const MyString& str2);friend ostream& operator<<(ostream& os, const MyString& st);friend istream& operator>>(istream& is, MyString& st);// static functionint HowMany();};#endif // MYSTRING_H_INCLUDED

以下是类的实现部分:

//======================================================// FileName: MyString.h// Author: sjhuangx// Date: 2016-03-03// Desc: a single string class//=======================================================#include "MyString.h"// static memberint MyString::num_strings = 0;// static functionint MyString::HowMany(){return num_strings;}// class methodsMyString::MyString(const char * s) // construct string from C string{if (s == NULL){len = 0;// set sizestr = new char[1];// allot storagestr[0] = '\0';// initialize pointer}else{len = std::strlen(s);// set sizestr = new char[len + 1];// allot storagestd::strcpy(str, s);// initialize pointer}num_strings++;// set object count}// default constructorMyString::MyString(){len = 0;str = new char[1];str[0] = '\0';// default stringnum_strings++;}// necessary destructorMyString::~MyString(){--num_strings;// update static memberdelete[] str;// delete the spacelen = 0;// reset len}// copy constructorMyString::MyString(const MyString& st){len = st.len;// same lengthstr = new char[len + 1];// allot spacestd::strcpy(str, st.str);// copy date to new location++num_strings;// handle static member update}//============ overload operator methods =============// assign String to StringMyString& MyString::operator=(const MyString& st){if (this == &st){return *this;}delete[] str;len = st.len;str = new char[len + 1];std::strcpy(str, st.str);return *this;}// assign a C string to MyStringMyString& MyString::operator=(const char* st){if (st == NULL){return *this;}delete[] str;len = std::strlen(st);str = new char[len + 1];std::strcpy(str, st);return *this;}// read-write char access for non-const Stringschar& MyString::operator[](int i){if (i > len)// if i large than length of string, then return the last of string{i = len;}if (i < 0)// if i less then 0, than return the first of string{i = 0;}return str[i];}// read-only char access for const Stringconst char& MyString::operator[](int i) const{if (i > len)// if i large than length of string, then return the last of string{i = len;}if (i < 0)// if i less then 0, than return the first of string{i = 0;}return str[i];}//============== overload friend operators ===============// overload less operatorbool operator<(const MyString& str1, const MyString& str2){return (std::strcmp(str1.str, str2.str) < 0);}bool operator>(const MyString& str1, const MyString& str2){return (std::strcmp(str1.str, str2.str) > 0);}bool operator==(const MyString& str1, const MyString& str2){return (std::strcmp(str1.str, str2.str) == 0);}// simple string outpoutostream& operator<<(ostream& os, const MyString& st){os << st.str;return os;}// simple string inputistream& operator>>(istream& is, MyString& st){char temp[MyString::CINLIM];is.get(temp, MyString::CINLIM);if (is){st = temp;}while (is && is.get() != '\n'){continue;}return is;}

简单测试如下:

#include "MyString.h"using namespace std;int main(int argc, char** argv){MyString mystr;mystr = "hello world";cout << mystr << endl;system("pause");return 0;}





0 0
原创粉丝点击