Advertisement
Guest User

MyString

a guest
May 25th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. MyString.h
  3.  
  4. #ifndef MYSTRING_H
  5. #define MYSTRING_H
  6.  
  7. #include <cstring>
  8.  
  9. class MyString
  10. {
  11. public:
  12.     MyString(const char *input = "\0");
  13.     MyString(const MyString &original);
  14.     ~MyString();
  15.  
  16.     MyString& operator = (const MyString &original);
  17.     MyString& operator = (const char *input);
  18.  
  19.     MyString& operator *= (const unsigned int countOfMultiplications);
  20.     MyString operator * (const unsigned int countOfMultiplications);
  21.  
  22.     operator const char*() const;
  23.  
  24.     char* getText() const;
  25.     unsigned int getLength() const;
  26.  
  27. private:
  28.     unsigned int length;
  29.     char *text;
  30. };
  31.  
  32.  
  33. ///////////////////////////////////////////////////////////////////////////////////
  34.  
  35. MyString.cpp
  36.  
  37. #endif // MYSTRING_H
  38.  
  39. #include "MyString.h"
  40.  
  41. MyString::MyString(const char *input) : length(strlen(input)), text(new char[length])
  42. {
  43.     strcpy(text, input);
  44. }
  45.  
  46. MyString::MyString(const MyString &original) : length(original.getLength()), text(new char[length])
  47. {
  48.     strcpy(text, original.getText());
  49. }
  50.  
  51. MyString::~MyString()
  52. {
  53.     delete []text;
  54. }
  55.  
  56. MyString& MyString::operator = (const MyString &original)
  57. {
  58.     if(this != &original)
  59.     {
  60.         delete []text;
  61.         length = strlen(original.getText());
  62.         text = new char[length];
  63.         strcpy(text, original.getText());
  64.     }
  65.     return *this;
  66. }
  67.  
  68. MyString& MyString::operator = (const char *input)
  69. {
  70.     if(strcmp(text, input) != 0)
  71.     {
  72.         delete []text;
  73.         length = strlen(input);
  74.         text = new char[length];
  75.         strcpy(text, input);
  76.     }
  77.     return *this;
  78. }
  79.  
  80. MyString& MyString::operator *= (const unsigned int countOfMultiplications)
  81. {
  82.     char tmpText[length];
  83.     strcpy(tmpText, text);
  84.     delete []text;
  85.     length *= countOfMultiplications;
  86.     text = new char[length];
  87.     strcpy(text, tmpText);
  88.     for(unsigned int i = 1; i < countOfMultiplications; i++)
  89.     {
  90.         strcat(text, tmpText);
  91.     }
  92.     return *this;
  93. }
  94.  
  95. MyString MyString::operator * (const unsigned int countOfMultiplications)
  96. {
  97.     char newText[length * countOfMultiplications];
  98.     strcpy(newText, text);
  99.     for(unsigned int i = 1; i < countOfMultiplications; i++)
  100.     {
  101.         strcat(newText, text);
  102.     }
  103.     return MyString(newText);
  104. }
  105.  
  106. MyString::operator const char*() const
  107. {
  108.     return text;
  109. }
  110.  
  111. char* MyString::getText() const
  112. {
  113.     return text;
  114. }
  115.  
  116. unsigned int MyString::getLength() const
  117. {
  118.     return length;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement