Home > front end >  Create a class for working with stitches. C
Create a class for working with stitches. C

Time:12-19

Create a class for working with stitches. The maximum length of the sequence is 254. The first byte must contain information about the actual number of array elements. Will perform an overload of operations:

  • "=" – assignment,
  • " " - concatenation (connection) of a term,
  • "<=" – relation "less than or equal to",
  • " >= " – relation "greater than or equal to",
  • "==" - relation "equals",
  • "!=" – relation "is not equal".

In the class, provide an initialization constructor, a copy constructor, and a destructor.

As a result, the program does not start for some reason

Here is my code

#include <iostream>
#include <string.h>

using namespace std;

class String {
public:
    char* str;

    String();
    ~String();

    String(const String& other);

    String operator = (String& line);
    String operator   (String& line);
    bool operator <= (String& line);
    bool operator >= (String& line);
    bool operator == (String& line);
    bool operator != (String& line);

    friend  ostream& operator<<(ostream& stream, String& obj);
    friend  istream& operator>>(istream& stream, String& obj);

    void strcopy(char* str1, char* str2);
    char mystrcat(char* str1, const char* str2);
    int strlenght(const char* str);

};

String::String() {
    str = new char[256];
}

String::~String() {
    delete[] str;
}

String::String(const String& other) {
    str = new char[256];
    strcopy(str, other.str);
}

String String::operator = (String& line) {
    strcopy(str, line.str);
    return *this;
}

String String::operator   (String& line) {
    mystrcat(str, line.str);
    return *this;
}

bool String::operator >= (String& line) {
    if (strlenght(str) >= strlenght(line.str)) {
        return true;
    }
    else {
        return false;
    }
}

bool String::operator <= (String& line) {
    if (strlenght(str) <= strlenght(line.str)) {
        return true;
    }
    else {
        return false;
    }
}

bool String::operator != (String& line) {
    if (strlenght(str) != strlenght(line.str)) {
        return true;
    }
    else {
        return false;
    }
}

bool String::operator == (String& line) {
}


ostream& operator<<(ostream& stream, String& obj)
{
    stream << obj.str;
    return stream;
}

istream& operator>>(istream& stream, String& obj)
{
    stream >> obj.str;
    return stream;
}


void String::strcopy(char* str1, char* str2) {
    while (*str1   = *str2  );
}

char String::mystrcat(char* str1, const char* str2) {
    char* begin = str1;

    while (*str1) {
        str1  ;
    }
    while (*str1   = *str2  );


    *str1 = '\0';
    return *begin;
}


int String::strlenght(const char* str) {
    int counter = 0;
    while (*str != '\0') {
        counter  ;
        str  ;
    }

    return counter;
}

int main() {
    String input1, input2;
    int change;
    cout << "Enter the first line: ";
    cin >> input1;
    cout << "Enter the second line: ";
    cin >> input2;
    cout << "\t\tActions" << endl;
    cout << "1. String Assignment;" << endl;
    cout << "2. String concatenation;" << endl;
    cout << "3.Relationship (greater than or equal to);" << endl;
    cout << "4.Relationship (less than or equal to);" << endl;
    cout << "5. Attitude (equal to);" << endl;
    cout << "6. Attitude (not equal)." << endl;
    cout << "Choice: ";
    cin >> change;
    switch (change) {
    case 1: {
        input2 = input1;
        cout << "Result: " << input2 << endl;;
        break;
    }
    case 2: {
        String result = input1   input2;
        cout << "Concatenation result: " << result << endl;
        break;
    }
    case 3: {
        bool result = input1 >= input2;
        if (result == true) {
            cout << "The first string is greater than or equal to the second" << endl;
        }
        else {
            cout << "The first line is less than the second" << endl;
        }
        break;
    }
    case 4: {
        bool result = input1 <= input2;
        if (result == true) {
            cout << "The first string is less than or equal to the second" << endl;
        }
        else {
            cout << "The first line is greater than the second" << endl;
        }
        break;
    }
    case 5: {
        if ((input1 == input2) == true) {
            cout << "Are equal" << endl;
        }
        else {
            cout << "Not equal" << endl;
        }
        break;
    }
    case 6: {
        bool result = input1 != input2;
        if (result == true) {
            cout << "The first line is not equal to the second" << endl;
        }
        else {
            cout << "The first line is equal to the second" << endl;
        }
        break;
    }

    default: {
        cout << "Mistake..." << endl;
        return 1;
        break;
    }
    }
    return 0;
}

CodePudding user response:

The only problem I can see with your code is that you are not returning anything on operator== Depending on your compiling flags this can produce a compilation error because you are not returning anything from the function.
Here is a possible implementation for that function:

bool String::operator == (String& line) 
{
    if (!strcmp(line.str, this->str))
        return true;
    else return false;
}
  •  Tags:  
  • c
  • Related