Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cstring>
- using namespace std;
- class String {
- public:
- String() : size(0), capacity(15), str(new char[15]) {
- total_number_strings++;
- }
- String(const int string_capacity) : size(0), capacity(string_capacity), str(new char[string_capacity]) {
- total_number_strings++;
- }
- String(const char new_str[]) {
- capacity = strlen(new_str) + 1;
- str = new char[capacity];
- strcpy(str, new_str);
- size = capacity - 1;
- total_number_strings++;
- }
- String(const String& other) : size(other.size), capacity(other.capacity), str(new char[capacity]) {
- strcpy(str, other.str);
- total_number_strings++;
- }
- String& operator=(const String& other) {
- if (&other != this) {
- size = other.size;
- capacity = other.capacity;
- delete[] str;
- str = new char[capacity];
- strcpy(str, other.str);
- }
- return *this;
- } // перегрузка оператора присваивания =
- String(String&& other) : size(other.size), capacity(other.capacity), str(other.str) {
- other.str = nullptr;
- other.size = 0;
- other.capacity = 0;
- total_number_strings++;
- }
- String& operator=(String&& other) {
- if (&other != this) {
- size = other.size;
- capacity = other.capacity;
- delete[] str;
- str = other.str;
- other.str = nullptr;
- other.size = 0;
- other.capacity = 0;
- }
- return *this;
- } // перегрузка оператора присваивания =
- char& operator[] (const int index) {
- if (index < 0 || index >= size) {
- cout << "Invalid index" << endl;
- abort;
- }
- return str[index];
- } // перегрузка оператора индексирования (сеттер)
- char operator[] (const int index) const {
- if (index < 0 || index >= size) {
- cout << "Invalid index" << endl;
- abort;
- }
- return str[index];
- } // перегрузка оператора индексирования для константных объектов
- bool operator==(const String& right) const {
- if (right.size != size) {
- return false;
- }
- for (int i = 0; i < size; i++) {
- if (str[i] != right.str[i])
- return false;
- }
- return true;
- }
- bool operator!=(const String& right) const {
- return !(*this == right);
- }
- int Size() const {
- return size;
- }
- int Capacity() const {
- return capacity;
- }
- static int GetTotalNumberStrings() {
- return total_number_strings;
- }
- ~String() {
- delete[] str;
- total_number_strings--;
- }
- private:
- int size;
- int capacity;
- char* str;
- static int total_number_strings;
- };
- ostream& operator<<(ostream& output, const String& string) {
- for (int i = 0; i < string.Size(); i++) {
- output << string[i];
- }
- return output;
- } // перегрузка оператора вывода в поток <<
- istream& operator>>(istream& input, String& string) {
- int capacity = 15;
- int i = 0;
- char* str = new char[capacity];
- char ch;
- while (ch = input.get()) {
- if (ch == ' ' || ch == '\n') {
- break;
- }
- if (i == capacity - 1) {
- char* temp = new char[capacity *= 2];
- for (int j = 0; j <= i; j++) {
- temp[j] = str[j];
- }
- delete[] str;
- str = temp;
- }
- str[i] = ch;
- i++;
- }
- if (i > 0) {
- str[i] = '\0';
- string = move(String(str));
- }
- return input;
- } // перегрузка оператора ввода из потока >>
- int String::total_number_strings = 0;
- int main() {
- String my_string;
- cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
- my_string = "Hello";
- cout << my_string << endl;
- cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
- my_string = "Hello my dear friend";
- cout << my_string << endl;
- cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
- my_string[2] = 'm';
- cout << my_string << endl;
- cout << my_string[1] << endl;
- String my_string2("1234");
- cout << my_string2 << endl;
- cout << "Size: " << my_string2.Size() << " Capacity: " << my_string2.Capacity() << endl;
- const String my_string3(my_string);
- cout << my_string3[1] << endl;
- cout << my_string3 << endl;
- cout << String::GetTotalNumberStrings() << endl;
- cout << (my_string == my_string2) << endl;
- cout << (my_string != my_string2) << endl;
- String my_string4;
- my_string4 = my_string;
- cout << my_string4 << endl;
- my_string4 = "Hello";
- cout << my_string4 << endl;
- String my_string5;
- cin >> my_string5;
- cout << my_string5 << endl;
- String my_string6(move(my_string4));
- cout << my_string6 << endl;
- cout << my_string4 << "clear" << endl;
- my_string4 = move(my_string);
- cout << my_string4 << endl;
- cout << my_string << "clear" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement