for a very long time I can not figure out how to solve this condition, I ask for your help
Condition of the problem: Create a class for working with lines. The maximum sequence length is 254. The first byte must contain information about the actual number of array elements. Perform an overload of operations:
- "=" – assignment,
- "+" – concatenation (connection) of strings,
- "<=" – relation "less than or equal",
- " >= " – relation "greater than or equal",
- "==" – relation "is equal",
- "!=" – relation "is not equal".
Provide an initialization constructor, copy constructor, destructor in the class.*
Here is my code Main.cpp file
#include <iostream>
#include <string.h>
#include "line.h"
using namespace std;
int main(){
setlocale(LC_ALL , "Ukrainian");
int change;
String input1, input2;
cout << "Enter the first line: ";
cin >> input1;
cout << "Enter the second line: ";
cin >> input2;
cout << "\t\tActions" << endl;
cout << "1.Assign strings;" << endl;
cout << "2.Concatenation (connection) of strings;" << endl;
cout << "3.Ratio (greater than or equal to);" << endl;
cout << "4.Ratio (less than or equal to);" << endl;
cout << "5.Ratio (equal to);" << endl;
cout << "6.Ratio (not equal)." << endl;
cout << "Your choice: ";
cin >> change;
switch(change){
case 1:{
input2 = input1;
cout << "Result: " << input2 << endl;;
break;
}
case 2:{
String result = input1 + input2;
cout << "Result of concatenation: " << result << endl;
break;
}
case 3:{
bool result = input1 >= input2;
if(result == true){
cout << "The first line is greater than or equal to the second
" << endl;
}
else{
cout << "The first line is smaller than the second" << endl;
}
break;
}
case 4:{
bool result = input1 <= input2;
if(result == true){
cout << "The first line is less than or equal to the second" << endl;
}
else{
cout << "The first line is bigger than the second" << endl;
}
break;
}
case 5:{
if((input1 == input2)){
cout << "Equals" << endl;
}
else{
cout << "Not Equals" << 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 << "Invalid value ..." << endl;
return 1;
break;
}
}
return 0;
}
Line.h file (String class)
#include <iostream>
#include <cstdlib>
using namespace std;
class String{
private:
char *str;
public:
String();
~String();
String(const String &other);
friend String operator + (String &line, String &line2);
String operator = (String &line);
bool operator <= (String &line);
bool operator >= (String &line);
friend bool operator == (String &line, String &line2);
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 operator + (String &line,String &line2){
String output = line;
line.mystrcat(output.str,line2.str);
return output;
}
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 operator == (String &line1, String &line2){
return line1.strlenght(line1.str) == line2.strlenght(line2.str);
}
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;
}
I understand that I could have done something wrong as I am just learning.
Let's move on to the problem. This part of the condition caused me a problem: The maximum sequence length is 254. The first byte must contain information about the actual number of array elements. Please tell me how this can be implemented. I will be very grateful