fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <stdio.h>
  4.  
  5. class String {
  6. public:
  7. String();
  8. String(const String &s);
  9. String(const char *s);
  10. ~String();
  11.  
  12. int Size() const;
  13.  
  14. char &At(int i);
  15. char At(int i) const;
  16. char &operator[](int i);
  17. char operator[](int i) const;
  18. String &operator=(const String &rhs);
  19.  
  20. private:
  21. int size_;
  22. char *data_;
  23. };
  24.  
  25. String::String() {
  26. size_ = 0;
  27. data_ = NULL;
  28. }
  29.  
  30. String::String(const char *s) {
  31. int len = 0;
  32. while (s[len] != '\0') ++len;
  33. size_ = len;
  34. data_ = new char[len];
  35. for (int i = 0; i < len; ++i) data_[i] = s[i];
  36. }
  37.  
  38. String::~String() {
  39. delete data_;
  40. }
  41.  
  42. String::String(const String &s) {
  43. size_ = s.size_;
  44. data_ = new char[size_];
  45. for (int i = 0; i < size_; i++) data_[i] = s[i];
  46. }
  47.  
  48. int String::Size() const {
  49. return size_;
  50. }
  51.  
  52. char &String::At(int i) {
  53. return data_[i];
  54. }
  55.  
  56. char String::At(int i) const {
  57. return data_[i];
  58. }
  59.  
  60. char String::operator[](int i) const {
  61. return At(i);
  62. }
  63.  
  64. char &String::operator[](int i) {
  65. return At(i);
  66. }
  67.  
  68. std::ostream &operator<<(std::ostream &s, const String &str) {
  69. for (int i = 0; i < str.Size(); ++i) s << str[i];
  70. return s;
  71. }
  72.  
  73. String operator+(const String &lhs, const String &rhs) {
  74. int new_size_ = lhs.Size() + rhs.Size();
  75. char tmp[new_size_]; //這行要加1嗎?
  76. for(int i = 0; i < lhs.Size(); i++){
  77. tmp[i] = lhs[i];
  78. }
  79. for(int i = 0; i < rhs.Size(); i++){
  80. tmp[i+lhs.Size()] = rhs[i];
  81. }
  82. tmp[new_size_] = '\0';
  83. String conString(tmp);
  84. return conString;
  85. }
  86.  
  87. String &String::operator=(const String &rhs) {
  88. this->size_ = rhs.Size();
  89. data_ = new char[rhs.Size()];
  90. for (int i = 0; i < rhs.Size(); i++) {
  91. data_[i] = rhs[i];
  92. }
  93. String &refString = *this;
  94. return refString;
  95. }
  96.  
  97. int main() {
  98. using namespace std;
  99. String a, b;
  100. cout << "a.Size(): " << a.Size() << endl;
  101. a = b = "Hello";
  102. cout << "a: " << a << endl;
  103. cout << "b: " << b << endl;
  104.  
  105. String c = "world";
  106. cout << "c: " << c << endl;
  107.  
  108. String d = a;
  109. cout << "d: " << d << endl;
  110.  
  111. a[0] = 'h';
  112. cout << "a: " << a << endl;
  113. cout << "b: " << b << endl;
  114. cout << "d: " << d << endl;
  115.  
  116. String e = a + " " + b + "!";
  117. cout << "e: " << e << endl;
  118.  
  119. system("pause");
  120. return 0;
  121. }
  122.  
Success #stdin #stdout #stderr 0s 3476KB
stdin
Standard input is empty
stdout
a.Size(): 0
a: Hello
b: Hello
c: world
d: Hello
a: hello
b: Hello
d: Hello
e: hello Hello!
stderr
sh: pause: not found