Created
October 16, 2016 04:50
-
-
Save YaoC/7bdd91df2d6dbd5e9a6efd9d13e293bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "String.h" | |
using namespace std; | |
int main() { | |
String t1("abcd"); | |
String t2("efg"); | |
cout <<"t1的大小为: "<< t1.size() << endl; | |
cout << "t2的大小为: " << t2.size() << endl; | |
cout << "t1+t2 的大小为: " << (t1 + t2).size() << endl; | |
system("pause"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAXLEN 128 | |
class String { | |
public: | |
int size()const; | |
String(const char* s = ""); | |
String operator + (const String s); | |
char & operator [] (int index); | |
char operator [] (int index)const; | |
private: | |
char buf[MAXLEN]; | |
}; | |
int String::size()const { | |
int i = 0; | |
while (buf[i] != '\0') { | |
i++; | |
} | |
return i; | |
} | |
char & String:: operator [] (int index) { | |
return buf[index]; | |
} | |
char String::operator [] (int index)const { | |
const char t = buf[index]; | |
return t; | |
} | |
String::String(const char* s) { | |
int i = 0; | |
while (s[i] != '\0') { | |
buf[i] = s[i]; | |
i++; | |
} | |
buf[i] = '\0'; | |
} | |
String String::operator + (const String s) { | |
char temp[MAXLEN] = { '\0' }; | |
int i = 0; | |
while (buf[i] != '\0') { | |
temp[i] = buf[i]; | |
i++; | |
} | |
cout << s.size() << endl; | |
for (int j = 0; j < s.size(); j++) { | |
temp[i] = s[j]; | |
i++; | |
} | |
temp[i] = '\0'; | |
return String(temp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment