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. printf("Lsize:%d Rsize:%d new_size_:%d\n",lhs.Size(),rhs.Size(),new_size_);
  76. char tmp[new_size_];
  77. for(int i = 0; i < lhs.Size(); i++){
  78. tmp[i] = lhs[i];
  79. }
  80. for(int i = 0; i < rhs.Size(); i++){
  81. tmp[i+lhs.Size()] = rhs[i];
  82. }
  83. String conString(tmp);
  84. printf("\n");
  85. return conString;
  86. }
  87.  
  88. String &String::operator=(const String &rhs) {
  89. this->size_ = rhs.Size();
  90. data_ = new char[rhs.Size()];
  91. for (int i = 0; i < rhs.Size(); i++) {
  92. data_[i] = rhs[i];
  93. }
  94. String &refString = *this;
  95. return refString;
  96. }
  97.  
  98. int main() {
  99. using namespace std;
  100. String a, b;
  101. cout << "a.Size(): " << a.Size() << endl;
  102. a = b = "Hello";
  103. cout << "a: " << a << endl;
  104. cout << "b: " << b << endl;
  105.  
  106. String c = "world";
  107. cout << "c: " << c << endl;
  108.  
  109. String d = a;
  110. cout << "d: " << d << endl;
  111.  
  112. a[0] = 'h';
  113. cout << "a: " << a << endl;
  114. cout << "b: " << b << endl;
  115. cout << "d: " << d << endl;
  116.  
  117. String e = a + " " + b + "!";//習題,這邊就開始出錯
  118. cout << "e: " << e << endl;
  119.  
  120. String p = a + b + a + b + a + b + a; //for debug
  121. cout << "p: " << p << endl; //for debug
  122.  
  123. system("pause");
  124. return 0;
  125. }
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
Lsize:5  Rsize:1  new_size_:6

Lsize:6  Rsize:5  new_size_:11

Lsize:11  Rsize:1  new_size_:12

e: hello Hello!
Lsize:5  Rsize:5  new_size_:10

Lsize:10  Rsize:5  new_size_:15

Lsize:15  Rsize:5  new_size_:20

Lsize:21  Rsize:5  new_size_:26

Lsize:26  Rsize:5  new_size_:31

Lsize:31  Rsize:5  new_size_:36

p: helloHellohelloHellohelloHellohello
stderr
sh: pause: not found