The task is to create a string class. I registered the constructors, overloaded the operations, and the program seems to work fine, but in the last line there is an error of execution "CRT detected I attach the class header file, if necessary I can attach the implementation.
#pragma once #include <iostream> using namespace std; class my_string { private: char *string_chars; size_t len; public: my_string(const char *string_chars); my_string(const size_t len); my_string(const my_string &obj); my_string(); ~my_string() { delete[] this->string_chars; } my_string operator+(const my_string &b) const; my_string operator+(const char *b) const; my_string &operator=(const my_string &b); my_string &operator=(const char *b); my_string &operator+=(const my_string &b); my_string &operator+=(const char *b); bool operator<(const my_string &b) const; bool operator<(const char *b) const; bool operator>(const my_string &b) const; bool operator>(const char *b) const; bool operator==(const my_string &b) const; bool operator==(const char *b) const; bool operator!=(const my_string &b) const; bool operator!=(const char *b) const; void print() const { cout << this->string_chars << endl; } const char *get_str() const { return this->string_chars; } size_t get_len() const { return this->len; } size_t first_sym(const char a) const; }; UPD: Attached implementation
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> #include "String.h" using namespace std; my_string::my_string(const char *string_chars) { this->len = strlen(string_chars); this->string_chars = new char[this->len + 1]; strcpy(this->string_chars, string_chars); this->string_chars[this->len] = 0; } my_string::my_string(const size_t len) { this->len = len; this->string_chars = new char[this->len + 1]; for (size_t i = 0; i <= len; i++) { this->string_chars[i] = 0; } } my_string::my_string(const my_string &obj) { strcpy(this->string_chars, obj.get_str()); this->len = obj.get_len(); } my_string::my_string() { this->len = 0; this->string_chars = new char[2]; for (int i = 0; i < 2; i++) { this->string_chars[i] = 0; } } my_string my_string::operator+(const my_string &b) const { size_t len = this->len + b.get_len(); char *result = new char[len + 1]; strcpy(result, this->string_chars); strcat(result, b.string_chars); result[len] = 0; my_string *res = new my_string(result); delete[] result; return *res; } my_string my_string::operator+(const char *b) const { size_t len = this->len + strlen(b); char *result = new char[len + 1]; strcpy(result, this->string_chars); strcat(result, b); result[len] = 0; my_string *res = new my_string(result); delete[] result; return *res; } my_string &my_string::operator=(const my_string &b) { if (&b != this) { this->len = b.get_len(); strcpy(this->string_chars, b.string_chars); } return *this; } my_string &my_string::operator=(const char *b) { if (b != this->string_chars) { this->len = strlen(b); strcpy(this->string_chars, b); } return *this; } my_string &my_string::operator+=(const my_string &b) { this->len += b.get_len(); strcat(this->string_chars, b.string_chars); return *this; } my_string &my_string::operator+=(const char *b) { this->len += strlen(b); strcat(this->string_chars, b); return *this; } bool my_string::operator<(const my_string &b) const { if (strcmp(b.string_chars, this->string_chars)) { return true; } else { return false; } } bool my_string::operator<(const char *b) const { if (strcmp(b, this->string_chars)) { return true; } else { return false; } } bool my_string::operator>(const my_string &b) const { if (strcmp(this->string_chars, b.string_chars)) { return true; } else { return false; } } bool my_string::operator>(const char *b) const { if (strcmp(this->string_chars, b)) { return true; } else { return false; } } bool my_string::operator==(const my_string &b) const { if (!strcmp(this->string_chars, b.string_chars) && this->len == b.len) { return true; } else { return false; } } bool my_string::operator==(const char *b) const { if (!strcmp(this->string_chars, b) && this->len == strlen(b)) { return true; } else { return false; } } bool my_string::operator!=(const my_string &b) const { if (abs(strcmp(this->string_chars, b.string_chars)) || this->len != b.len) { return true; } else { return false; } } bool my_string::operator!=(const char *b) const { if (abs(strcmp(this->string_chars, b)) || this->len != strlen(b)) { return true; } else { return false; } } size_t my_string::first_sym(const char a) const { char *sym = strchr(this->string_chars, a); if (sym != nullptr) { return sym - this->string_chars; } return -1; }