C++的运算符重载实现两个字符串的连接(+号运算符)其实就是连接两个对象

我们先来了解一下string 和char 两者的区别

char  定义字符

string  定义字符串

一、赋值的区别:

 char: 

char ch1[] = "give me";
  char ch2[] = "a cup";
  strcpy(ch1,ch2);
  cout<<"ch1="<<ch1<<endl;


char ch1[MAX_SIZE];
get(char1);

string:


 string str1 = "give me";
  string str2 = "a cup";
  ①str1 = str2;
     cout<<"str1="<<str1<<endl;
  ②str1.assign(str2,0,5);       // 参数2为起始位置,参数3为字符数
     cout<<"str1="<<str1<<endl;
 
string str("dog loves pig");
 
string str;
getline(cin,str);

二、长度的获取:

char:

 char ch1[] = "give me";
  int m = strlen(ch1);  //不包括‘\0’
cout<<" m="<<m<<endl;
//m=7

string:

  string str1 = "give me";
  ①int m = strlen(str1.c_str());
     cout<<"m="<<m<<endl;
  ②int n = str1.size();
     cout<<"n="<<n<<endl;
  ③int k = str1.length();
cout<<"k="<<k<<endl;
//k=7

我们可以这样定义一个字符串:

char *str;

但是我们需要注意的是,如果这样定义字符串就一定要对他进行动态内存分配,因为他只是代表一个指针,我们知道指针只有四个字节的空间,不能用来储存一段字符串,只有给他进行动态内存分配之后才能用来保存字符串

复习一下使用拷贝构造函数使用的条件:

1、用一个对象初始化另一个对象

2、函数参数是类的对象的时候(对象的值传递)传引用不需要

3、当函数返回值是别人的类的时候

下面我们来看一看代码怎么实现:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>

using namespace std;
class CString
{
private:
	char* m_str;
public:
	//有参构造函数 定义成const是因为等一下要给他赋初值
	CString(const char* str)
	{
		//动态内存分配,分配内存空间
		m_str = new char[strlen(str) + 1];
		//加1是因为字符串是以\0结束的,我们要用一个结束符来标识它
		//赋值
		strcpy(m_str, str);
	}
	//使用析构函数释放动态内存分配的空间
	~CString()
	{
		if (m_str)
		{
			delete[] m_str;
		}
	}
	//调用拷贝构造函数
	CString(const CString& other)
	{
		if (this != &other)//this是本类对象,other是传入的对象
		{
			//动态内存申请
			m_str = new char[strlen(other.m_str) + 1];
			strcpy(m_str, other.m_str);
		}
	}
	CString operator+(const CString& other_string)
	{
		//先分配足够的空间
		int len1 = strlen(m_str);
		int len2 = strlen(other_string.m_str);
		char* temp = new char[len1 + len2 + 1];
		strcpy(temp, m_str);
		strcat(temp, other_string.m_str);
		CString X(temp);//创建一个对象来保存所得字符串并返回它//不是本类的对象,需要使用到拷贝构造函数
		return X;
	}
	//获取
	const char* getStr() const
	{
		return m_str;
	}//第一个const表示这个字符串在主函数(外部)中使用的时候不允许被修改是一个常量字符串
	//第二个const表示在这个函数内(内部)我们对任何成员都不能修改保证数据的安全
};
int main()
{
	CString one("Hello ");
	CString two("World");
	CString three = one + two;
	cout << three.getStr() << endl;
	return 0;
}

 

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C++ 中,可以使用运算符重载实现字符串连接。具体实现如下: ```cpp #include <iostream> #include <cstring> class String { private: char* str; public: String() : str(nullptr) {} String(const char* s) { str = new char[strlen(s) + 1]; strcpy(str, s); } ~String() { if (str) delete[] str; } // 重载 + 运算符 String operator+ (const String& s) const { String res; res.str = new char[strlen(str) + strlen(s.str) + 1]; strcpy(res.str, str); strcat(res.str, s.str); return res; } // 重载 += 运算符 String& operator+= (const String& s) { char* new_str = new char[strlen(str) + strlen(s.str) + 1]; strcpy(new_str, str); strcat(new_str, s.str); if (str) delete[] str; str = new_str; return *this; } // 重载 << 运算符 friend std::ostream& operator<< (std::ostream& os, const String& s) { os << s.str; return os; } }; int main() { String s1("hello"); String s2("world"); String s3 = s1 + s2; // 使用重载 + 运算符连接字符串 std::cout << s3 << std::endl; s1 += s2; // 使用重载 += 运算符连接字符串 std::cout << s1 << std::endl; return 0; } ``` 在上述代码中,我们定义了一个 `String` 类,其中重载了 `+` 运算符和 `+=` 运算符实现字符串连接。同时,我们还重载了 `<<` 运算符来方便输出字符串。 在 `+` 运算符实现中,我们首先申请一段新的字符串空间,将两个字符串连接起来,最后返回一个新的 `String` 对象。而在 `+=` 运算符实现中,我们首先申请一段新的字符串空间,将两个字符串连接起来,然后释放旧的字符串空间,将指针指向新的字符串空间。 在 `main` 函数中,我们演示了如何使用重载运算符连接字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值