0% found this document useful (0 votes)
279 views

"Mystring.h": #Include #Include #Include Namespace Const Char

A sample implementation for the mystring class from Data Structures and Other Objects Using C++ 4th Ed

Uploaded by

kmets89
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
279 views

"Mystring.h": #Include #Include #Include Namespace Const Char

A sample implementation for the mystring class from Data Structures and Other Objects Using C++ 4th Ed

Uploaded by

kmets89
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <cstring>
#include "mystring.h"
namespace main_savitch_4
{
mystring::mystring(const char str[])
// The constructor (default argument is an empty string.)
// Precondition: str is an ordinary null-terminated string.
// Postcondition: The string contains the sequence of chars from str.
{
size_t size = strlen(str);
sequence = new char[size + 1];
strcpy(sequence, str);

current_length = size;
allocated = current_length + 1;

mystring::mystring(const mystring& source)


// copy constructor
// Postcondition: an object has been created with the same
// data as source but with a new pointer
{
sequence = new char[source.allocated];
strcpy(sequence, source.sequence);

current_length = source.current_length;
allocated = source.allocated;

mystring::~mystring()
// destructor
// Precondition: a mystring object has been dynamically allocated in memory
// Postcondition: the array created by the mystring object has been safely
deleted
{
delete[] sequence;
sequence = 0;
}
void mystring::operator += (const mystring& addend)
// Postcondition: addend has been catenated to the end of the string.
{
if (current_length + addend.current_length > allocated - 1)
reserve(current_length + addend.current_length);
// All overloaded operator += functions check to see if more space
// is needed before allocating any additional memory
strcat(sequence, addend.sequence);
current_length += addend.current_length;
}
void mystring::operator +=(const char addend[])
// Precondition: addend is an ordinary null-terminated string.
// Postcondition: addend has been catenated to the end of the string.
{
if (current_length + strlen(addend) > allocated - 1)

reserve(current_length + strlen(addend));
strcat(sequence, addend);
current_length += strlen(addend);
}
void mystring::operator +=(char addend)
// Postcondition: The single character addend has been catenated to the
// end of the string.
{
if (current_length + 1 > allocated - 1)
reserve(current_length + 1);
sequence[current_length] = addend;
sequence[current_length + 1] = '\0';
current_length++;
}
void mystring::reserve(size_t n)
// Postcondition: All functions will now work efficiently(without
// allocating new memory) until n characters are in the string.
{
if (n > current_length)
// A larger sequence is created only if the extra space is requested
{
char *new_seq;
new_seq = new char[n + 1];
strcpy(new_seq, sequence);
delete[] sequence;
sequence = new_seq;
allocated = n + 1;
}
}
void mystring::operator =(const mystring& source)
// Postcondition: A mystring object is returned with the same
// member data as source and a different pointer
{
if (current_length == source.current_length)
strcpy(sequence, source.sequence);
else
{
char *temp;
temp = new char[source.allocated];
strcpy(temp, source.sequence);
delete[] sequence;
sequence = temp;
current_length = source.current_length;
allocated = source.allocated;
}
}
char mystring::operator [ ](size_t position) const
// Precondition: position < length( ).
// Postcondition: The value returned is the character at the specified
// position of the string. A string's positions start from 0 at the start
// of the sequence and go up to length( )-1 at the right end.
{
return sequence[position];
}

std::ostream& operator <<(std::ostream& outs, const mystring& source)


// Postcondition: The sequence of characters in source has been written
// to outs. The return value is the ostream outs.
{
std::cout << source.sequence;
}

return outs;

bool operator ==(const mystring& s1, const mystring& s2)


// Postcondition: Returns true if two strings match, otherwise false
{
return !strcmp(s1.sequence, s2.sequence);
}
bool operator !=(const mystring& s1, const mystring& s2)
// Postcondition: Returns true if two strings do not match
{
return strcmp(s1.sequence, s2.sequence);
}
bool operator >=(const mystring& s1, const mystring& s2)
// Postcondition: Returns true if two strings match or if s1
// follows s2 alphabetically
{
return (strcmp(s1.sequence, s2.sequence) >= 0);
}
bool operator <=(const mystring& s1, const mystring& s2)
// Postcondition: Returns true if two strings match or if s2
// follows s1 alphabetically
{
return (strcmp(s1.sequence, s2.sequence) <= 0);
}
bool operator > (const mystring& s1, const mystring& s2)
// Postcondition: Returns true if if s1
// follows s2 alphabetically
{
return (strcmp(s1.sequence, s2.sequence) > 0);
}
bool operator < (const mystring& s1, const mystring& s2)
// Postcondition: Returns true if if s2
// follows s1 alphabetically
{
return (strcmp(s1.sequence, s2.sequence) < 0);
}
mystring operator +(const mystring& s1, const mystring& s2)
// Postcondition: The string returned is the catenation of s1 and s2.
{
mystring new_string;
new_string += s1;
new_string += s2;
return new_string;
}
std::istream& operator >>(std::istream& ins, mystring& target)

//
//
//
//
//
//
{

Postcondition: A string has been read from the istream ins, and the
istream ins is then returned by the function. The reading operation
skips white space (i.e., blanks, newlines, tabs) at the start of ins.
Then the string is read up to the next white space or the end of the
file. The white space character that terminates the string has not
been read.
char c;
while ((ins) && isspace(ins.peek()))
ins.ignore();
while (ins && ((ins.peek()) != '\n'))
{
ins >> c;
target += c;
}

return ins;

void getline(std::istream& ins, mystring& target, char delimiter)


// Postcondition: A string has been read from the istream ins. The reading
// operation starts by skipping any white space, then reading all characters
// (including white space) until the delimiter is read and discarded (but
// not added to the end of the string). The return value is ins.
{
char c;
while (ins && ((ins.peek()) != delimiter))
{
}
}

ins >> c;
target += c;

You might also like