Question
text.cpp #include #include #include #include #include Text.h using namespace std; Text::Text(const char *inputString) /// Initialize using inputString. Null is accounted for with default parameter of
text.cpp
#include
#include
#include
#include
#include "Text.h"
using namespace std;
Text::Text(const char *inputString) /// Initialize using inputString. Null is accounted for with default parameter of ""
{
try ///Exception handler for failure to allocate memory for array
{
BUFFERSIZE = strlen(inputString) + 1; ///Sets buffer size equal to the string length of the input + 1 to account for NULL char at the end
buffer = new char[BUFFERSIZE](); ///Creates dynamic char array of size BUFFERSIZE
}
catch (bad_alloc &e) ///Catch block outputs error message if dynamic array fails to initialize/allocate memory
{
cout
}
for (int i = 0; i
{
buffer[i] = inputString[i];
}
}
Text::Text(const Text &other) /// Copy constructor. Initializes object with value of another object
{
BUFFERSIZE = other.getLength() + 1; ///Sets BUFFERSIZE of new object equal to copied object
buffer = new char[BUFFERSIZE]; ///Creates dynamic char array named buffer
buffer = other.buffer; ///Copies chars from other buffer to new buffer
}
void Text::operator = (const Text &other) ///Overloading function for the '=' operator
{
if (this != &other) ///Operation will only run if the object on the left side is not the same object on the right side of the operator.
{
BUFFERSIZE = other.getLength() + 1; ///Sets current BUFFERSIZE equal to BUFFERSIZE of the object on the right of the operator
delete[] buffer; ///Deletes the current buffer array
buffer = new char[BUFFERSIZE]; ///Reinitializes buffer array to new BUFFERSIZE
for (int i = 0; i
{
buffer[i] = other[i];
}
}
}
Text::~Text() ///Destructor for Text onject
{
if (buffer != NULL) ///Operation will not run if buffer is already NULL
delete[] buffer; ///Deallocates memory in the heap taken up by the dynamic array
}
int Text::getLength() const /// Returns number of characters stored in buffer, not counting the NULL character
{
int count = 0; ///Starts counter at 0
while (buffer[count] != NULL) ///Counts until NULL char is encountered
{
count++; ///Increments count for each char
}
return count; ///Returns value of count variable to the caller
}
char Text::operator [] (int n) const /// Subscript
{
return buffer[n]; ///Returns the nth element of the buffer
}
void Text::clear() /// Clear string
{
for (int i = 0; i
{
buffer[i] = NULL;
}
}
void Text::showStructure() const ///Outputs the entire buffer to the user
{
for (int i = 0; i
{
cout
}
cout
}
istream & operator >> (istream &input, Text &inputText) ///Function to overload operator '>>'
{
const int textBufferSize = 256; ///Initializes textBufferSize to a const value of 256
char textBuffer[textBufferSize]; ///Initializes textBuffer to textBufferSize elements
input >> setw(textBufferSize) >> textBuffer; ///Sets input width to textBufferSize (possibly truncating input), and then storing input into textBuffer
inputText = textBuffer; ///Stores textBuffer in inputText object via the overloaded assignment operator
return input; ///Returns istream input object
}
ostream & operator
{
output
return output; ///Returns the output ostream object
}
hi
it is from c++ Data structures.
A full-page version of this exercise with space for writing in answers is available in the online supplements for Lab 1. Part A What are the implications of having no destructor in a class like Text that does dynamic memory allocation? What are the practical consequences of not having a destructor for these classes inia long-running program? Part B What other operators might it make sense to overload in the Text class? Name four and briefly describe how they would work. Part C Are there any operators that it does not make sense to overload in the Text class? Why not? A full-page version of this exercise with space for writing in answers is available in the online supplements for Lab 1. Part A What are the implications of having no destructor in a class like Text that does dynamic memory allocation? What are the practical consequences of not having a destructor for these classes inia long-running program? Part B What other operators might it make sense to overload in the Text class? Name four and briefly describe how they would work. Part C Are there any operators that it does not make sense to overload in the Text class? Why notStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started