Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- class String {
- private:
- static int count; // Счетчик созданных объектов строк
- char* str;
- int size;
- public:
- String() : String(80) {}
- explicit String(int size) : size(size) {
- str = new char[size];
- count++;
- }
- String(const char* input) : String(strlen(input)) {
- strcpy(str, input);
- }
- ~String() {
- delete[] str;
- count--;
- }
- static int getCount() {
- return count;
- }
- void input() {
- std::cout << "Enter a string: ";
- std::cin.getline(str, size);
- }
- void display() const {
- std::cout << "String: " << str << std::endl;
- }
- };
- int String::count = 0;
- int main() {
- String str1; // Используется конструктор по умолчанию
- str1.input();
- str1.display();
- String str2(50); // Используется конструктор с указанием размера
- str2.input();
- str2.display();
- String str3("Hello, World!"); // Используется конструктор с инициализацией от пользователя
- str3.display();
- std::cout << "Count of strings: " << String::getCount() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement