Need the help of smart people. Necessary: ​​Create a string class that implements the text string. That is, it stores a chain of characters of variable length, allows you to determine its length, supports the operation of concatenation and comparison of two strings. Create examples of use and a set of tests. (Task for the option: Create a class EIGHTFLOW.Add the ability to fill the value of a string from a number in the binary system). The task was done, but the teacher needs to add an overload to the assignment operator and somehow make the check. The program code is attached. The main file is PROGA.cpp, the additional file is String.h. PROGA.cpp file:

#include "String.h" #include <iostream> #include <conio.h> #include <cstring> #include <cstdlib> #define _CRT_SECURE_NO_WARNINGS using namespace std; int main() { setlocale(LC_ALL, "Russian"); cout << "Лабораторная работа №2"<<endl; cout <<"------------------------------------------------------------------------------"<<endl; getOctStr s("20000"); getOctStr f("25000"); cout << "Первая строка: "; cout << s << endl; cout << "Перевод из восьмеричной в двоичную систему счисления: "; cout << sf << endl; cout <<"------------------------------------------------------------------------------"<<endl; cout << "Вторая строка: "; cout << f<< endl; cout << "Перевод из восьмеричной в двоичную систему счисления: "; cout << f - s << endl; cout <<"------------------------------------------------------------------------------"<<endl; cout << "Склеивание двух строк: "; s += f; cout << s << endl; cout <<"------------------------------------------------------------------------------"<<endl; cout << "Длина первой строки: "; cout << s.getLength() << endl; cout <<"------------------------------------------------------------------------------"<<endl; cout << "Длина второй строки: "; cout << f.getLength() << endl; cout <<"------------------------------------------------------------------------------"<<endl; cout << "Строки равны - 1. Строки не равны - 0"<<endl; cout << (s == f) << endl; cout <<"------------------------------------------------------------------------------"<<endl; _getch(); return 0; } String::String() { Str = 0; Length = 0; } String::String(const char* ptr) : Length(strlen(ptr)), Str(new char[Length + 1]) { strcpy(Str, ptr); } String::String(const String& t) : Length(strlen(t.Str)), Str(new char[Length + 1]) { strcpy(Str, t.Str); } String& String::operator += (const String& t) { int newLength = Length + t.Length; char *newStr = new char[newLength + 1]; strcpy(newStr, Str); strcat(newStr, t.Str); delete[] Str; Str = newStr; Length = newLength; return *this; } bool String::operator == (const String& t) const { return Length == t.Length && strcmp(Str, t.Str) == 0; } bool String::operator != (const String& t) const { return !(operator == (t)); } bool String::is_empty() const { return Str == 0 || Str[0] == '\0'; } const char* String::getStr() const { return Str; } int String::getLength() const { return Length; } ostream & String::show(ostream & os) const { return os << "\"" << (Str ? Str : "") << "\""; } String::~String() { Length = 0; delete[] Str; Str = 0; } getOctStr::getOctStr() : String() { } getOctStr::getOctStr(const char* ptr) : String(ptr) { } getOctStr::getOctStr(const getOctStr& t) : String(t) { } String getOctStr::operator - (const String& t) { const int MAX_BUF = 80; char buf[MAX_BUF]; _ltoa(strtoll(Str, 0, 8), buf, 2); return String(buf); } getOctStr::~getOctStr() { } 

File String.h:

 #pragma once #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> #include <cstdlib> #include <cassert> #include <vector> using namespace std; class String { protected: int Length; char* Str; public: String(); String(const char* ptr); String(const String& t); String& operator += (const String& t); bool operator == (const String& t) const; String& operator = (const String& t); if (this ==&t) { return *this; } Length = t.Length; return *this; bool operator != (const String& t) const; bool is_empty() const; const char* getStr() const; int getLength() const; ostream & show(ostream & os) const; friend ostream & operator << (ostream & os, const String & s) { return s.show(os); } ~String(); }; class getOctStr : public String { public: getOctStr(); getOctStr(const char* ptr); getOctStr(const getOctStr& t); String operator - (const String& t); ~getOctStr(); }; 

Just do not understand how to do it, but you need to do it. Thanks in advance for the help to all who tell. PS I would be very grateful to the finished code, since I tried adding a string to String.h:

 String& operator = (String& t); 

And in the PROGA.cpp line:

 String& String::operator = (String& t) { swap (Length, t.Length); swap (Str, t.Str); return *this; } 

To which I was told that the swap is not used at all, but on the other - it does not work at all and everything does not work. Thanks in advance for the answer!

    1 answer 1

    When you write int a = 1, b = 2; a = b; int a = 1, b = 2; a = b; do you expect b change after setting a? An assignment operation should not change its argument.

    The first approach to solving your problem: when assigning, release the current line, allocate memory for the new line and copy the data:

     String& operator=(const String& t) { // аргумент не должен изменятся -> const delete[] Str; Length = t.Length; Str = new char[Length]; strcpy(Str, t.Str); return *this; } 

    This one has disadvantages, such as: the problem of the security of exceptions, the need to check for assignment to oneself. The best approach would be to use the copy-and-swap idiom :

     String& operator=(const String& t) { String temp{t}; std::swap(*this, temp); return *this; } 

    More about idiom copy-and-swap

    • one
      For copy & swap, IMHO, better instead of String& operator=(const String& t){String temp{t}; make String &operator=(String temp){ . Then it will also work as move assignment ... - HolyBlackCat
    • @HolyBlackCat, I agree. @QypeR, String& String::operator= and implementation. - academ
    • @acade, Thank you very much, that is, I need to add the above lines to the PROGA.cpp file, and in the String.h file leave the string String & operator = (String & t); So? And if not, how?) - QypeR
    • @HolyBlackCat, but I do not agree - this is a bad way. Need move - add the appropriate operator. - ixSci
    • @ixSci is possible more in detail? How is it worse? - HolyBlackCat