.定义一个类CmpString,私有继承自基类BaseString,实现字符串的比较功能。

来源:百度知道 编辑:UC知道 时间:2024/06/01 03:43:06

#include <cstring>
#include <string>
#include <iostream>
using namespace std;

class BaseString
{
public:
BaseString()
{
length = 0;
buffer = NULL;
}
BaseString(const char *str);
~BaseString()
{
delete[] buffer;
}

protected:
char *buffer;
int length;
};

BaseString::BaseString(const char *str)
{
if (str && *str)
{
length = strlen(str);
buffer = new char[length + 1];
strcpy(buffer, str);
}
else
{
length = 0;
buffer = NULL;
}
}

class CmpString : private BaseString
{
public:
CmpString() {}
CmpString(const char *str) : BaseString(str) {}
CmpString(const CmpString &o) : BaseString(o.buffer)
{
}
CmpString & operator=(const CmpString &o)
{
if (this != &o)
{
delete[