Advertisement
m4ly

class Napis

Feb 8th, 2014
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. class Napis
  2. {
  3. private:
  4.     char *nazwa;
  5.     int dlugosc;
  6.  
  7. public:
  8.     Napis();
  9.     Napis(const char * tab);
  10.     ~Napis();
  11.  
  12.     Napis& operator= (const Napis &);
  13.     Napis& operator+ (const Napis &);
  14.     friend ostream & operator<< (ostream &, const Napis &);
  15. };
  16.  
  17. Napis::Napis()
  18. {
  19.     nazwa = new char[16 + 1];
  20.     dlugosc = 0;
  21.     memset(nazwa, 0, 16 + 1);
  22. }
  23.  
  24. Napis::Napis(const char * tab)
  25. {
  26.     dlugosc = (int)strlen(tab);
  27.     nazwa = new char[dlugosc + 1];
  28.  
  29.     memset(nazwa, 0, dlugosc + 1);
  30.     memcpy(nazwa, tab, dlugosc);
  31.  
  32. }
  33.  
  34. Napis::~Napis()
  35. {
  36.     dlugosc = 0;
  37.     delete[] nazwa;
  38.     nazwa = NULL;
  39. }
  40.  
  41. ostream& operator<<(ostream &out, const Napis &n)
  42. {
  43.     return out << n.nazwa << endl;
  44. }
  45.  
  46.  
  47. Napis& Napis::operator= (const Napis &n1)
  48. {
  49.     dlugosc = n1.dlugosc;
  50.     nazwa = new char[dlugosc + 1];
  51.    
  52.     memset(nazwa, 0, dlugosc + 1);
  53.     memcpy(nazwa, n1.nazwa, dlugosc);
  54.     return *this;
  55. }
  56.  
  57. Napis& Napis::operator+ (const Napis &n1)
  58. {
  59.     int len = n1.dlugosc + dlugosc;
  60.     char *buffer = new char[len + 1];
  61.  
  62.     memset(buffer, 0, len + 1);
  63.     memcpy(buffer, nazwa, dlugosc);
  64.     memcpy(buffer + dlugosc, n1.nazwa, n1.dlugosc);
  65.  
  66.     delete[] nazwa;
  67.     nazwa = NULL;
  68.     nazwa = buffer;
  69.  
  70.     return *this;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement