Advertisement
istomina_sofia

класс конструктор деструктор массив

Jun 25th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. class MyString
  6. {
  7. private:
  8.     char* m_s;
  9.     int L;
  10.     const int length = 50;
  11. public:
  12.     void set();
  13.     MyString();
  14.     ~MyString();
  15.     void update();
  16.     void print();
  17. };
  18.  
  19. MyString::MyString()
  20. {
  21.     cout << "Вызвался конструктор" << endl;
  22.     m_s = new char[length];
  23. }
  24.  
  25. MyString::~MyString()
  26. {
  27.     cout << "Вызвался деструктор" << endl;
  28.     delete[] m_s;
  29. }
  30.  
  31. void MyString::set()
  32. {
  33.     cout << "Введите элементы строки " << endl;
  34.     cin >> m_s;
  35.     L = strlen(m_s);
  36. }
  37.  
  38. void MyString::update()
  39. {
  40.     if (L > 10)
  41.     {
  42.         char* m_s1 = new char[length];
  43.         int k = 0;
  44.         bool match = false;
  45.         const char numbers[11] = "1234567890";
  46.         for (int i = 0; i < L; i++)
  47.         {
  48.             for (int j = 0; j < 11; j++)
  49.                 if (m_s[i] == numbers[j])
  50.                 {
  51.                     match = true;
  52.                     break;
  53.                }
  54.                 if (match != true)
  55.                 {
  56.                     m_s1[k] = m_s[i];
  57.                     k++;
  58.                 }
  59.                 match = false;
  60.         }
  61.         m_s = m_s1;
  62.         L = k;
  63.     }
  64. }
  65.  
  66. void MyString::print()
  67. {
  68.     for (int i = 0; i < L; i++)
  69.     {
  70.         cout << m_s[i] << " ";
  71.     }
  72.     cout << endl;
  73. }
  74.  
  75. int main()
  76. {
  77.     setlocale(0, "RUS");
  78.     MyString string;
  79.     string.set();
  80.     cout << "Введенная строка : ";
  81.     string.print();
  82.     string.update();
  83.     cout << "Измененная строка : ";
  84.     string.print();
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement