作业5-继承和派生

1.全面的MyString

输入

输出

1. abcd-efgh-abcd-
2. abcd-
3.
4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijAl-
9. ijAl-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
big
me
take
abcd
qrst-abcd-

  1 /*使程序输出指定结果*/ 
  2 #include <cstdlib>
  3 #include <iostream>
  4 using namespace std;
  5 int strlen(const char * s) 
  6 {    int i = 0;
  7     for(; s[i]; ++i);
  8     return i;
  9 }
 10 
 11 void strcpy(char * d,const char * s)
 12 {
 13     int i = 0;
 14     for( i = 0; s[i]; ++i)
 15         d[i] = s[i];
 16     d[i] = 0;
 17         
 18 }
 19 
 20 int strcmp(const char * s1,const char * s2)
 21 {
 22     for(int i = 0; s1[i] && s2[i] ; ++i) {
 23         if( s1[i] < s2[i] )
 24             return -1;
 25         else if( s1[i] > s2[i])
 26             return 1;
 27     }
 28     return 0;
 29 }
 30 
 31 void strcat(char * d,const char * s)
 32 {
 33     int len = strlen(d);
 34     strcpy(d+len,s);
 35 }
 36 
 37 class MyString
 38 {
 39 // 在此处补充你的代码
 40     char *p;
 41 public:
 42     MyString(const char * s = NULL) { //擅于利用缺省的参数,不要再写一个 
 43         if(s) {
 44             p = new char[strlen(s) + 1];
 45             strcpy(p,s);
 46         }
 47         else
 48             p = NULL;
 49 
 50     }
 51     ~MyString() { if(p) delete [] p; } 
 52     MyString(const MyString & s){
 53         if(s.p==NULL) p = NULL;
 54         else{
 55             p = new char[strlen(s.p) + 1];
 56             strcpy(p,s.p);
 57         }
 58     }
 59     MyString & operator=(const MyString & s) {
 60         if(p) delete [] p;
 61         if(s.p==NULL){
 62             p = NULL; return * this;
 63         }
 64         p = new char[strlen(s.p)+1]; 
 65         strcpy(p, s.p);
 66         return * this; 
 67     }
 68     MyString & operator=(const char * s) {
 69         if(p) delete [] p;
 70         if(s==NULL){
 71             p = NULL; return * this;
 72         }
 73         p = new char[strlen(s)+1]; 
 74         strcpy(p, s);
 75         return * this; 
 76     }
 77     char & operator[](const int i){
 78         return p[i];
 79     }
 80     friend MyString operator+(const MyString & a, const MyString & b){
 81         MyString ans;
 82         ans.p = new char[strlen(a.p)+strlen(b.p)+1];
 83         strcpy(ans.p, a.p);
 84         strcat(ans.p, b.p); //不能改变原来的a 
 85         return ans;
 86     }
 87     MyString & operator+=(const char * s) {
 88         MyString temp(s);
 89         *this = *this+temp;
 90         return * this; 
 91     }
 92     bool operator<(const MyString s){
 93         return (strcmp(p, s.p)==-1);
 94     }
 95     bool operator>(const MyString s){
 96         return (strcmp(p, s.p)==1);
 97     }
 98     bool operator==(const MyString s){
 99         return (strcmp(p, s.p)==0);
100     }
101     char * operator()(int start, int len){
102         char *temp = new char[len+1];
103         for (int i = start; i < start+len; i++){
104             temp[i-start] = p[i];
105         }
106         temp[len] = '\0';
107         return temp;
108     } 
109     friend ostream & operator<<(ostream & o, const MyString & s){
110         if(s.p==NULL )return o; // 空指针就不输出了 
111         o<<s.p;
112         return o;
113     } 
114     
115 };
116 
117 
118 int CompareString( const void * e1, const void * e2)
119 {
120     MyString * s1 = (MyString * ) e1;
121     MyString * s2 = (MyString * ) e2;
122     if( * s1 < *s2 ) //重载小于号 
123     return -1;
124     else if( *s1 == *s2)
125     return 0;
126     else if( *s1 > *s2 ) //重载大于号 
127     return 1;
128 }
129 
130 int main()
131 {
132     MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); //构造函数、复制构造函数 
133     MyString SArray[4] = {"big","me","about","take"};
134     cout << "1. " <<s1<<s2<<s3<< s4<< endl; //重载流插入  不能读 空指针 
135     s4 = s3; //重载等于号 
136     s3 = s1 + s3; // 重载+号 
137     cout << "2. " << s1 << endl;
138     cout << "3. " << s2 << endl;
139     cout << "4. " << s3 << endl;
140     cout << "5. " << s4 << endl;
141     cout << "6. " << s1[2] << endl;
142     s2 = s1;
143     s1 = "ijkl-"; //重载等于号 
144     s1[2] = 'A' ; // 重载中括号 
145     cout << "7. " << s2 << endl;
146     cout << "8. " << s1 << endl;
147     s1 += "mnop"; //重载+= 
148     cout << "9. " << s1 << endl;
149     s4 = "qrst-" + s2;
150     cout << "10. " << s4 << endl;
151     s1 = s2 + s4 + " uvw " + "xyz";
152     cout << "11. " << s1 << endl;
153     qsort(SArray,4,sizeof(MyString),CompareString);
154     for( int i = 0;i < 4;i ++ )
155     cout << SArray[i] << endl;
156     //s1的从下标0开始长度为4的子串
157     cout << s1(0,4) << endl; //重载小括号 
158     //s1的从下标5开始长度为10的子串
159     cout << s1(5,10) << endl;  
160     return 0;
161 }

备注:这道题其实没涉及到继承和派生,就是重温运算符重载。唯一一个有意思的地方,就是cout<<字符串空指针会导致后面的输出都没办法继续输出,所以在重载的时候要判断一下,如果是空指针就不输出才行。还有就是重温一下重载[]和(),注意一下返回值是什么。还有就是善于利用参数缺省的构造函数,这样就不用写两个构造函数了qwq

posted @ 2020-03-15 16:52  timeaftertime  阅读(179)  评论(0编辑  收藏  举报