设计字符串类 myString,若有 myString 类对象 s1、s2 和 s3,重载运算符实现如下操作: 1)重载“+”实现字符串连接功能,使表达式 s1= s2+s3 成立;

设计字符串类 myString,若有 myString 类对象 s1、s2 和 s3,重载运算符实现如下操作:
1)重载“+”实现字符串连接功能,使表达式 s1= s2+s3 成立;
2)重载“<”判断两个字符串的大小,如 if(s1 < s2) cout<<s1<<”<”<<s2;
3)重载“<<”与“>>”,实现字符串的输入与输出操作,如 cin>>s2; cout<<s2<<endl;
4)重载运算符“()”,从字符串对象中返回一个子串。如 s1(2,4)表示返回从子串,即从
s[2](s1 第 3 个字符)开始的子串(包括 s1[2]、s1[3]和 s1[4]三个字符)。
5)重载运算符[],获取下标为 i 的字符,并可以执行对该字符赋值的操作;
6) 重载运算符=,实现字符串对象的赋值操作。

#include <iostream>
#include<cstring>

using namespace std;

class myString
{
public:
	myString(int size, const char*);
	myString();
	~myString();
	myString(const myString&);
	myString operator+(myString&);
	bool operator<(myString&);
	friend ostream& operator<<(ostream&, const myString&);
	friend istream& operator>>(istream&, myString&);
	myString operator()(int, int);
	myString& operator[](int);
	myString& operator=(const myString&);
private:
	int length;
	char* s;
};

myString::myString(int size, const char* s1) :length(size)
{
	s = new char[length + 1]();
	strcpy(s, s1);
}

myString::myString()
{
	length = 0;
	s = new char[length + 1]();
}

myString::~myString()
{
	delete[]s;
	s = nullptr;
}

myString::myString(const myString&s1) {
	length = s1.length;
	s = new char[length + 1]();
	strcpy(s, s1.s);
}

myString myString::operator+(myString& s1) {
	myString S(length + s1.length, s);
	strcat(S.s, s1.s);
	return S;
}

bool myString::operator<(myString& s1) {
	if (strcmp(s, s1.s) >= 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

ostream& operator<<(ostream& os, const myString& s1) {
	os << s1.s << endl;
	return os;
}

istream& operator>>(istream& is, myString& s1) {
	cout << "length:";
	is >> s1.length;
	if (s1.s!=NULL)
	{
		delete[]s1.s;
	}
	s1.s = new char[s1.length+1]();
	cout << "string:";
	is >> s1.s;
	return is;
}

myString myString::operator()(int j, int k) {
	myString s1(k - j + 1, "");
	for (int i = 0; i < k - j + 1; i++)
	{
		s1.s[i] = s[j + i];
	}

	return s1;
}

myString& myString::operator[](int i) {
	cout << "You are goning to change it as:" << endl;
	cin >> s[i];
	return *this;
}

myString& myString::operator=(const myString& s1) {
	length = s1.length;
	if (s!=NULL)
	{
		delete[]s;
	}
	s = new char[length + 1];
	strcpy(s, s1.s);
	return*this;
}

int main()
{
	myString s1(5, "zhang"), s2(2, "li"), s3;
	cin >> s3;
	cout << s3;
	cout << s1 << s2 << s3;
	s3 = s1 + s2;
	cout << s3;
	if (s1 < s2)
	{
		cout << s1 << "<" << s2;
	}
	myString s5 = s1(2, 4);
	cout << s5;
	cout << s2[1];
	return 0;
}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们定义一个字符串: ```cpp #include <cstring> class MyString { public: MyString(const char* str = nullptr); MyString(const MyString& other); ~MyString(); MyString& operator=(const MyString& other); const char* c_str() const; private: char* m_data; }; MyString::MyString(const char* str) { if (str == nullptr) { m_data = new char[1]; *m_data = '\0'; } else { m_data = new char[strlen(str) + 1]; strcpy(m_data, str); } } MyString::MyString(const MyString& other) { m_data = new char[strlen(other.m_data) + 1]; strcpy(m_data, other.m_data); } MyString::~MyString() { delete[] m_data; } MyString& MyString::operator=(const MyString& other) { if (this != &other) { delete[] m_data; m_data = new char[strlen(other.m_data) + 1]; strcpy(m_data, other.m_data); } return *this; } const char* MyString::c_str() const { return m_data; } ``` 接下来,我们可以用成员函数和友元函数分别实现重载“+=”运算符: ```cpp // 成员函数实现 MyString& MyString::operator+=(const MyString& other) { char* temp = new char[strlen(m_data) + strlen(other.m_data) + 1]; strcpy(temp, m_data); strcat(temp, other.m_data); delete[] m_data; m_data = temp; return *this; } // 友元函数实现 MyString operator+(const MyString& str1, const MyString& str2) { MyString temp; delete[] temp.m_data; temp.m_data = new char[strlen(str1.m_data) + strlen(str2.m_data) + 1]; strcpy(temp.m_data, str1.m_data); strcat(temp.m_data, str2.m_data); return temp; } MyString& MyString::operator+=(const MyString& other) { *this = *this + other; return *this; } ``` 使用示例: ```cpp #include <iostream> using namespace std; int main() { MyString str1("Hello"); MyString str2(" World!"); str1 += str2; // 成员函数实现 cout << str1.c_str() << endl; MyString str3("Hello"); MyString str4(" C++!"); MyString str5 = str3 + str4; // 友元函数实现 cout << str5.c_str() << endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值