E
E
Egor Nameles2018-09-24 14:55:10
C++ / C#
Egor Nameles, 2018-09-24 14:55:10

Implementation of the "String" class, help in writing a program, am I doing it right?

Good afternoon. We need your help in implementing one program. Here is the essence of the task:
Create a class STRING that implements a text string. That is, it stores a string of characters of variable length, allows you to determine its length, supports the operation of concatenation and comparison of two strings. Compile use cases and test suite.
Create class HEXADIMAL_STRING. Strings of this class can only contain characters '0', '1', '2', '3','4', '5','6', '7','8', '9','A, 'B','C', 'D', 'E', 'F'. If any non-allowed characters are encountered in the initializing string, HEX_STRING is set to zero.

Here is what I did. I would be grateful if you could help me complete/correct the program. I will be glad to all remarks and explanations.
My code:
String.h

#pragma once
#include <iostream>
#include <cstring>

using namespace std;

class String
{
protected:
  char* Str;
  int Length;
public:
  String();
  String(const char* ptr);
  String(const String& t);
  String& operator = (String& t);
  String& operator += (const String& t);
  bool operator == (const String& t) const;
  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();
};

String.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "String.h"
#include <iostream>
#include <cstring>

using namespace std;

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 = (String& t)
{
  swap(Length, t.Length);
  swap(Str, t.Str);
  return *this;
}

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;
}

main.cpp
#include "String.h"
#include <iostream>
#include <conio.h>
#include <cstring>

using namespace std;

int main()
{
  setlocale(LC_ALL, "Russian");

  String s("0 1 2 3 4 5 6 7 8 9 A B C D E F");
  String f("Qwertyy");
  cout << s << endl;
  cout << (s == f) << endl;
  s += f;
  cout << s << endl;

  _getch();
  return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2018-09-24
@EgaNator

Not...
the order of initialization in the constructor

protected:
  char* Str;  // first
  int Length; // second

Then or so
String::String(const char* ptr)
  : Str(new char[strlen(ptr) + 1]), Length(strlen(ptr) + 1)
{
  strcpy(Str, ptr);
}

String::String(const String& t)
  : Str(new char[t.Length]), Length(t.Length)
{
  strcpy(Str, t.Str);
}

Or swap members.
protected:
  int Length;  // first
  char* Str;   // second

it is better of course to navigate / peep at / in STL
https://ru.cppreference.com/w/cpp/string/char_traits
https://ru.cppreference.com/w/cpp/string/basic_string
//...
struct hex_str_traits
{
  static bool is_hex_chars(const char* s)
  {
    auto size{ strlen(s) };
    for (auto idx{ 0 }; idx < size; ++idx)
    {
      if (!in_range(s[idx], '0', '9') && !in_range(s[idx], 'A', 'F'))
        return false;
    }
    return true;
  }
  static bool in_range(const int& value, const int& a, const int& b)
  {
    return a <= value && value <= b;
  }
};
//...

class hex_string : public String
{
//...
public:
  hex_string(const char* s);
//...
};
//...
hex_string::hex_string(const char* s) 
  : String( (hex_str_traits::is_hex_chars(s) ? s : "") )
{
}
//...

P
Pavel, 2018-09-24
Yazovskikh @unepic

Your = operator is very cruel to the right side. Having written a=b;, I would be very surprised to find in b the contents of a.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question