《C++ Primer Plus》第12章的编程练习2。这一章介绍类和动态内存分配,相当精彩。
编写简单的String类,并测试。
1 //string1.h -- fixed and augmented string class defination 2 #include <iostream> 3 using std::ostream; 4 using std::istream; 5 6 #ifndef STRING1_H_ 7 #define STRING1_H_ 8 class String 9 { 10 private: 11 char *str; // pointer to string 12 int len; // length of string 13 static int num_strings; // number of objects 14 enum {CINLIM = 90}; // cin input limit 15 public: 16 // constructor and other methods 17 String(const char *s); // constructor 18 String(); // default constructor 19 String(const String &); // copy constructor 20 ~String(); // destructor 21 int length() const {return len;} 22 void stringlow(); // Added Stringlow 23 void stringup(); // Added Stringup 24 int has(const char); // Added StringCharNum 25 26 //overloaded operator methods 27 String& operator=(const String &); 28 String& operator=(const char *); 29 char& operator[](int i); 30 const char& operator[](int i) const; 31 32 //overloaded operator friends 33 friend bool operator<(const String &st, const String &st2); 34 friend bool operator>(const String &st1, const String &st2); 35 friend bool operator==(const String &st, const String &st2); 36 friend ostream& operator<<(ostream &os, const String &st); 37 friend istream& operator>>(istream &is, String &st); 38 friend String operator+(const String &s1, const String &s2); 39 40 //static function 41 static int HowMany(); 42 }; 43 #endif
1 // string1.cpp -- String class methods 2 #include <cstring> // string.h for some 3 #include "string2.h" // includes <iostream> 4 #include <cctype> 5 using std::cin; 6 using std::cout; 7 8 // initializing static class member 9 10 int String::num_strings = 0; 11 12 // static method 13 int String::HowMany() 14 { 15 return num_strings; 16 } 17 18 // class methods 19 String::String(const char * s) // construct String from C string 20 { 21 len = strlen(s); // set size 22 str = new char[len + 1]; // allot storage 23 strcpy(str, s); // initialize pointer 24 num_strings++; // set object count 25 } 26 27 String::String() // default constructor 28 { 29 len = 4; 30 str = new char[1]; 31 str[0] = '\0'; // default string 32 num_strings++; 33 } 34 35 String::String(const String & st) 36 { 37 num_strings++; // handle static member update 38 len = st.len; // same length 39 str = new char [len + 1]; // allot space 40 strcpy(str, st.str); // copy string to new location 41 } 42 43 String::~String() // necessary destructor 44 { 45 --num_strings; // required 46 delete [] str; // required 47 } 48 49 void String::stringlow() 50 { 51 int i=0; 52 while (str[i]) 53 { 54 str[i]=tolower(str[i]); 55 i++; 56 } 57 } 58 void String::stringup() 59 { 60 int i=0; 61 while (str[i]) 62 { 63 str[i]=toupper(str[i]); 64 i++; 65 } 66 } 67 68 int String::has(const char c) 69 { 70 int i=0,cc=0; 71 while(str[i]) 72 { 73 if(str[i]==c) 74 { 75 cc++; 76 } 77 i++; 78 } 79 return cc; 80 } 81 // overloaded operator methods 82 83 // assign a String to a String 84 String & String::operator=(const String & st) 85 { 86 if (this == &st) 87 return *this; 88 delete [] str; 89 len = st.len; 90 str = new char[len + 1]; 91 strcpy(str, st.str); 92 return *this; 93 } 94 95 // assign a C string to a String 96 String & String::operator=(const char * s) 97 { 98 delete [] str; 99 len =strlen(s); 100 str = new char[len + 1]; 101 strcpy(str, s); 102 return *this; 103 } 104 // read-write char access for non-const String 105 char & String::operator[](int i) 106 { 107 return str[i]; 108 } 109 110 // read-only char access for const String 111 const char & String::operator[](int i) const 112 { 113 return str[i]; 114 } 115 116 // overloaded operator friends 117 118 bool operator<(const String &st1, const String &st2) 119 { 120 return (strcmp(st1.str, st2.str) < 0); 121 } 122 123 bool operator>(const String &st1, const String &st2) 124 { 125 return st2.str < st1.str; 126 } 127 128 bool operator==(const String &st1, const String &st2) 129 { 130 return (strcmp(st1.str, st2.str) == 0); 131 } 132 133 // simple String output 134 ostream & operator<<(ostream & os, const String & st) 135 { 136 os << st.str; 137 return os; 138 } 139 140 // quick and dirty String input 141 istream & operator>>(istream & is, String & st) 142 { 143 char temp[String::CINLIM]; 144 is.get(temp, String::CINLIM); 145 if (is) 146 st = temp; 147 while (is && is.get() != '\n') 148 continue; 149 return is; 150 } 151 String operator+(const String &s1, const String &s2)//注意友元函数定义时不要friend,而且不要String:: 152 { 153 String result; 154 result.len = s1.len + s2.len; 155 result.str = new char[result.len+1]; 156 strcpy(result.str, s1.str); 157 strcat(result.str, s2.str); 158 return result; 159 }
1 #include <iostream> 2 #include "string2.h" 3 int main() 4 { 5 using std::cout; 6 using std::cin; 7 String s1(" and I am a C++ student."); 8 String s2 = "Please enter your name: "; 9 String s3; 10 cout << s2; // overloaded << operator 11 cin >> s3; // overloaded >> operator 12 s2 = "My name is " + s3; // overloaded =, + operators 13 cout << s2 << ".\n"; 14 s2 = s2 + s1; 15 s2.stringup(); // converts string to uppercase 16 cout << "The string\n" << s2 << "\ncontains " << s2.has('A') 17 << " 'A' characters in it.\n"; 18 s1 = "red"; // String(const char *), 19 // then String & operator=(const String&) 20 String rgb[3] = { String(s1), String("green"), String("blue")}; 21 cout << "Enter the name of a primary color for mixing light: "; 22 String ans; 23 bool success = false; 24 while (cin >> ans) 25 { 26 ans.stringlow(); // converts string to lowercase 27 for (int i = 0; i < 3; i++) 28 { 29 if (ans == rgb[i]) // overloaded == operator 30 { 31 cout << "That's right!\n"; 32 success = true; 33 break; 34 } 35 } 36 if (success) 37 break; 38 else 39 cout << "Try again!\n"; 40 } 41 cout << "Bye\n"; 42 return 0; 43 }
浙公网安备 33010602011771号