Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //.H FILE
- #pragma once
- #include <iostream>
- #include <vector>
- using std::vector;
- class String
- {
- public:
- String();
- String(const char * str);
- String(const String & copy);
- ~String();
- char* p_str;
- const char* data() const;
- String & operator = (const String & rhs);
- String & operator += (const String & rhs);
- public:
- char * m_str;
- char * storage_str;
- };
- std::ostream& operator << (std::ostream& output, const String& oStream);
- std::istream& operator >> (std::istream& input, const String& iStream);
- //CPP FILE
- #pragma once
- #include "string_header.h"
- #include <iostream>
- #include <cstring>
- #include <vector>
- #pragma warning(disable : 4996)
- using std::strlen;
- using std::strcpy;
- using std::strcat;
- using std::streamsize;
- using std::streambuf;
- //default constructor, sets m_str to nullptr.
- String::String() : m_str(nullptr), storage_str(nullptr)
- {
- m_str = new char[666];
- }
- //constructor that passes a const char ptr called "str" I.E. String alpha("Hello");
- String::String(const char * str) : m_str(nullptr), storage_str(nullptr)
- {
- m_str = new char[strlen(str) + 1];
- strcpy(m_str, str);
- }
- //Allows for a string to be passed as a parameter to initialize a string objects m_str.
- String::String(const String & copy) : m_str(nullptr), storage_str(nullptr)
- {
- if (copy.m_str != nullptr)
- {
- m_str = new char[strlen(copy.m_str) + 1];
- strcpy(m_str, copy.m_str);
- }
- }
- //Subroutine used to return the private m_str of a String object, preventing the forced use of a public data member.
- const char* String::data() const
- {
- return m_str;
- }
- //overloads the = operator for Assigning a string to another string.
- String & String::operator = (const String & rhs)
- {
- if (this != &rhs)
- {
- delete[] m_str;
- if (rhs.m_str != nullptr)
- {
- m_str = new char[strlen(rhs.m_str) + 1];
- strcpy(m_str, rhs.m_str);
- }
- else if (rhs.m_str == nullptr)
- {
- m_str = nullptr;
- }
- }
- return *this;
- }
- //overloads the += operator to cocatenate two strings.
- String & String::operator += (const String & rhs)
- {
- if (this != &rhs)
- {
- if (rhs.m_str != nullptr)
- {
- storage_str = new char[strlen(m_str) + 1];
- strcpy(storage_str, m_str);
- delete m_str;
- m_str = new char[(strlen(storage_str) + strlen(rhs.m_str)) + 1];
- strcpy(m_str, storage_str);
- strcat(m_str, rhs.m_str);
- }
- else if (rhs.m_str == nullptr)
- {
- m_str = nullptr;
- }
- }
- return *this;
- }
- //Defines how string objects are to be handled by the output operators.
- std::ostream& operator << (std::ostream& output, const String& oStream)
- {
- return output << oStream.data();
- }
- std::istream& operator >> (std::istream& input, const String& iStream)
- {
- streambuf* in_buf = input.rdbuf();
- streamsize size_of_stream = in_buf->in_avail();
- return input >> iStream.m_str;
- }
- //destructors.
- String::~String()
- {
- delete[] m_str;
- delete[] storage_str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement