C++常考编程题之string封装

本文详细介绍了如何在C++中实现自定义的String类,包括构造函数、析构函数、拷贝构造函数、赋值函数和连接函数的编写。通过深入解析每个函数的实现细节,帮助读者理解C++字符串操作的底层机制。

编写类String的构造函数、析构函数、拷贝构造函数、赋值函数和连接函数
已知类String的原型为:
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operator =(const String &other); // 赋值函数
String operator +(const String &other); //连接函数
private:
char *m_data; // 用于保存字符串
};
请编写String的上述5个函数。

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

class String
{
public:
	String(const char *str = NULL);     // 普通构造函数
	String(const String &other);        // 拷贝构造函数
	~String(void);                      // 析构函数
	String& operator= (const String &other);  // 赋值函数
	String  operator+ (const String &other);  //连接函数
	char* data(){ return m_data; }
private:
	char    *m_data;                    // 用于保存字符串
};

String  String::operator+ (const String &other) //连接函数,相当于strcat
{                     
	if (other.m_data == NULL)
	{
		return *this;
	}
	else
	{
		if (m_data == NULL)
		{
			return other;
		}
		else
		{
			String str;
			int len =strlen(m_data)+strlen(other.m_data)+ 1;
			str.m_data = new char[len];
			strcpy(str.m_data, this->m_data);
			strcat(str.m_data, other.m_data);
			return str;
		}
	}
}

String& String::operator= (const String& other) //赋值函数
{
	if (this->m_data == NULL) //先判断自己是否为空
	{
		if (other.m_data == NULL) //对方是否为空
		{
			//忽略
		}
		else
		{
			int len = strlen(other.m_data) + 1;
			m_data = new char[len];
			strcpy(m_data, other.m_data);
		}
	}
	else
	{
		delete[] m_data;
		if (other.m_data == NULL)
		{
			m_data = nullptr;
		}
		else
		{
			int len = strlen(other.m_data) + 1;
			m_data = new char[len];
			strcpy(m_data, other.m_data);
		}
	}
	return *this;
}

String::String(const char* str)
{
	if (str == NULL)
	{
		m_data = NULL;
		cout << "nullptr" << endl;
	}
	else
	{             
		int len = strlen(str) + 1; //+1,因为要加\0,补充:size_t strlen(const char *s);
		m_data = new char[len];
		strcpy(m_data, str); //把str指向的内容复制到m_data指向的内存里
	}    //char *strcpy(char *dest, const char *src);
	cout << "String构造函数" << endl;
}

String::String(const String& other) //将原有对象中的成员变量的内容逐个复制给新的对象中的成员变量
{
	if (other.m_data == NULL)
	{
		m_data = NULL;
	}
	else
	{
		int len = strlen(other.m_data) + 1;
		this->m_data = new char[len];
	/*	for(int i=0;i < len; i++)
		{
			this->m_data[i] = other.m_data[i];
		}
	*/
		strcpy(m_data, other.m_data); //与上面for循环等价
    }
	cout << "String 拷贝构造函数" << endl;
}

String::~String()
{
	if (m_data != NULL)
	{
		delete[] m_data;
	}
	cout << "String 析构函数" << endl;
}

int main(int argc, char* argv[])
{
	String str1 = "hello ";
	String str2 = "world!";
	String str3 = str1 + str2;//str3=str1+(str2);
	cout << str3.data() << endl;//输出:hello world!
	
	return 0;
}

【源码免费下载链接】:https://renmaiwang.cn/s/2gdnj 《R语言数据挖掘方法及应用》由薛薇编写而成的一本系统阐述R语言在数据挖掘领域前沿技术的著作。该书旨在指导读者学会使用R语言进行高效、实用的数据分析与建模工作,涵盖了从理论基础到实践操作的全过程。作为一款功能强大且开源的统计计算图形处理平台,R语言凭借其丰富的工具库社区支持,在数据分析与可视化方面展现出显著优势。在数据挖掘领域,R语言提供了包括`caret`、`randomForest`、`tm`、`e1071`等广泛使用的专用包,这些工具能够帮助用户更便捷地进行数据预处理、特征选择、模型构建结果评估。全书首先介绍R语言的基本知识体系,涵盖环境配置与安装方法、基础语法规范以及常见数据类型分析等内容。这些基础知识是开展后续数据分析工作的必备技能,通过学习可以快速掌握R语言的核心功能。随后章节深入讲解了数据挖掘的主要概念与流程,包括数据清洗、转换整理探索性分析等环节,同时详细阐述了分类、聚类、关联规则挖掘及预测等多种典型任务的具体实施方法。这些内容有助于读者全面理解数据挖掘的整体架构及其核心工作步骤。在应用实践部分,薛薇老师结合真实案例展示了R语言在实际业务场景中的具体运用,例如市场细分分析、客户流失预测以及个性化推荐系统等。通过这些案例研究,读者可以深入学习如何利用相关工具包解决实际问题,并提升数据分析能力。此外,书中配套的“案例数据集”“代码资源”为读者提供了实践操作的机会,使理论知识能够更好地转化为动手技能。通过实际操作分析,读者可以加深对R语言数据挖掘方法的理解并灵活运用。总之,《R语言数据挖掘方法及应用》是一部全面讲解R语言在数据分析与建模领域的教材,无论你是刚开始学习的新人还是经验丰富的专业人士,都能从中获益匪浅。通过深入研读此书,你可以掌握R语言的数据挖掘技巧,并将其应用到实
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值