谁能帮我写这个程序,满意再加100

来源:百度知道 编辑:UC知道 时间:2024/06/18 05:00:48
下面是一个使用字符串类CString 的程序实例及结果,请设计并完成字符串类CString
#include <iostream.h>
void main ()
{
CString s1 (“Hang”), s2(“ Zhou”);
CString s3;

s3 = s1 + s2;
CString s4 (s3);
s4 [4] = ‘+’;
cout << “s1 = “ << s1 << endl;
cout << “s2 = “ << s2 << endl;
cout << “s3 = “ << s3 << endl;
cout << “s4 = “ << s4 << endl;

}
执行结果 :
s1=Hang
s2= Zhou
s3=Hang Zhou
s4=Hang+Zhou
要用c++写的

其实就是难在重载运算符,呵呵,这个不怎么常用,所以,可能运行时会有些许毛病,但是方法还是可行的,你可以在我下面的代码中修改。由于没有达到楼主的要求,故在此,只做参考了啊~!~~~~~~

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class CString
{
private:
char *str;
int len;
public:
CString();
CString(const CString &str);
CString(const char *);
~CString() { delete[] this->str;}
int length() { return this->len;}
CString &operator+(const CString &);
CString &operator=(const CString &);
CString &operator+=(const CString &);
CString &operator+=(const char *);
int operator==(const CString &str);
int operator<(CString &str);
int operator>(CString &str);
int operator!();
char &operator[](int);
friend ostream &operator<<(ostream &output, CSt