Question
Use C++ For the following: Implement the Text ADT (use the declaration in the Text.h ?le) - Text(), operator=() and ~Text(), getLength(), clear(), operator[] -
Use C++ For the following:
Implement the Text ADT (use the declaration in the Text.h ?le) - Text(), operator=() and ~Text(), getLength(), clear(), operator[] - Implement the functions toUpper() and toLower() and test them by activating LAB1_TEST1 - Implement the operators ==, <, > and test them by activating LAB1_TEST2
*******************config.h*******************
/** * Text class (Lab 1) configuration file. * Activate test 'N' by defining the corresponding LAB1_TESTN to have the value 1. */ #define LAB1_TEST1 0 // Programming exercise 2: toUpper and toLower #define LAB1_TEST2 0 // Programming exercise 3: ==, <, and >
**********************Text.h*********************
//-------------------------------------------------------------------- // // Laboratory 1 Text.h // **Instructor's Solution** // Class declaration for the array implementation of the Text ADT // //--------------------------------------------------------------------
#ifndef TEXT_H #define TEXT_H
#include
using namespace std;
class Text { public:
// Constructors and operator= Text ( const char *charSeq = "" ); // Initialize using char* Text ( const Text &other ); // Copy constructor void operator = ( const Text &other ); // Assignment
// Destructor ~Text ();
// Text operations int getLength () const; // # characters char operator [] ( int n ) const; // Subscript void clear (); // Clear string
// Output the string structure -- used in testing/debugging void showStructure () const;
//-------------------------------------------------------------------- // In-lab operations // toUpper/toLower operations (Programming Exercise 2) Text toUpper( ) const; // Create upper-case copy Text toLower( ) const; // Create lower-case copy
// Relational operations (Programming Exercise 3) bool operator == ( const Text& other ) const; bool operator < ( const Text& other ) const; bool operator > ( const Text& other ) const;
private:
// Data members int bufferSize; // Size of the string buffer char *buffer; // Text buffer containing a null-terminated sequence of characters
// Friends
// Text input/output operations (In-lab Exercise 1) friend istream & operator >> ( istream& input, Text& inputText ); friend ostream & operator << ( ostream& output, const Text& outputText );
};
#endif
*****************test1.cpp******************
//-------------------------------------------------------------------- // // Laboratory 1 test1.cpp // // Test program for the operations in the Text ADT // //-------------------------------------------------------------------- #include
#include "Text.h" Text::Text ( const char *charSeq ) { } Text::Text ( const Text &other ) { } void Text::operator = ( const Text &other ) { } Text::~Text () { } int Text::getLength () const { return -1; } char Text::operator [] ( int n ) const { return 0; } void Text::clear () { } void Text::showStructure () const { } Text Text::toUpper( ) const { Text temp; return temp; } Text Text::toLower( ) const { Text temp; return temp; } bool Text::operator == ( const Text& other ) const { return false; } bool Text::operator < ( const Text& other ) const { return false; } bool Text::operator > ( const Text& other ) const { return false; } **********textio.cpp*************
//-------------------------------------------------------------------- // // Laboratory 1, In-lab Exercise 1 textio.cpp // // String input/output operations // //--------------------------------------------------------------------
#include
#include "Text.h"
//--------------------------------------------------------------------
istream & operator >> ( istream &input, Text &inputText )
// Text input function. Extracts a string from istream input and // returns it in inputText. Returns the state of the input stream.
{ const int textBufferSize = 256; // Large (but finite) char textBuffer [textBufferSize]; // text buffer
// Read a string into textBuffer, setw is used to prevent buffer // overflow.
input >> setw(textBufferSize) >> textBuffer;
// Apply the Text(char*) constructor to convert textBuffer to // a string. Assign the resulting string to inputText using the // assignment operator.
inputText = textBuffer;
// Return the state of the input stream.
return input; }
//--------------------------------------------------------------------
ostream & operator << ( ostream &output, const Text &outputText )
// Text output function. Inserts outputText in ostream output. // Returns the state of the output stream.
{ output << outputText.buffer; return output; }
Step 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