daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Jun 24th, 2018 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //.H FILE
  2. #pragma once
  3.  
  4. #include <iostream>
  5. #include <vector>
  6.  
  7. using std::vector;
  8.  
  9. class String
  10. {
  11. public:
  12.     String();
  13.     String(const char * str);
  14.     String(const String & copy);
  15.  
  16.  
  17.     ~String();
  18.     char* p_str;
  19.     const char* data() const;
  20.  
  21.     String & operator = (const String & rhs);
  22.     String & operator += (const String & rhs);
  23.  
  24. public:
  25.     char * m_str;
  26.     char * storage_str;
  27. };
  28.  
  29. std::ostream& operator << (std::ostream& output, const String& oStream);
  30.  
  31. std::istream& operator >> (std::istream& input, const String& iStream);
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. //CPP FILE
  39. #pragma once
  40. #include "string_header.h"
  41. #include <iostream>
  42. #include <cstring>
  43. #include <vector>
  44. #pragma warning(disable : 4996)
  45.  
  46. using std::strlen;
  47. using std::strcpy;
  48. using std::strcat;
  49. using std::streamsize;
  50. using std::streambuf;
  51.  
  52. //default constructor, sets m_str to nullptr.
  53. String::String() : m_str(nullptr), storage_str(nullptr)
  54. {
  55.     m_str = new char[666];
  56. }
  57.  
  58. //constructor that passes a const char ptr called "str" I.E. String alpha("Hello");
  59. String::String(const char * str) : m_str(nullptr), storage_str(nullptr)
  60. {
  61.     m_str = new char[strlen(str) + 1];
  62.     strcpy(m_str, str);
  63. }
  64.  
  65.  
  66. //Allows for a string to be passed as a parameter to initialize a string objects m_str.
  67. String::String(const String & copy) : m_str(nullptr), storage_str(nullptr)
  68. {
  69.     if (copy.m_str != nullptr)
  70.     {
  71.         m_str = new char[strlen(copy.m_str) + 1];
  72.         strcpy(m_str, copy.m_str);
  73.     }
  74. }
  75.  
  76. //Subroutine used to return the private m_str of a String object, preventing the forced use of a public data member.
  77. const char* String::data() const
  78. {
  79.     return m_str;
  80. }
  81.  
  82. //overloads the = operator for Assigning a string to another string.
  83. String & String::operator = (const String & rhs)
  84. {
  85.     if (this != &rhs)
  86.     {
  87.         delete[] m_str;
  88.         if (rhs.m_str != nullptr)
  89.         {
  90.             m_str = new char[strlen(rhs.m_str) + 1];
  91.             strcpy(m_str, rhs.m_str);
  92.         }
  93.  
  94.         else if (rhs.m_str == nullptr)
  95.         {
  96.             m_str = nullptr;
  97.         }
  98.     }
  99.     return *this;
  100. }
  101.  
  102. //overloads the += operator to cocatenate two strings.
  103. String & String::operator += (const String & rhs)
  104. {
  105.     if (this != &rhs)
  106.     {
  107.         if (rhs.m_str != nullptr)
  108.         {
  109.             storage_str = new char[strlen(m_str) + 1];
  110.             strcpy(storage_str, m_str);
  111.             delete m_str;
  112.             m_str = new char[(strlen(storage_str) + strlen(rhs.m_str)) + 1];
  113.             strcpy(m_str, storage_str);
  114.             strcat(m_str, rhs.m_str);
  115.         }
  116.         else if (rhs.m_str == nullptr)
  117.         {
  118.             m_str = nullptr;
  119.         }
  120.     }
  121.     return *this;
  122. }
  123.  
  124. //Defines how string objects are to be handled by the output operators.
  125. std::ostream& operator << (std::ostream& output, const String& oStream)
  126. {
  127.     return output << oStream.data();
  128. }
  129.  
  130.  
  131. std::istream& operator >> (std::istream& input, const String& iStream)
  132. {
  133.     streambuf* in_buf = input.rdbuf();
  134.     streamsize size_of_stream = in_buf->in_avail();
  135.    
  136.     return input >> iStream.m_str;
  137. }
  138.  
  139. //destructors.
  140. String::~String()
  141. {
  142.     delete[] m_str;
  143.     delete[] storage_str;
  144. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top