实现代码如下:
1#include <iostream>
2 #include <string.h>3 #include <stdlib.h>
4 using namespace std;
5 class _string{
6 private:
7 char *str;
8 int length;
9
10 public:
11 _string()//默认构造函数
12 {
13 length=0;
14 str=new char[1];
15 str[0]='\0';
16 }
17 _string(const char *a);//构造函数
18 _string(int n,char a);//构造函数,将对象初始化为n个a
19 ~_string(){delete str; length=0;}//析构函数
20 _string(_string &a);//拷贝构造函数
21 int size(){return length;}//获得字符串长度
22 _string operator+(const _string &a);//重载加操作符
23 _string& operator+=(const _string &a);//重载加等于操作符
24 _string& operator=(const _string &a);//重载赋值操作符
25 char &operator[](int n);//重载下标操作符
26 _string substr(int pos,int n);//返回子字符串
27 _string substr(int pos);//返回子字符串
28 //重载输入输出运算符
29 friend istream& operator>>(istream &is,_string& str);
30 friend ostream& operator<<(ostream &os,_string &str);
31
32 };
33 _string::_string(const char *a){
34 length=strlen(a);
35 str=new char[length+1];
36 int i=0;
37 for(;i<length;i++){
38 str[i]=a[i];
39 }
40 str[length]='\0';
41 }
42
43 _string::_string(int n,char a){
44 length=n;
45 str=new char[n+1];
46 int i;
47 for(i=0;i<n;i++)
48 str[i]=a;
49 str[n]='\0';
50 }
51 _string::_string(_string &a){
52 length=strlen(a.str);
53 str=new char[length+1];
54 int i;
55 for(i=0;i<length;i++)
56 str[i]=a.str[i];
57 str[length]='\0';
58 }
59
60 _string _string::_string::operator+(const _string &a){
61 int newlen=length+a.length;
62 char *tmp;
63 tmp=new char[newlen+1];
64 int i,count=0;
65 for(i=0;i<length;i++){
66 tmp[i]=str[i];
67 count++;
68 }
69 for(i=0;count<newlen;count++,i++)
70 tmp[count]=a.str[i];
71 tmp[newlen]='\0';
72 _string temp(tmp);
73 return temp;
74 }
75
76 _string& _string::operator=(const _string &a){
77 if(this==&a)
78 return *this;
79 delete []str;
80 int length=a.length;
81 str=new char[length+1];
82 int i;
83 for(i=0;i<length;i++)
84 str[i]=a.str[i];
85 str[length]='\0';
86 return *this;
}
88 _string& _string::operator+=(const _string &a){
89 int newlen=length+a.length;
90 char *tmp=new char[newlen+1];
91 int i,count=0;
92 for(i=0;i<length;i++){
93 tmp[i]=str[i];
94 count++;
95 }
96 for(i=0;count<newlen;count++,i++){
97 tmp[count]=a.str[i];
98 }
99 tmp[newlen]='\0';
100 _string temp(tmp);
101 *this=temp;
102 return *this;
103 }
104 char &_string::operator[](int n){
105 if(n==0)
106 return this->str[0];
107 else if(n>0&&n<length)
108 return this->str[n-1];
109 else
110 cout<<"overflow\n";
111
112 }
113 //从pos位置开始,返回n个字符
114 _string _string::substr(int pos,int n){
char *p=new char[n+1];
116 int i;
117 for(i=0;i<n;i++,pos++){
118 p[i]=str[pos];
119 }
120 p[n]='\0';
121 _string temp(p);
122 return temp;
123
124 }
125 _string _string::substr(int pos){
126 int size=length-pos+1;
127 char *p=new char[size];
128 int i;
129 for(i=0;i<size;i++,pos++){
130 p[i]=str[pos];
131 }
132 p[size]='\0';
133 _string temp(p);
134 return temp;
135
136 }
137 istream& operator>>(istream &is,_string& str){
138 delete []str.str;
139 str.str=new char[256];
140 is>>str.str;
141 str.length=strlen(str.str);
142 return is;
143
144 }
145 ostream& operator<<(ostream &os,_string &str){
146 return os<<str.str;
147
148 }
149
150
151 int main(){
152 _string s1("hello"),s2(" world"),s("wukong");
153 cout<<"s1="<<s1<<endl;
154 cout<<"s2="<<s2<<endl;
155 cout<<"s="<<s<<endl;
156 s1+=s2;
157 cout<<"s1+=s2:"<<s1<<endl;
158 cout<<"s1="<<s1<<endl;
159 cout<<"s2="<<s2<<endl;
160 cout<<"s1[9]="<<s1[3]<<endl;
161 _string s3;
162 // s3+=s2;
163 // cout<<"s3+=s2:"<<s3<<endl;
164 s3=s+s2;
165 cout<<"s3=s+s2:"<<s3<<endl;
166 //cin>>s3;
167 //cout<<"s3:"<<s3<<endl;
168 _string s4;
169 s4=s3.substr(6,5);
170 cout<<"s4="<<s4<<endl;
171 s4=s3.substr(0,6);
172 cout<<"s4="<<s4<<endl;
173 _string s5(s4);
174 cout<<"s5="<<s5<<endl;
175 _string s6(5,'s');
176 cout<<"s6="<<s6<<endl;
177 _string s7;
178 s7="givechar";//为什么这个也可以呢?
179 cout<<"s7="<<s7<<endl;
180
181 }
运行结果:
UC#g++ string2.cpp
UC#./a.out
s1=hello
s2= world
s=wukong
s1+=s2:hello world
s1=hello world
s2= world
s1[9]=l
s3=s+s2:wukong world
s4= worl
s4=wukong
s5=wukong
s5=givechar
UC#