Advertisement
plarmi

hwcpp_1_3

Jun 14th, 2023
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. class String {
  5. private:
  6.     static int count; // Счетчик созданных объектов строк
  7.     char* str;
  8.     int size;
  9.  
  10. public:
  11.     String() : String(80) {}
  12.  
  13.     explicit String(int size) : size(size) {
  14.         str = new char[size];
  15.         count++;
  16.     }
  17.  
  18.     String(const char* input) : String(strlen(input)) {
  19.         strcpy(str, input);
  20.     }
  21.  
  22.     ~String() {
  23.         delete[] str;
  24.         count--;
  25.     }
  26.  
  27.     static int getCount() {
  28.         return count;
  29.     }
  30.  
  31.     void input() {
  32.         std::cout << "Enter a string: ";
  33.         std::cin.getline(str, size);
  34.     }
  35.  
  36.     void display() const {
  37.         std::cout << "String: " << str << std::endl;
  38.     }
  39. };
  40.  
  41. int String::count = 0;
  42.  
  43. int main() {
  44.     String str1; // Используется конструктор по умолчанию
  45.     str1.input();
  46.     str1.display();
  47.  
  48.     String str2(50); // Используется конструктор с указанием размера
  49.     str2.input();
  50.     str2.display();
  51.  
  52.     String str3("Hello, World!"); // Используется конструктор с инициализацией от пользователя
  53.     str3.display();
  54.  
  55.     std::cout << "Count of strings: " << String::getCount() << std::endl;
  56.  
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement