Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////////
- MyString.h
- #ifndef MYSTRING_H
- #define MYSTRING_H
- #include <cstring>
- class MyString
- {
- public:
- MyString(const char *input = "\0");
- MyString(const MyString &original);
- ~MyString();
- MyString& operator = (const MyString &original);
- MyString& operator = (const char *input);
- MyString& operator *= (const unsigned int countOfMultiplications);
- MyString operator * (const unsigned int countOfMultiplications);
- operator const char*() const;
- char* getText() const;
- unsigned int getLength() const;
- private:
- unsigned int length;
- char *text;
- };
- ///////////////////////////////////////////////////////////////////////////////////
- MyString.cpp
- #endif // MYSTRING_H
- #include "MyString.h"
- MyString::MyString(const char *input) : length(strlen(input)), text(new char[length])
- {
- strcpy(text, input);
- }
- MyString::MyString(const MyString &original) : length(original.getLength()), text(new char[length])
- {
- strcpy(text, original.getText());
- }
- MyString::~MyString()
- {
- delete []text;
- }
- MyString& MyString::operator = (const MyString &original)
- {
- if(this != &original)
- {
- delete []text;
- length = strlen(original.getText());
- text = new char[length];
- strcpy(text, original.getText());
- }
- return *this;
- }
- MyString& MyString::operator = (const char *input)
- {
- if(strcmp(text, input) != 0)
- {
- delete []text;
- length = strlen(input);
- text = new char[length];
- strcpy(text, input);
- }
- return *this;
- }
- MyString& MyString::operator *= (const unsigned int countOfMultiplications)
- {
- char tmpText[length];
- strcpy(tmpText, text);
- delete []text;
- length *= countOfMultiplications;
- text = new char[length];
- strcpy(text, tmpText);
- for(unsigned int i = 1; i < countOfMultiplications; i++)
- {
- strcat(text, tmpText);
- }
- return *this;
- }
- MyString MyString::operator * (const unsigned int countOfMultiplications)
- {
- char newText[length * countOfMultiplications];
- strcpy(newText, text);
- for(unsigned int i = 1; i < countOfMultiplications; i++)
- {
- strcat(newText, text);
- }
- return MyString(newText);
- }
- MyString::operator const char*() const
- {
- return text;
- }
- char* MyString::getText() const
- {
- return text;
- }
- unsigned int MyString::getLength() const
- {
- return length;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement