Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #ifndef CIAG_H
  2. #define CIAG_H
  3.  
  4. #include <iostream>
  5. #include <string.h>
  6.  
  7. class Ciag
  8. {
  9. public:
  10. Ciag();
  11. Ciag(char* arr);
  12. Ciag(const Ciag& obj);
  13. ~Ciag();
  14. int dl(){ return lengthCiag; }
  15. int ile() { return counter; }
  16. char toCharArray();
  17. void wyswietl();
  18. Ciag& operator = (const Ciag& c);
  19. Ciag& operator +=(Ciag& c);
  20. private:
  21. std::size_t lengthCiag;
  22. char* ptrCiag;
  23. static std::size_t counter;
  24. };
  25.  
  26. #endif // CIAG_H
  27.  
  28.  
  29.  
  30. //plik cpp plik cpp plik cpp
  31.  
  32.  
  33.  
  34. #include "ciag.h"
  35.  
  36. std::size_t Ciag::counter = 0;
  37.  
  38. Ciag::Ciag()
  39. {
  40. ptrCiag = nullptr;
  41. lengthCiag = 0;
  42. counter++;
  43. }
  44.  
  45. Ciag::Ciag(char* arr)
  46. {
  47. lengthCiag = strlen(arr);
  48. ptrCiag = new char[lengthCiag + 1];
  49. strcpy(ptrCiag, arr);
  50. counter++;
  51. }
  52.  
  53. Ciag::Ciag(const Ciag& obj)
  54. {
  55. lengthCiag = obj.lengthCiag;
  56. ptrCiag = new char[ lengthCiag + 1];
  57. strcpy(ptrCiag, obj.ptrCiag);
  58. }
  59.  
  60. Ciag::~Ciag()
  61. {
  62. delete [] ptrCiag;
  63. ptrCiag = nullptr;
  64. counter--;
  65. }
  66.  
  67. char Ciag::toCharArray()
  68. {
  69. return *ptrCiag;
  70. }
  71.  
  72. Ciag& Ciag::operator =(const Ciag& c)
  73. {
  74. if(this != &c)
  75. {
  76. if(c.lengthCiag != lengthCiag)
  77. {
  78. delete [] ptrCiag;
  79. lengthCiag = 0;
  80. ptrCiag = nullptr;
  81. ptrCiag = new char[ c.lengthCiag + 1];
  82. lengthCiag = c.lengthCiag;
  83. }
  84. std::copy(c.ptrCiag, c.ptrCiag + c.lengthCiag, ptrCiag);
  85. }
  86. return *this;
  87. }
  88.  
  89. Ciag& Ciag::operator +=(Ciag& c)
  90. {
  91. Ciag temp;
  92. delete [] temp.ptrCiag;
  93. temp.ptrCiag = nullptr;
  94. temp.ptrCiag = new char[ lengthCiag + c.lengthCiag + 1];
  95. temp.lengthCiag = lengthCiag + c.lengthCiag;
  96. temp.ptrCiag = c.ptrCiag;
  97. strcat(temp.ptrCiag, c.ptrCiag);
  98.  
  99. return *this;
  100. }
  101.  
  102. void Ciag::wyswietl()
  103. {
  104. std::cout << ptrCiag << "\n";
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. //main main main
  116.  
  117. #include <iostream>
  118. #include "ciag.h"
  119. using namespace std;
  120.  
  121. int main()
  122. {
  123. Ciag c1, c2, c3, c4;
  124. c1 = "Hejka";
  125. c3 = "lel";
  126. c4 = "nom";
  127. c2 = c1;
  128. c4 += c3;
  129. c1.wyswietl();
  130. c2.wyswietl();
  131. c4.wyswietl();
  132. cout << c4.dl();
  133. return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement