fork download
  1. #include<cstring>
  2. #include<iostream>
  3.  
  4. class stringb
  5. {
  6.  
  7. public:
  8. stringb() :
  9. p (new char[25]), len(25)
  10. {
  11. }
  12. stringb(char* s) :
  13. p (new char[std::strlen(s)])
  14. {
  15. len = std::strlen(s);
  16. std::strcpy (p, s );
  17. }
  18.  
  19. stringb (const stringb& other) :
  20. p (new char[std::strlen (other.p) + 1]), len(std::strlen (other.p))
  21. {
  22. std::strcpy (p, other.p );
  23. }
  24.  
  25. /** Move Constructor Since C++11*/
  26. stringb (stringb&& other) :
  27. p (other.p),len(0)
  28. {
  29. other.p = nullptr;
  30. }
  31.  
  32. /** Destructor */
  33. ~stringb()
  34. {
  35. delete[] p;
  36. }
  37.  
  38. /** Copy Assignment Operator */
  39. stringb& operator= (const stringb& other)
  40. {
  41. stringb temporary (other);
  42. std::swap (p, temporary.p);
  43. return *this;
  44. }
  45.  
  46. /** Move Assignment Operator Since C++11*/
  47. stringb& operator= (stringb&& other)
  48. {
  49. std::swap (p, other.p);
  50. return *this;
  51. }
  52.  
  53. private:
  54. char* p;
  55. std::size_t len;
  56. friend std::ostream& operator<< (std::ostream& os, const stringb& stringb)
  57. {
  58. os << stringb.p;
  59. return os;
  60. }
  61.  
  62.  
  63. friend stringb operator+(const stringb &a,const stringb &b)
  64. {
  65. stringb temp;
  66. temp.len = a.len + b.len;
  67. temp.p=new char[(temp.len)+1];
  68. std::strcpy(temp.p, a.p);
  69. std::strcat(temp.p, b.p);
  70. return (temp);
  71. }
  72.  
  73. /* Not sure what this will do */
  74. friend stringb operator-(const stringb &a,const stringb &b)
  75. {
  76. stringb temp;
  77. temp.len=a.len-b.len;
  78. temp.p=new char[temp.len+1];
  79. std::strcpy(temp.p,a.p);
  80. return (temp);
  81. }
  82. };
  83.  
  84.  
  85.  
  86. int main()
  87. {
  88. stringb a("POW");
  89. stringb b("JACK");
  90.  
  91. stringb c;
  92. c=a+b;
  93. std::cout<< c<< ","<<a<<","<<b;
  94. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
POWJACK,POW,JACK