Advertisement
Berty97

Untitled

Jan 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Sir
  5. {
  6. private:
  7. char *s;
  8.  
  9. public:
  10.  
  11. Sir();
  12. Sir(int);
  13. Sir(char *);
  14. Sir(const char*);
  15. Sir(const Sir&);
  16. ~Sir();
  17.  
  18. Sir operator+(Sir&);
  19. Sir operator+(const char*);
  20. Sir& operator=(Sir& x);
  21. Sir& operator+=(Sir& x);
  22. Sir& operator+=(const char*);
  23.  
  24. char operator[](int i);
  25.  
  26. operator char*();
  27.  
  28. friend std::ostream& operator<<(std::ostream& out, Sir& s);
  29.  
  30. int Length();
  31.  
  32. void Afisare();
  33. };
  34.  
  35. /* Constructor implicit */
  36. Sir::Sir()
  37. {
  38. s = NULL;
  39. }
  40.  
  41. /* Copy constructor */
  42. Sir::Sir(const Sir& x)
  43. {
  44. if (x.s)
  45. {
  46. this->s = new char[strlen(x.s) + 1];
  47. strcpy(s, x.s);
  48. }
  49. else
  50. s = NULL;
  51. }
  52.  
  53. /* Constructori de initializare*/
  54. Sir::Sir(int l)
  55. {
  56. s = new char[l + 1]();
  57. }
  58.  
  59. Sir::Sir(char* x)
  60. {
  61. s = NULL;
  62. if (x)
  63. {
  64. s = new char[strlen(x) + 1];
  65. strcpy(s, x);
  66. }
  67. }
  68.  
  69. Sir::Sir(const char* x) {
  70.  
  71. s = NULL;
  72. if (x) {
  73. s = new char[strlen(x) + 1];
  74. strcpy(s, x);
  75. }
  76. }
  77.  
  78. /* Destructor */
  79. Sir::~Sir()
  80. {
  81. if (s)
  82. delete[] s;
  83. }
  84.  
  85.  
  86. /* Metoda pentru lungime */
  87. int Sir::Length()
  88. {
  89. if (s)
  90. return strlen(s);
  91. else
  92. return 0;
  93. }
  94.  
  95. /* Metoda pentru afisare */
  96. void Sir::Afisare()
  97. {
  98. cout << s << " ";
  99. }
  100.  
  101.  
  102. Sir Sir::operator+(Sir& x)
  103. {
  104. Sir a(this->Length() + x.Length());
  105.  
  106. if (s)
  107. strcpy(a.s, s);
  108. if (x.s)
  109. strcat(a.s, x.s);
  110. return a;
  111. }
  112.  
  113. Sir Sir::operator+(const char* add)
  114. {
  115. Sir other(add);
  116.  
  117. return *this + other;
  118.  
  119.  
  120. }
  121.  
  122. Sir& Sir::operator+=(Sir& x)
  123. {
  124. if (s)
  125. if (x.s)
  126. {
  127. char *temp = new char[strlen(s) + 1];
  128. strcpy(temp, s);
  129. delete[] s;
  130. s = new char[strlen(temp) + strlen(x.s) + 1];
  131. strcpy(s, temp);
  132. strcat(s, x.s);
  133. delete[] temp;
  134. return *this;
  135. }
  136. else
  137. return *this;
  138. else
  139. {
  140. if (x.s)
  141. {
  142.  
  143. s = new char[strlen(x.s) + 1];
  144. strcpy(s, x.s);
  145. return *this;
  146. }
  147. else
  148. return *this;
  149. }
  150. }
  151.  
  152. Sir& Sir::operator+=(const char* x) {
  153. Sir x_s(x);
  154. *this += x_s;
  155. return *this;
  156. }
  157.  
  158. Sir& Sir::operator=(Sir& x)
  159. {
  160. if (s)
  161. delete[] s;
  162. if (x.s)
  163. {
  164. s = new char[strlen(x.s) + 1];
  165. strcpy(s, x.s);
  166. }
  167. return *this;
  168. }
  169.  
  170.  
  171. /* poti scrie (char*)x, cu x Sir, fara sa comenteze compilatorul */
  172. Sir::operator char *()
  173. {
  174. return s;
  175. }
  176.  
  177.  
  178. /* afisare, poti scrie cout << x , cu x de tip Sir */
  179. std::ostream& operator<<(std::ostream& out, Sir& s) {
  180. out << s.s << " ";
  181. return out;
  182. }
  183.  
  184.  
  185. /* poti sa scrii x[2], ca la string sau array de char-uri si o sa-ti dea litera respectiva
  186. char Sir::operator[](int i) {
  187. if (s)
  188. return s[i];
  189. else
  190. return NULL;
  191. }*/
  192.  
  193. int main()
  194. {
  195. Sir a("abc"); /* constructor de initializare */
  196. Sir b(3); /* constructor de lungime */
  197. Sir d(6);
  198. Sir c(a); /* copyconstructor */
  199. Sir aux(" This");
  200. Sir aux2(" is");
  201. Sir test = aux; /* = */
  202. test = test + aux2; /* +(Sir)*/
  203. test = test + " very"; /*+(const char*) */
  204. test += " cool, "; /* +=(const char*) */
  205. test += Sir("they all work\n"); /* +=(Sir) */
  206. std::cout << test; /* << */
  207. std::cout << "Al doilea caracter este " << ((char*)test)[2] << "\n"; /* operator [] */
  208.  
  209. system("pause");
  210. return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement