C ++ | char * Design based on a string class MyString

Title from C ++ Programming Language (fourth edition) Author Zheng Li Exercises 6-24

Here is the code based on char * implementation:

/*
 * @Author: Hellcat
 * @Date: 2020-03-24 11:44:47
 * This file is MyString.h
 */
// class with pointer members必须有copy ctor(拷贝构造)和copy op =(拷贝复制)
#ifndef __MyString__
#define __MyString__

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

class MyString {
public:
	MyString(const char* cstr = nullptr);
	MyString(const MyString& str);
	MyString& operator = (const MyString& str);
	MyString& operator += (const MyString& str);
	MyString operator + (const MyString& str);
	int length();
	~MyString();
	char* c_str() const { return m_data; }
private:
	char* m_data;
	friend ostream& operator << (ostream&os, const MyString& str);
	friend istream& operator >> (istream&is, MyString& str);
};

  Class implementation file:

inline MyString :: MyString (const char * cstr / * = nullptr * /) {// default argument only to the primary 
	IF (CSTR = nullptr a!) { 
		M_DATA = new new char [strlen (CSTR) +. 1]; // + 1 allocation '\ 0' 
		strcpy (M_DATA, CSTR); 
	} 
	the else {// No initial value specified 
		M_DATA = new new char [1]; 
		* M_DATA = '\ 0'; 
	} 
} 

inline MyString :: MyString (const & MyString STR) {// deep copy 
	M_DATA = new new char [strlen (str.m_data) +. 1]; 
	strcpy (M_DATA, str.m_data); 
} 

inline MyString :: MyString & operator = (const & MyString STR) { 
	IF (the this == & STR) // detected assignment to self 
		return the this *; 

	Delete [] M_DATA; 
	M_DATA = new new char [strlen (str.m_data) +. 1]; 
	strcpy (M_DATA, str.m_data); 
	return * the this; 
}

// 返回局部对象 不能return reference
inline MyString MyString::operator + (const MyString& str) {
	MyString res;
	res.m_data = new char[strlen(this->m_data) + strlen(str.m_data) + 1];
	strcpy(res.m_data, this->m_data);
	strcat(res.m_data, str.m_data);
	return res;
}

inline MyString& MyString::operator += (const MyString& str) {
	if(str.m_data == nullptr || *str.m_data == '\0')
		return *this;
	if(this == &str) {    // 检测自我 +=
		MyString temp(*this);
		return *this += temp;
	}
	int len = strlen(m_data) + strlen(str.m_data) + 1;
	char* ptr = m_data;
	m_data = new char[len];
	strcpy(m_data, ptr);
	strcat(m_data, str.m_data);
	return *this;
	// 或者也可以用前面写的 + 和 = 重载
	// *this = *this + str;
	// return *this;
}

inline bool operator <= (const MyString& str1, const MyString& str2) {
    return strcmp(str1.c_str(), str2.c_str()) <= 0;
}

// array new with array delete
inline MyString::~MyString() {
	delete[] m_data;
}

ostream& operator << (ostream&os, const MyString& str) {
    return os<<str.c_str();
}

istream& operator >> (istream&is, MyString& str) {
    char temp[1010];
    if(is>>temp) {
        delete [] str.m_data;
        str.m_data = new char[strlen(temp) + 1];
		strcpy(str.m_data, temp);
    }
	return is;
}

inline int MyString::length() {
    return strlen(this->m_data);
}

  Test code:

// test135.cpp
#include "MyString.h"

inline void test(const char* title, bool value) {
    cout<<title<<" returns "<<(value ? "true" : "false")<<endl;
}

int main() {
    MyString s1 = "DEF";
    cout<<"s1 is "<<s1<<endl;

    MyString s2;
    cout<<"Please enter s2: ";
    cin>>s2;
    cout<<"length of s2: "<<s2.length()<<endl;
    // 测试比较运算符
    test("s1<=\"ABC\"", s1 <= "ABC");
    test("\"DEF\"<=s1", "DEF" <= s1);
    // 测试连接运算符
    s2 += s1;
    cout<<"s2 = s2 + s1: "<<s2<<endl;
    cout<<"length of s2: "<<s2.length()<<endl;
    return 0;
}

  operation result:

 

Guess you like

Origin www.cnblogs.com/tedukuri/p/12566690.html