在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符。
[]运算符重载
+运算符重载+=运算符重载
<<运算符重载
>>运算符重载
String.h:
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#ifndef _STRING_H_
#define _STRING_H_ #include <iostream> using namespace std; class String { public: String( const char *str = ""); String( const String &other); String & operator=( const String &other); String & operator=( const char *str); bool operator!() const; char & operator[]( unsigned int index); const char & operator[]( unsigned int index) const; friend String operator+( const String &s1, const String &s2); String & operator+=( const String &other); friend ostream & operator<<(ostream &os, const String &str); friend istream & |