1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include "mystring.h"
//**************************************************
// Constructor to initialize the str member *
// with a string constant. *
//**************************************************
MyString::MyString(char *sptr)
{
len = strlen(sptr);
str = new char[len + 1];
strcpy(str, sptr);
}
//*************************************************
// Copy constructor. *
//*************************************************
MyString::MyString(MyString &right)
{
str = new char[right.length() + 1];
strcpy(str, right.getValue());
len = right.length();
}
//************************************************
// Overloaded = operator. Called when operand *
// on the right is another MyString object. *
// Returns the calling object. *
//************************************************
MyString MyString::operator=(MyString &right)
{
if (len != 0)
delete [] str;
str = new char[right.length() + 1];
strcpy(str, right.getValue());
len = right.length();
return *this;
}
//***********************************************
// Overloaded = operator. Called when operand *
// on the right is a string. *
// Returns the calling object. *
//***********************************************
MyString MyString::operator=(const char *right)
{
if (len != 0)
delete [] str;
len = strlen(right);
str = new char[len + 1];
strcpy(str, right);
return *this;
}
//**************************************************
// Overloaded += operator. Called when operand *
// on the right is another MyString object. *
// Concatenates the str member of right to the *
// str member of the calling object. *
// Returns the calling object. *
//**************************************************
MyString MyString::operator+=(MyString &right)
{
char *temp = str;
str = new char[strlen(str) + right.length() + 1];
strcpy(str, temp);
strcat(str, right.getValue());
if (len != 0)
delete [] temp;
len = strlen(str);
return *this;
}
//****************************************************
// Overloaded += operator. Called when operand *
// on the right is a string. Concatenates the *
// string right to the str member of *
// the calling object. *
// Returns the the calling object. *
//****************************************************
MyString MyString::operator+=(const char *right)
{
char *temp = str;
str = new char[strlen(str) + strlen(right) + 1];
strcpy(str, temp);
strcat(str, right);
if (len != 0)
delete [] temp;
return *this;
}
//*********************************************************
// Overloaded == operator. *
// Called when the operand on the right is a MyString *
// object. Returns true if right.str is the same as str. *
//*********************************************************
bool MyString::operator==(MyString &right)
{
return !strcmp(str, right.getValue());
}
//*****************************************************
// Overloaded == operator. *
// Called when the operand on the right is a string. *
// Returns true if right is the same as str. *
//******************************************************
bool MyString::operator==(const char *right)
{
return !strcmp(str, right);
}
void MyString::indexError() const
{
cout << "Error: Index out of bounds." << endl;
exit(0);
}
// Overloaded [] operator.
char& MyString::operator[](int index) const
{
// if index out of bounds, print error message
if(index < 0 || index > len-1)
{
indexError();
}
else return *(str + index);
}
char& MyString::at(int index) const
{
// if index out of bounds, print error message
if(index < 0 || index > len-1)
{
indexError();
}
else return *(str + index);
}
MyString MyString::append(const char* str2)
{
return str += str2;
}
MyString MyString::append(MyString& str2)
{
return str += str2.str;
}
|