18.03.21 继承作业
A:全面的MyString
描述
程序填空,输出指定结果
#include <cstdlib> #include <iostream> using namespace std; int strlen(const char * s) { int i = 0; for(; s[i]; ++i); return i; } void strcpy(char * d,const char * s) { int i = 0; for( i = 0; s[i]; ++i) d[i] = s[i]; d[i] = 0; } int strcmp(const char * s1,const char * s2) { for(int i = 0; s1[i] && s2[i] ; ++i) { if( s1[i] < s2[i] ) return -1; else if( s1[i] > s2[i]) return 1; } return 0; } void strcat(char * d,const char * s) { int len = strlen(d); strcpy(d+len,s); } class MyString { // 在此处补充你的代码 }; int CompareString( const void * e1, const void * e2) { MyString * s1 = (MyString * ) e1; MyString * s2 = (MyString * ) e2; if( * s1 < *s2 ) return -1; else if( *s1 == *s2) return 0; else if( *s1 > *s2 ) return 1; } int main() { MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); MyString SArray[4] = {"big","me","about","take"}; cout << "1. " << s1 << s2 << s3<< s4<< endl; s4 = s3; s3 = s1 + s3; cout << "2. " << s1 << endl; cout << "3. " << s2 << endl; cout << "4. " << s3 << endl; cout << "5. " << s4 << endl; cout << "6. " << s1[2] << endl; s2 = s1; s1 = "ijkl-"; s1[2] = 'A' ; cout << "7. " << s2 << endl; cout << "8. " << s1 << endl; s1 += "mnop"; cout << "9. " << s1 << endl; s4 = "qrst-" + s2; cout << "10. " << s4 << endl; s1 = s2 + s4 + " uvw " + "xyz"; cout << "11. " << s1 << endl; qsort(SArray,4,sizeof(MyString),CompareString); for( int i = 0;i < 4;i ++ ) cout << SArray[i] << endl; //s1的从下标0开始长度为4的子串 cout << s1(0,4) << endl; //s1的从下标5开始长度为10的子串 cout << s1(5,10) << endl; return 0; }
输入
无
输出
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. 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-
来源Guo Wei
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 char *p; 2 public: 3 //构造函数 4 MyString(const char *a){ 5 //if(p)delete[]p; 6 p=new char[strlen(a)+1]; 7 strcpy(p,a); 8 } 9 MyString(){ 10 p=new char[1]; 11 p[0]='\0'; 12 } 13 //copy 14 MyString(const MyString&s){ 15 p=new char[strlen(s.p)+1]; 16 strcpy(p,s.p); 17 } 18 //destruct 19 ~MyString(){if(p)delete[]p;} 20 //cout 21 friend ostream&operator<<(ostream&os,MyString &a){ 22 os<<a.p; 23 return os; 24 } 25 //class+class 26 friend MyString operator+(const MyString &a,const MyString &b){ 27 char *res=new char[strlen(b.p)+strlen(a.p)+1]; 28 strcpy(res,a.p); 29 strcat(res,b.p); 30 return MyString(res); 31 } 32 //[] 33 char&operator[](int x){ 34 return p[x]; 35 } 36 //class=class 37 MyString& operator=(const MyString&a){ 38 if(p)delete[]p; 39 p=new char[strlen(a.p)+1]; 40 strcpy(p,a.p); 41 return *this; 42 } 43 //class=char 44 MyString& operator=(char *s){ 45 if(p)delete[]p; 46 p=new char[strlen(s)+1]; 47 strcpy(p,s); 48 return *this; 49 } 50 //class+=char 51 MyString operator +=(const char *s){ 52 *this=*this+MyString(s); 53 return *this; 54 } 55 //char+class 56 friend MyString operator +(char *s,MyString&a){ 57 return MyString(s)+a; 58 } 59 //class+char 60 MyString operator +(char*s){ 61 return *this+MyString(s); 62 } 63 //< 64 friend bool operator<(MyString&a,MyString&b){ 65 return (strcmp(a.p,b.p)==-1); 66 } 67 //> 68 friend bool operator>(MyString&a,MyString&b){ 69 return (strcmp(a.p,b.p)==1); 70 } 71 //= 72 friend bool operator==(MyString&a,MyString&b){ 73 return (strcmp(a.p,b.p)==0); 74 } 75 //() 76 char*operator()(int x,int y){ 77 char *ptr=new char[y+1]; 78 for(int i=0;i<y;i++){ 79 ptr[i]=p[i+x]; 80 } 81 ptr[y]='\0'; 82 return ptr; 83 }
去掉了if(p)delete[]p;一行就过了??emmm……不知道是什么情况
以及vs里不好调……空手调bug
加个分析
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 int main() 2 { 3 MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);//构造函数和复制函数 4 MyString SArray[4] = {"big","me","about","take"}; 5 cout << "1. " << s1 << s2 << s3<< s4<< endl;//输出流重载 6 s4 = s3; 7 s3 = s1 + s3;//加号重载 连接字符串 8 cout << "2. " << s1 << endl; 9 cout << "3. " << s2 << endl; 10 cout << "4. " << s3 << endl; 11 cout << "5. " << s4 << endl; 12 cout << "6. " << s1[2] << endl;//重构[] 返回引用 13 s2 = s1;//重载赋值 14 s1 = "ijkl-";//不知可否用构造函数直接解决 15 s1[2] = 'A' ; 16 cout << "7. " << s2 << endl; 17 cout << "8. " << s1 << endl; 18 s1 += "mnop";//重构+= 19 cout << "9. " << s1 << endl; 20 s4 = "qrst-" + s2;//全局函数重载+ 21 cout << "10. " << s4 << endl; 22 s1 = s2 + s4 + " uvw " + "xyz";//又一个全局函数重载+ 并且重载都返MyString& 不对 临时不能返& 23 cout << "11. " << s1 << endl; 24 qsort(SArray,4,sizeof(MyString),CompareString);//重载<>== 25 for( int i = 0;i < 4;i ++ ) 26 cout << SArray[i] << endl; 27 //s1的从下标0开始长度为4的子串 重载() 28 cout << s1(0,4) << endl; 29 //s1的从下标5开始长度为10的子串 30 cout << s1(5,10) << endl; 31 return 0; 32 }
B:继承自string的MyString
描述
程序填空,输出指定结果
#include <cstdlib> #include <iostream> #include <string> #include <algorithm> using namespace std; class MyString:public string { // 在此处补充你的代码 }; int main() { MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); MyString SArray[4] = {"big","me","about","take"}; cout << "1. " << s1 << s2 << s3<< s4<< endl; s4 = s3; s3 = s1 + s3; cout << "2. " << s1 << endl; cout << "3. " << s2 << endl; cout << "4. " << s3 << endl; cout << "5. " << s4 << endl; cout << "6. " << s1[2] << endl; s2 = s1; s1 = "ijkl-"; s1[2] = 'A' ; cout << "7. " << s2 << endl; cout << "8. " << s1 << endl; s1 += "mnop"; cout << "9. " << s1 << endl; s4 = "qrst-" + s2; cout << "10. " << s4 << endl; s1 = s2 + s4 + " uvw " + "xyz"; cout << "11. " << s1 << endl; sort(SArray,SArray+4); for( int i = 0;i < 4;i ++ ) cout << SArray[i] << endl; //s1的从下标0开始长度为4的子串 cout << s1(0,4) << endl; //s1的从下标5开始长度为10的子串 cout << s1(5,10) << endl; return 0; }
输入
无
输出
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. 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:如果将程序中所有 "MyString" 用 "string" 替换,那么除
了最后两条红色的语句编译无法通过外,其他语句都没有问题,而且输出和前
面给的结果吻合。也就是说,MyString 类对 string 类的功能扩充只体现在最
后两条语句上面。
提示 2: string 类有一个成员函数 string substr(int start,int
length); 能够求从 start 位置开始,长度为 length 的子串
提示 3: C++中,派生类的对象可以赋值给基类对象,因为,一个派生
类对象,也可看作是一个基类对象(大学生是学生)。反过来则不行(学生未
必是大学生) 同样,调用需要基类对象作参数的函数时,以派生类对象作为实参,也是没有问题的来源Guo Wei
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public: 2 //construct 3 MyString():string("\0"){} 4 MyString(const char*s):string(s){} 5 MyString(const MyString&a):string(a){} 6 //+ 7 MyString operator+(const char *b){ 8 string str=*this; 9 str+=b; 10 return MyString(str.c_str()); 11 } 12 MyString operator+(const MyString&a){ 13 string str1=*this,str2=a; 14 str1+=a; 15 return MyString(str1.c_str()); 16 } 17 18 friend MyString operator+(const char*s,const MyString &a){ 19 string str1=a,str2=s; 20 str2+=str1; 21 return MyString(str2.c_str()); 22 } 23 //+= 24 MyString& operator+=(const char*s){ 25 *this=*this+s; 26 return *this; 27 } 28 //() 29 string operator()(int x,int y){ 30 string str; 31 for(int i=0;i<y;i++) 32 str+=(*this)[x+i]; 33 return MyString(str.c_str()); 34 }
早点看到提示就好了……