Advertisement
Berty97

Untitled

Jan 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Sir
  6. {
  7. protected:
  8. char* s;
  9.  
  10.  
  11. public:
  12. Sir();
  13. Sir(char* nume);
  14. ~Sir();
  15. void Afisare();
  16. Sir operator+=(Sir& td);
  17. Sir& operator=( Sir& td);
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. };
  25. Sir::Sir()
  26. {
  27. s = NULL;
  28. }
  29. Sir::Sir(char* s_nou)
  30. {
  31. s = new char[strlen(s_nou) + 1];
  32. strcpy(s, s_nou);
  33. }
  34. Sir::~Sir()
  35. {
  36. /* if(s)
  37. {
  38. delete [] s;
  39. }*/
  40. }
  41. void Sir::Afisare()
  42. {
  43. cout << s << endl;
  44. cout << endl;
  45. }
  46.  
  47. Sir Sir::operator+=(Sir& td)
  48. {
  49. Sir rez;
  50.  
  51. rez.s = new char[strlen(s)+strlen(td.s) + 1];
  52. strcpy(rez.s,s);
  53. strcat(rez.s,td.s);
  54.  
  55.  
  56. delete []s;
  57. s=new char[strlen(rez.s)+1];
  58. strcpy(s,rez.s);
  59.  
  60. return s;
  61.  
  62.  
  63. }
  64. Sir& Sir::operator=(Sir& td)
  65. {
  66.  
  67. if (this == &td)
  68. return *this;
  69. else
  70. {
  71. if (s)
  72. {
  73. delete[]s;
  74. }
  75. if (td.s)
  76. {
  77. s = new char[strlen(td.s) + 1];
  78. strcpy(s, td.s);
  79. }
  80.  
  81. return *this;
  82. }
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. class SirNr :public Sir
  90. {
  91. public:
  92. SirNr():Sir(){}
  93. SirNr(char* nume):Sir(nume)
  94. {
  95. Numere(s);
  96. }
  97. SirNr& operator+=(SirNr& td)
  98. {
  99.  
  100. Sir::operator+=(td);
  101. return *this;
  102.  
  103.  
  104. }
  105. SirNr& operator=(SirNr& td)
  106. {
  107. Sir::operator=(td);
  108. return *this;
  109. }
  110.  
  111. void Numere(char *(&nr))
  112. {
  113. char *rez;
  114. int a=0,b=0;
  115. for(int i=0;i<strlen(nr);i++)
  116. {
  117. if(int(nr[i])>=48 && int(nr[i])<=57)
  118. a++;
  119. }
  120. rez=new char[a+1];
  121. for(int i=0;i<strlen(nr);i++)
  122. {
  123. if(int(nr[i])>=48 && int(nr[i])<=57)
  124. {
  125. rez[b]=nr[i];
  126. b++;
  127. }
  128. }
  129. rez[a]=NULL;
  130. nr=new char[a+1];
  131. strcpy(nr,rez);
  132. delete[] rez;
  133.  
  134. }
  135. };
  136.  
  137.  
  138.  
  139. int main()
  140. {
  141.  
  142. SirNr A("masina22"),B("me1312"),C;
  143. A.Afisare();
  144. B.Afisare();
  145. A+=B;
  146. A.Afisare();
  147.  
  148. system("pause");
  149. return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement