Advertisement
istomina_sofia

статические и константные методы

Jun 25th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 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.  
  12.     static int count;  
  13.     const int id;
  14.  
  15. public:
  16.     void set();
  17.     MyString();
  18.     ~MyString();
  19.     void update();
  20.     void print();
  21.     static int getcount();
  22.     int getid() const;
  23. };
  24.  
  25. MyString::MyString() :id(count + 1)
  26. {
  27.     cout << "Вызвался конструктор" << endl;
  28.     m_s = new char[length];
  29.     count++;
  30.  
  31. }
  32.  
  33. MyString::~MyString()
  34. {
  35.     cout << "Вызвался деструктор.   id "<< id << endl;
  36.     count--;
  37.     cout << "Объектов класса " << count << endl;
  38.     delete[] m_s;
  39. }
  40.  
  41.  int MyString::getcount()
  42. {
  43.      cout << "Объектов класса " << count << endl;
  44.      return count;
  45. }
  46.  
  47.  int MyString::getid() const
  48.  {
  49.      cout << endl;
  50.      return id;
  51.  }
  52.  
  53. void MyString::set()
  54. {
  55.     cout << "Введите элементы строки " << endl;
  56.     cin >> m_s;
  57.     L = strlen(m_s);
  58. }
  59.  
  60. void MyString::update()
  61. {
  62.     if (L > 10)
  63.     {
  64.         char* m_s1 = new char[length];
  65.         int k = 0;
  66.         bool match = false;
  67.         const char numbers[11] = "1234567890";
  68.         for (int i = 0; i < L; i++)
  69.         {
  70.             for (int j = 0; j < 11; j++)
  71.                 if (m_s[i] == numbers[j])
  72.                 {
  73.                     match = true;
  74.                     break;
  75.                 }
  76.             if (match != true)
  77.             {
  78.                 m_s1[k] = m_s[i];
  79.                 k++;
  80.             }
  81.             match = false;
  82.         }
  83.         m_s = m_s1;
  84.         L = k;
  85.     }
  86. }
  87.  
  88. void MyString::print()
  89. {
  90.     for (int i = 0; i < L; i++)
  91.     {
  92.         cout << m_s[i] << " ";
  93.     }
  94.     cout << endl;
  95. }
  96.  
  97. int MyString::count = 0;
  98.  
  99. int main()
  100. {
  101.     setlocale(0, "RUS");
  102.     MyString string1;
  103.     string1.getcount();
  104.     string1.set();
  105.     cout << "Введенная строка : ";
  106.     string1.print();
  107.     string1.update();
  108.     cout << "Измененная строка : ";
  109.     string1.print();
  110.     string1.getid();
  111.    
  112.     MyString string2;
  113.     string2.getcount();
  114.     string2.set();
  115.     cout << "Введенная строка : ";
  116.     string2.print();
  117.     string2.update();
  118.     cout << "Измененная строка : ";
  119.     string2.print();
  120.     string2.getid();
  121.    
  122.     MyString string3;
  123.     string3.getcount();
  124.     string3.set();
  125.     cout << "Введенная строка : ";
  126.     string3.print();
  127.     string3.update();
  128.     cout << "Измененная строка : ";
  129.     string3.print();
  130.     string3.getid();
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement