求一c++类,以作参考

来源:百度知道 编辑:UC知道 时间:2024/05/30 00:33:12
编写一个MyString类
要求不使用<string.h>等现有的字符串类库,完成以下功能:
字符串的初始化(空构造与拷贝构造)
int SetString(MyString str); 或者 int SetString(char* p);//设定一个字符串的初 始值
char GetAt(int Index); //得到指定位置的字符
int SetAt(int Index, char ch);//设定某一位置的字符(index位置字符为ch)
int Compare(const MyString &str1,const MyString &str1);//字符串比较
char * C_str();//返回C风格的字符串(字符数组)

参靠下MFC的CString吧!

以下是String.h的内容:
class String
{
friend ostream& operator<<(ostream & os, const String& str);
public:
String();
~String();
String(char *cString);
String(const String &string);
String &operator = (const String &sourceStr);
String &operator = (const char *sourceStr);
char GetAt(int index);
bool SetAt(int index, const char ch);
int CompareTo(const String& anotherString);
char * C_str(){return cString;}
int GetLength()const{return length(cString);}
private:
int length(const char * cStr)const;
void copy(const char* from, char *to);
public:
char *cString;
};

以下是String.cpp的内容:
#include<iostream.h>
#include<assert.h>
#include "String.h"

int main()
{
String str1("asfkkkk");
String str2 = "jsfkkkk";
cout<<str1<<"\t"<<