string1.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // string1.cpp -- String class methods
  2. #include <cstring> // string.h for some
  3. #include "string1.h" // includes <iostream>
  4. using std::cin;
  5. using std::cout;
  6. // initializing static class member
  7. int String::num_strings = 0;
  8. // static method
  9. int String::HowMany()
  10. {
  11. return num_strings;
  12. }
  13. // class methods
  14. String::String(const char * s) // construct String from C string
  15. {
  16. len = std::strlen(s); // set size
  17. str = new char[len + 1]; // allot storage
  18. std::strcpy(str, s); // initialize pointer
  19. num_strings++; // set object count
  20. }
  21. String::String() // default constructor
  22. {
  23. len = 4;
  24. str = new char[1];
  25. str[0] = '\0'; // default string
  26. num_strings++;
  27. }
  28. String::String(const String & st)
  29. {
  30. num_strings++; // handle static member update
  31. len = st.len; // same length
  32. str = new char [len + 1]; // allot space
  33. std::strcpy(str, st.str); // copy string to new location
  34. }
  35. String::~String() // necessary destructor
  36. {
  37. --num_strings; // required
  38. delete [] str; // required
  39. }
  40. // overloaded operator methods
  41. // assign a String to a String
  42. String & String::operator=(const String & st)
  43. {
  44. if (this == &st)
  45. return *this;
  46. delete [] str;
  47. len = st.len;
  48. str = new char[len + 1];
  49. std::strcpy(str, st.str);
  50. return *this;
  51. }
  52. // assign a C string to a String
  53. String & String::operator=(const char * s)
  54. {
  55. delete [] str;
  56. len = std::strlen(s);
  57. str = new char[len + 1];
  58. std::strcpy(str, s);
  59. return *this;
  60. }
  61. // read-write char access for non-const String
  62. char & String::operator[](int i)
  63. {
  64. return str[i];
  65. }
  66. // read-only char access for const String
  67. const char & String::operator[](int i) const
  68. {
  69. return str[i];
  70. }
  71. // overloaded operator friends
  72. bool operator<(const String &st1, const String &st2)
  73. {
  74. return (std::strcmp(st1.str, st2.str) < 0);
  75. }
  76. bool operator>(const String &st1, const String &st2)
  77. {
  78. return st2 < st1;
  79. }
  80. bool operator==(const String &st1, const String &st2)
  81. {
  82. return (std::strcmp(st1.str, st2.str) == 0);
  83. }
  84. // simple String output
  85. ostream & operator<<(ostream & os, const String & st)
  86. {
  87. os << st.str;
  88. return os;
  89. }
  90. // quick and dirty String input
  91. istream & operator>>(istream & is, String & st)
  92. {
  93. char temp[String::CINLIM];
  94. is.get(temp, String::CINLIM);
  95. if (is)
  96. st = temp;
  97. while (is && is.get() != '\n')
  98. continue;
  99. return is;
  100. }