作业5-继承和派生

作业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] = '