在线等,谢谢亲们了。定义一个字符串类string,其中包括两个数据成员:(补充在下面)
指向存放字符串的动态数组首地址的指针ps和字符串长度len,定义一个字符串类string,其中包括两个数据成员:指向存放字符串的动态数组首地址的指针ps和字符串长度len...
指向存放字符串的动态数组首地址的指针ps和字符串长度len,定义一个字符串类string,其中包括两个数据成员:指向存放字符串的动态数组首地址的指针ps和字符串长度len,并定义如下成员函数:1)带参数P的构造函数,其功能是申请一个动态数组存放P所指的字符串;2)析构函数;3)拷贝构造函数;4)显示字符串对象成员函数print();5) 类string的友元函数contact(),用于连接两个字符串对象;6)在主函数中测试各函数的功能。
展开
1个回答
展开全部
#include <iostream>
#include <memory.h>
using namespace std;
class c_string {
typedef unsigned size_t;
friend c_string& contact(c_string &str1, const c_string& str2);
public:
c_string(size_t length = 20) : m_nLength(length) {
m_pStr = new char[m_nLength + 1];
memset(m_pStr, 0, m_nLength + 1);
}
c_string(const char* pStr) {
m_nLength = strlen(pStr);
m_pStr = new char[m_nLength + 1];
memcpy(m_pStr, pStr, m_nLength + 1);
}
c_string(const c_string& cstr) {
m_nLength = cstr.length();
m_pStr = new char[m_nLength + 1];
memcpy(m_pStr, cstr.c_str(), m_nLength + 1);
}
c_string& operator=(const c_string& cstr) {
if (this == &cstr) {
return *this;
}
delete m_pStr;
m_nLength = cstr.length();
m_pStr = new char[m_nLength + 1];
memcpy(m_pStr, cstr.c_str(), m_nLength + 1);
return *this;
}
void print() const {
cout << m_pStr << endl;
}
~c_string() {
if (m_pStr != NULL) {
delete m_pStr;
}
}
size_t length() const {
return m_nLength;
}
const char* c_str() const {
return m_pStr;
}
private:
char * m_pStr;
size_t m_nLength;
};
c_string& contact(c_string &str1, const c_string& str2) {
if (str2.length() == 0) {
return str1;
}
char *tem = new char[str1.m_nLength + str2.m_nLength + 1];
memcpy(tem, str1.m_pStr, str1.m_nLength);
memcpy(tem + str1.m_nLength, str2.m_pStr, str2.m_nLength + 1);
delete str1.m_pStr;
str1.m_pStr = tem;
return str1;
}
int main() {
char str[] = "hello world";
c_string *_str = new c_string(str);
_str->print();
c_string _str2(*_str);
_str2.print();
c_string _str3;
_str3 = *_str;
delete _str;
_str3.print();
c_string _str4(" how are you ?");
contact(_str2, _str4);
_str2.print();
return 0;
}
//这儿没有考虑new delete的优化问题,没有如同vector那样的分配管理内存
#include <memory.h>
using namespace std;
class c_string {
typedef unsigned size_t;
friend c_string& contact(c_string &str1, const c_string& str2);
public:
c_string(size_t length = 20) : m_nLength(length) {
m_pStr = new char[m_nLength + 1];
memset(m_pStr, 0, m_nLength + 1);
}
c_string(const char* pStr) {
m_nLength = strlen(pStr);
m_pStr = new char[m_nLength + 1];
memcpy(m_pStr, pStr, m_nLength + 1);
}
c_string(const c_string& cstr) {
m_nLength = cstr.length();
m_pStr = new char[m_nLength + 1];
memcpy(m_pStr, cstr.c_str(), m_nLength + 1);
}
c_string& operator=(const c_string& cstr) {
if (this == &cstr) {
return *this;
}
delete m_pStr;
m_nLength = cstr.length();
m_pStr = new char[m_nLength + 1];
memcpy(m_pStr, cstr.c_str(), m_nLength + 1);
return *this;
}
void print() const {
cout << m_pStr << endl;
}
~c_string() {
if (m_pStr != NULL) {
delete m_pStr;
}
}
size_t length() const {
return m_nLength;
}
const char* c_str() const {
return m_pStr;
}
private:
char * m_pStr;
size_t m_nLength;
};
c_string& contact(c_string &str1, const c_string& str2) {
if (str2.length() == 0) {
return str1;
}
char *tem = new char[str1.m_nLength + str2.m_nLength + 1];
memcpy(tem, str1.m_pStr, str1.m_nLength);
memcpy(tem + str1.m_nLength, str2.m_pStr, str2.m_nLength + 1);
delete str1.m_pStr;
str1.m_pStr = tem;
return str1;
}
int main() {
char str[] = "hello world";
c_string *_str = new c_string(str);
_str->print();
c_string _str2(*_str);
_str2.print();
c_string _str3;
_str3 = *_str;
delete _str;
_str3.print();
c_string _str4(" how are you ?");
contact(_str2, _str4);
_str2.print();
return 0;
}
//这儿没有考虑new delete的优化问题,没有如同vector那样的分配管理内存
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询