Advertisement
thevals

para3

Mar 18th, 2022
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <malloc.h>
  3. using namespace std;
  4. class stroka{
  5. protected:
  6.     int len;
  7.     char* pCh;
  8. public:
  9.     stroka(int = 0);
  10.     stroka(char);
  11.     stroka(const char*);
  12.     stroka(const stroka&);
  13.     ~stroka();
  14.     char* GetStr(void) const {
  15.         return pCh;
  16.     }
  17.     int GetLen(void) const {
  18.         return len;
  19.     }
  20.     void Show(void);
  21. };
  22. stroka::stroka(int val) :len(val), pCh(new char[len + 1]) {
  23.     len = val;
  24.     if (val == 0) pCh[0] = '\0';
  25.     else pCh = new char[len + 1];
  26.     cout << "stroka::stroka(int val)" << endl;
  27. }
  28. stroka::stroka(char ch) :len(1), pCh(new char[len + 1])
  29. {
  30.     pCh[0] = ch;
  31.     pCh[1] = '\0';
  32.     cout << "stroka::stroka(char ch)" << endl;
  33. }
  34.  
  35. stroka::stroka(const char* S) :len(0), pCh()
  36. {
  37.     //strcpy_s(pCh, len + 1, S);
  38.     const char *cptr = S;
  39.     while (*(cptr++) != '\0') len++;
  40.     pCh = new char[len + 1];
  41.     char *ptr1 = pCh;
  42.     const char *ptr2 = S;
  43.     for (int i = 0; i <= len; i++) {
  44.         *ptr1 = *ptr2;
  45.         ptr1++;
  46.         ptr2++;
  47.     }
  48.     cout << "stroka::stroka(const char* S)" << endl;
  49. }
  50. stroka::stroka(const stroka& from) :len(0), pCh()
  51. {
  52.     const char* cptr = from.pCh;
  53.     while (*(cptr++) != '\0') len++;
  54.     pCh = new char[len + 1];
  55.     char* ptr1 = pCh;
  56.     const char* ptr2 = from.pCh;
  57.     for (int i = 0; i <= len; i++) {
  58.         *ptr1 = *ptr2;
  59.         ptr1++;
  60.         ptr2++;
  61.     }
  62.     cout << "stroka::stroka(const stroka& from)" << endl;
  63. }
  64. stroka::~stroka()
  65. {
  66.     if (pCh)
  67.         delete[] pCh;
  68.     cout << "stroka::~stroka()" << endl;
  69. }
  70. void stroka::Show(void)
  71. {
  72.     cout << "pCh=" << pCh << endl;
  73.     cout << "len=" << len << endl;
  74. }
  75.  
  76. int main()
  77. {
  78.     stroka obj("FBIT");
  79.     stroka obj1 = obj;
  80.     obj.Show();
  81.     obj1.Show();
  82.     stroka obj2('F');
  83.     obj2.Show();
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement