C++面向对象程序设计之字符串类的相关操作
1.实验任务与要求:
1)编写一个表示字符串的类String。
2)改写默认构造函数,生成一个空串,(不分配内存)。
3)添加一个带参数的构造函数,根据字符串常量构造一个字符串对象;String s(“HelloWorld”)。
4)在析构函数中释放内存。
5)编写拷贝构造函数和拷贝赋值函数(operator=)。(注意用深拷贝)
6)编写成员函数GetLength,返回字符串的长度。
7)编写成员函数bool GetChar(int index, char &ch),获取指定位置index处的字符,通过函数参数ch传出,如果index在合理范围内,返回true,否则返回false。
8)编写成员函数bool SetChar(int index, const char ch),把字符串位置index处的字符改变为ch,如果index在合理范围内,返回true,否则返回false。
9)编写成员函数Concat(const String &s),把字符串s拼接到当前对象表示的字符串后面,即:String a(“Hello”); String b(“World”); 则,a.Concat(b);之后,a中的内容变为”HelloWorld”。
10)编写成员函数Display,在控制台输出该字符串的内容。
11)要求:
在指定的testString.cpp中填写你的代码,不要修改main()函数。
12)提交内容包括:
A.本实验报告,请把实验报告文件名中的########替换为你的学号,XXX替换为你的姓名。
B.源代码。上述函数编写在同一个源文件中,源文件命名规则为CPP-4-########.cpp,其中########替换为你的学号。
2.把程序运行结果截图粘贴在下方:
3.把你完成的源文件插入到下方:
#include <iostream>
#include <cstring>
using namespace std;
/*请把下面的字符串分别改为你的姓名和学号*/
const char *name = " ";
const char *ID = " ";
/*在下面添加你的代码
类的声明和实现代码都写在下方*/
class String {
private:
int length;//字符串的长度
char *pData;//字符串的内容,注意:字符串以'\0'结尾
public:
/*改写默认构造函数*/
String() {
pData=NULL;
length=0;
}
/*构造函数*/
String(const char *s) {
pData = new char[strlen(s) + 1];
memset(pData, 0, sizeof(pData));
length = 0;
while (s[length] != '\0')
{
pData[length] = s[length];
length++;
}
pData[length] = '\0';
}
/*在下方编写拷贝构造函数*/
String(const String &T) {
pData = new char[T.length + 1];
memset(pData, 0, sizeof(pData));
for (int i = 0;i <= T.length;i++)
pData[i] = T.pData[i];
length = T.length;
}
/*在下方编写拷贝赋值运算operator=*/
String& operator=(const String &other) {
if (this == &other)
return *this;
delete[]pData;
pData = new char[strlen(other.pData) + 1];
memset(pData, '\0', sizeof(pData));
for (int i = 0;i < other.length;i++)
pData[i] = other.pData[i];
length = other.length;
return *this;
}
/*析构函数*/
~String() {
delete[]pData;
pData = NULL;
length = 0;
}
/*在下方编写其它成员函数*/
int GetLength()
{
return length;
}
bool GetChar(int index, char &ch)
{
if (index >= length || index<0)
return false;
else
{
ch = pData[index];
return true;
}
}
bool SetChar(int index, const char ch)
{
if (index >= length || index<0)
return false;
else
{
pData[index] = ch;
return true;
}
}
void Concat(const String &s)
{
char* str = new char[length + s.length + 1];
int i;
for (i = 0;i < length;i++)
{
str[i] = pData[i];
}
for (int j = 0;i < length + s.length + 1;i++, j++)
{
str[i] = s.pData[j];
}
length = length + s.length;
delete[]pData;
pData = str;
}
/*Display函数不用改写*/
void Display() const {
if (0 != pData)
std::cout << pData;
std::cout << endl;
}
};
/*上述代码编写完成之后,请把下面的宏改为:
#define TEST 1
*/
#define TEST 1
/*=======================================================*/
/*以下代码不要修改*/
/*=======================================================*/
int main(void) {
std::cout << name << " " << ID << endl;
#if TEST
String s1;
s1.Display();
String s2("Hello World!");
s2.Display();
cout << s2.GetLength() << endl;
String s3(" C++ Programming.");
s3.Display();
cout << s3.GetLength() << endl;
s2.Concat(s3);
s2.Display();
cout << s2.GetLength() << endl;
char ch = 0;
if (s2.GetChar(0, ch)) {
s2.SetChar(0, 'h');
s2.Display();
}
String s4 = String("My God!");
s4.Display();
#endif // TEST
return 0;
}