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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include<iostream>
#include<string>
#include<cassert>
#include<iomanip>
using namespace std;
//***********************************************************
// Author: D.S. Malik
//
// This class specifies the members to implement the basic
// properties of array-based lists.
//***********************************************************
class newString
{
friend ostream& operator<<(ostream&, const newString&);
friend istream& operator>>(istream&, newString&);
public:
const newString& operator=(const newString&);
//Overloads the assignment operator
newString(const char*);
const newString & operator+(const newString&);
newString();
//newString(const newString&); //copy constructor
~newString();
char &operator[] (int);
const char& operator[] (int) const;
const newString &operator+=(const newString&);
int listSize() const;
//Function to determine the number of elements in the list
//Postcondition: Returns the value of length.
int maxListSize() const;
//Function to determine the size of the list.
//Postcondition: Returns the value of maxSize.
void print() const;
//Function to output the elements of the list
//Postcondition: Elements of the list are output on the
// standard output device.
void insertAt(int location, const string & insertItem);
//Function to insert an item in the list at the
//position specified by location. The item to be inserted
//is passed as a parameter to the function.
//Postcondition: Starting at location, the elements of the
// list are shifted down, list[location] = insertItem;,
// and length++;. If the list is full or location is
// out of range, an appropriate message is displayed.
//arrayListType(int size = 100);
newString(char size = 100);
//constructor
//Creates an array of the size specified by the
//parameter size. The default array size is 100.
//Postcondition: The list points to the array, length = 0,
// and maxSize = size
newString(const newString& );
//copy constructor
//~newString();
//destructor
//Deallocates the memory occupied by the array.
protected:
string *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
char *strPtr; //pointer to the char array
//that holds the string
int strLength; //data member to store the length
// of the string
};
newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char(strLength + 1);
assert(strPtr != NULL);
strcpy(strPtr, str);
}
int newString::listSize() const
{
return length;
}
int newString::maxListSize() const
{
return maxSize;
}
void newString::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
void newString::insertAt
(int location, const string& insertItem)
{
if (location < 0 || location >= maxSize)
cerr << "The position of the item to be inserted "
<< "is out of range" << endl;
else
if (length >= maxSize) //list is full
cerr << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at the
//specified position
length++; //increment the length
}
} //end insertAt
newString::newString(char size)
{
if (size <= 0)
{
cerr << "The array size must be positive. Creating "
<< "an array of size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new string[maxSize];
assert(list != NULL);
}
newString::~newString()
{
delete [] list;
}
newString::newString
(const newString& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new string[maxSize]; //create the array
assert(list != NULL); //terminate if unable to allocate
//memory space
for (int j = 0; j < length; j++) //copy otherList
list [j] = otherList.list[j];
} //end copy constructor
const newString& newString::operator=
(const newString& otherList)
{
if (this != &otherList) //avoid self-assignment
{
delete [] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new string[maxSize]; //create the array
assert(list != NULL); //if unable to allocate memory
//space, terminate the program
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
}
const newString &newString::operator+(const newString &rightStr)
{
strcat(strPtr, rightStr.strPtr);
}
//const newString &newString::operator+=(const newString &rightStr)
//{
// strPtr += rightStr;
//}
//Overload the insertion operator <<
ostream& operator<<(ostream& osObject, const newString& str)
{
osObject << str.strPtr;
return osObject;
}
//Overload the extraction operator >>
istream& operator>>(istream& isObject, newString &str)
{
char temp[81];
isObject >> setw(81) >> temp;
str = temp;
return isObject;
}
int main()
{
//overloadOprPlus s1, s2, s3;
newString s1 = "Hello " ;
newString s2 = "there ";
newString s3 = " ";
s3 = s1 + s2;
cout << s3;
}
| |