2、实现一个自定义的字符串类String。 定义具有两个私有数据成员,分别表示字符串的存储地址(char*类型)和长度(int类型)。实现字符串类String的构造函数、析构函数

2、实现一个自定义的字符串类String。

i. 定义具有两个私有数据成员,分别表示字符串的存储地址(char*类型)和长度(int类型)。实现字符串类String的构造函数、析构函数、拷贝构造函数、赋值运算。
ii. 实现运算符重载:String + String、String + char *
iii. 重载运算符 << 和 >> ,实现字符串的输入和输出。
iv. 在main函数写一段简单的使用该字符类(包含上述运算)的测试程序

test.cpp

#include"string.h"
#include<iostream>
using namespace std;
int main()
{
	String s1, s2;
	s1 = "chenhaoran";
	s2 = "zhangchao";
	cout << s1<<endl;
	String s3(s2);
	cout << s3<<endl;
	String s4 = s3;
	cout << s4 << endl;
	//cout << s4 + s2 << endl;
	String s6 = s4 + s2;
	s1 = s4 + s2;//这不是调用复制构造函数
	cout << s6 << endl;
	return 0;
}

string.h

#pragma once
#include<ostream>
#include<istream>
using namespace std;
class String {
private:
	char *_str;
	int _size;
public:
	String();
	~String();
	String(const String &s);
	//friend String operator +(const String &temp1, const String &temp2);
	//friend String operator +(const String &temp1, const char * temp2);
	String operator+(const String &temp);
	String operator+(const char *temp);
	//String &operator =(String temp);
	String &operator =(const char *temp);
	friend ostream &operator<< (ostream &out, String &temp);//这里为什么是取地址符
	friend istream &operator>>(istream &in,String &temp);

};

string.cpp

#include"string.h"
#include<string.h>
#include<iostream>
using namespace std;
String::String()
{
	_str = new char[20];
	_size = 0;
}
String::~String()
{
	if (this->_str != NULL)
	{
		delete[] this->_str;	
		this->_str = NULL;
		this->_size = 0;
	}
}
String::String(const String &s)
{
	int len = strlen(s._str);
	_str = new char[len + 20];
	strcpy(_str, s._str);
	_size = s._size;
}
String String::operator+(const String &temp)
{
	String *p=new String();
	p->_str = new char[strlen(this->_str) + strlen(temp._str) + 1];
	strcpy(p->_str, this->_str);
	strcat(p->_str, temp._str);
	return *p;
}
/*
String operator +(const String &temp1,const String &temp2)
{
	//String temp(temp1);
	String temp;
	strcpy(temp._str, temp1._str);
		//strcat(temp._str, temp1._str);
	temp._size += temp1._size;
	strcat(temp._str, temp2._str);
	temp._size += temp2._size;
	return temp;
}
String operator +(const String &temp1,const char *temp2)
{
	String temp(temp1);
	int len = strlen(temp2);
	strcat(temp._str, temp2);
	temp._size += len;
	return temp;

}
*/
//String &String::operator=(String temp)
//{
	
//	strcpy(this->_str, temp._str);
//	this->_size = temp._size;
//	return *this;
//}
String &String::operator=(const char *temp)
{
	memset(this, '\0', this->_size);
	strcpy(this->_str, temp);
	this->_size = strlen(temp);
	return *this;
}
ostream &operator<< (ostream &out,String &temp)
{
	out << temp._str;
	return out;
}
istream &operator>> (istream &in,String &temp)
{
	in >> temp._size;
	return in;
}
定义一个MyString实现两个字符串相加。在该中,我们将使用字符指针来实现字符串的深拷贝。 首先,我们需要在私有成员定义一个字符指针来表示字符串。我们可以将其命名为`str`。此外,我们还可以定义一个整型成员变量`len`来表示字符串长度。 接下来,我们可以构造一个默认构造函数,用来初始化字符串为空字符串并将长度设置为0。另外,我们还可以构造一个带参构造函数,用来初始化字符串并计算长度。 接着,我们需要实现一个字符串相加的成员函数`stringAdd`。该函数接收一个参数,即待相加的字符串。首先,我们需要动态分配一个大小适当的内存空间来存储两个字符串的结果。然后,我们可以使用`strcpy`函数将第一个字符串拷贝到结果字符串中。接着,我们使用`strcat`函数将第二个字符串拼接到结果字符串之后。最后,我们返回结果字符串。 由于我们使用了字符指针来实现字符串的深拷贝,因此在析构函数中,我们需要释放掉动态分配的内存空间,以避免内存泄漏。 最后,我们可以在的外部使用该实现字符串相加的功能。 下面是一个示例代码: ```cpp #include <iostream> #include <cstring> class MyString { private: char* str; int len; public: MyString() { str = new char[1]; str[0] = '\0'; len = 0; } MyString(const char* s) { len = strlen(s); str = new char[len + 1]; strcpy(str, s); } ~MyString() { delete[] str; } char* stringAdd(const MyString& s) { char* result = new char[len + s.len + 1]; strcpy(result, str); strcat(result, s.str); return result; } }; int main() { MyString s1("Hello"); MyString s2(" World"); char* result = s1.stringAdd(s2); std::cout << result << std::endl; delete[] result; return 0; } ``` 这样,我们就定义一个MyString,可以实现两个字符串的相加操作,并通过字符指针实现字符串的深拷贝。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值