设计一个字符串类MyString,该类包含有: 1) 数据成员char* content、int capacity、int length;其中content用来保存字符内容,capacity用来表示content的容量,length表示存放的字符个数;提示:一旦出现存储空间不足,MyString要能实现自动扩容,并完成必要的数据迁移,capacity的是按照32的倍数增长。 2)MyString类具有如下几个构造函数: MyString(); //产生空串,分配初始存储空间32,即capacity=32, length=0 MyString(int iVal); //将iVal转成字符串,并初始化成员变量content MyString(float fVal);//将fVal转成字符串,并初始化成员变量content MyString(char* s); //用s来初始化成员变量content MyString(MyString& obj);//拷贝构造函数 3)重载如下运算符: //字符串连接运算, MyString operator+(char* s); MyString operator+(char c); MyString operator+(MyString obj); MyString operator=(char* ); MyString operator=(MyString obj); char operator[](int index); 4)以友元函数的形式定义运算符+的重载,实现单个字符与MyString的连接运算,例如: str = ‘c’+str; //假设str是MyString的对象 5)析构函数 ~MyString(); 其他成员函数可自行扩充。并编写测试主程序,逐个测试这些构造函数和运算符重载。
时间: 2024-04-14 18:30:40 浏览: 167
这是一个实现上述要求的字符串类MyString的示例代码:
```cpp
#include <iostream>
#include <cstring>
class MyString {
private:
char* content;
int capacity;
int length;
public:
MyString();
MyString(int iVal);
MyString(float fVal);
MyString(char* s);
MyString(const MyString& obj);
~MyString();
MyString operator+(char* s);
MyString operator+(char c);
MyString operator+(const MyString& obj);
MyString operator=(char* s);
MyString operator=(const MyString& obj);
char operator[](int index);
friend MyString operator+(char c, const MyString& obj);
};
MyString::MyString() {
capacity = 32;
content = new char[capacity];
length = 0;
}
MyString::MyString(int iVal) {
capacity = 32;
content = new char[capacity];
length = sprintf(content, "%d", iVal);
}
MyString::MyString(float fVal) {
capacity = 32;
content = new char[capacity];
length = sprintf(content, "%f", fVal);
}
MyString::MyString(char* s) {
capacity = 32;
int sLength = strlen(s);
while (capacity <= sLength) {
capacity += 32;
}
content = new char[capacity];
strcpy(content, s);
length = sLength;
}
MyString::MyString(const MyString& obj) {
capacity = obj.capacity;
content = new char[capacity];
strcpy(content, obj.content);
length = obj.length;
}
MyString::~MyString() {
delete[] content;
}
MyString MyString::operator+(char* s) {
int sLength = strlen(s);
while (capacity < length + sLength) {
capacity += 32;
char* newContent = new char[capacity];
strcpy(newContent, content);
delete[] content;
content = newContent;
}
strcat(content, s);
length += sLength;
return *this;
}
MyString MyString::operator+(char c) {
if (capacity <= length + 1) {
capacity += 32;
char* newContent = new char[capacity];
strcpy(newContent, content);
delete[] content;
content = newContent;
}
content[length] = c;
content[length + 1] = '\0';
length++;
return *this;
}
MyString MyString::operator+(const MyString& obj) {
int objLength = obj.length;
while (capacity < length + objLength) {
capacity += 32;
char* newContent = new char[capacity];
strcpy(newContent, content);
delete[] content;
content = newContent;
}
strcat(content, obj.content);
length += objLength;
return *this;
}
MyString MyString::operator=(char* s) {
int sLength = strlen(s);
while (capacity < sLength) {
capacity += 32;
delete[] content;
content = new char[capacity];
}
strcpy(content, s);
length = sLength;
return *this;
}
MyString MyString::operator=(const MyString& obj) {
if (this == &obj) {
return *this;
}
capacity = obj.capacity;
delete[] content;
content = new char[capacity];
strcpy(content, obj.content);
length = obj.length;
return *this;
}
char MyString::operator[](int index) {
if (index < 0 || index >= length) {
std::cerr << "Index out of range!" << std::endl;
return '\0';
}
return content[index];
}
MyString operator+(char c, const MyString& obj) {
MyString result;
result.capacity = obj.capacity + 1;
result.content = new char[result.capacity];
result.content[0] = c;
strcpy(result.content + 1, obj.content);
result.length = obj.length + 1;
return result;
}
int main() {
MyString str1; // Testing default constructor
std::cout << "str1: " << str1[0] << std::endl;
MyString str2(123); // Testing int constructor
std::cout << "str2: " << str2[0] << std::endl;
MyString str3(3.14f); // Testing float constructor
std::cout << "str3: " << str3[0] << std::endl;
MyString str4("Hello"); // Testing char* constructor
std::cout << "str4: " << str4[0] << std::endl;
MyString str5 = str4; // Testing copy constructor
std::cout << "str5: " << str5[0] << std::endl;
str5 = " World"; // Testing char* assignment operator
std::cout << "str5: " << str5[0] << std::endl;
str5 = str4; // Testing MyString assignment operator
std::cout << "str5: " << str5[0] << std::endl;
MyString str6 = 'c' + str5; // Testing friend operator+ overload
std::cout << "str6: " << str6[0] << std::endl;
MyString str7 = 'd' + str5; // Testing operator+ overload
std::cout << "str7: " << str7[0] << std::endl;
return 0;
}
```
这个示例代码实现了MyString类,并分别测试了各个构造函数和运算符重载的功能。注意,为了简化示例,这里没有实现完整的成员函数,你可以根据需要自行扩展。
阅读全文