ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ | 基于char*设计一个字符串类MyString

2020-03-25 15:52:47  阅读:393  来源: 互联网

标签:const return C++ char str MyString data


题目来自C++语言程序设计(第四版) 作者郑莉 习题6-24

下面是代码基于char*的实现:

/*
 * @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);
};

  类的实现文件:

inline MyString::MyString(const char* cstr /*= nullptr*/) { //  default argument只能给一次
	if(cstr != nullptr) {
		m_data = new char[strlen(cstr) + 1];     // + 1 分配'\0'
		strcpy(m_data, cstr);
	}
	else { // 未指定初值
		m_data = new char[1];
		*m_data = '\0';
	}
}

inline MyString::MyString(const MyString& str) { // 深拷贝
	m_data = new char[strlen(str.m_data) + 1];
	strcpy(m_data, str.m_data);
}

inline MyString& MyString::operator = (const MyString& str) {
	if(this == &str) // 检测自我赋值
		return *this;

	delete [] m_data;
	m_data = new char[strlen(str.m_data) + 1];
	strcpy(m_data, str.m_data);
	return *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);
}

  测试代码:

// 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;
}

  运行结果:

 

标签:const,return,C++,char,str,MyString,data
来源: https://www.cnblogs.com/tedukuri/p/12566690.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有