Write in C++. Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dy- namic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting of words separated by blanks. Enforce the restriction that the array elements of type StringVar contain no blanks (except for the end marker elements of type StringVar).
Your class Text will have member functions corresponding to all the member functions of StringVar. The constructor with an argument of type const char a[] will initialize the Text object in the same way as described below for input_line. If the C-string argument contains the new-line symbol '\n', that is considered an error and ends the program with an error message.
The member function input_line will read blank separated strings and store each string in one element of the dynamic array with base typeStringVar. Multiple blank spaces are treated the same as a single blank space. When outputting an object of the class Text, insert one blank between each value of type StringVar. You may either assume that no tab symbols are used or you can treat the tab symbols the same as a blank; if this is a class assignment, ask your instructor how you should treat the tab symbol.
Add the enhancements described in Programming Project 6. The overloaded version of the extraction operator >> will fill only one element of the dynamic array.
//This is the definition for the class StringVar
//whose values are strings. An object is declared as follows.
//Note that you use (max_size), not [max_size]
// StringVar the_object(max_size);
//where max_size is the longest string length allowed.
#include <iostream>
using namespace std;
class StringVar
{
public:
StringVar(int size);
//Initializes the object so it can accept string values up to
size
//in length. Sets the value of the object equal to the empty
string.
StringVar( );
//Initializes the object so it can accept string values of length
100
//or less. Sets the value of the object equal to the empty
string.
StringVar(const char a[]);
//Precondition: The array a contains characters terminated with
'\0'.
//Initializes the object so its value is the string stored in a
and
//so that it can later be set to string values up to strlen(a) in
length
StringVar(const StringVar& string_object);
//Copy constructor.
~StringVar( );
//Returns all the dynamic memory used by the object to the
freestore.
int length( ) const;
//Returns the length of the current string value.
void input_line(istream& ins);
//Precondition: If ins is a file input stream, then ins has
been
//connected to a file.
//Action: The next text in the input stream ins, up to '\n', is
copied
//to the calling object. If there is not sufficient room,
then
//only as much as will fit is copied.
friend ostream& operator <<(ostream& outs, const
StringVar& the_string);
//Overloads the << operator so it can be used to output
values
//of type StringVar
//Precondition: If outs is a file output stream, then outs
//has already been connected to a file.
private:
char *value; //pointer to dynamic array that holds the string
value.
int max_length; //declared max length of any string value.
};
//Program to demonstrate use of the class StringVar.
void conversation(int max_name_size);
//Carries on a conversation with the user.
int main( )
{
using namespace std;
conversation(30);
cout << "End of demonstration.\n";
return 0;
}
// This is only a demonstration function:
void conversation(int max_name_size)
{
using namespace std;
StringVar your_name(max_name_size), our_name("Borg");
cout << "What is your name?\n";
your_name.input_line(cin);
cout << "We are " << our_name << endl;
cout << "We will meet again " << your_name <<
endl;
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//DISPLAY 11.12 Implementation of StringVar
//This is the implementation of the class StringVar.
//The definition for the class StringVar is in Display 11.11.
#include <iostream>
#include <cstdlib>
#include <cstddef>
#include <cstring>
using namespace std;
//Uses cstddef and cstdlib:
StringVar::StringVar(int size) : max_length(size)
{
value = new char[max_length + 1];//+1 is for '\0'.
value[0] = '\0';
}
//Uses cstddef and cstdlib:
StringVar::StringVar( ) : max_length(100)
{
value = new char[max_length + 1];//+1 is for '\0'.
value[0] = '\0';
}
//Uses cstring, cstddef, and cstdlib:
StringVar::StringVar(const char a[]) : max_length(strlen(a))
{
value = new char[max_length + 1];//+1 is for '\0'.
strcpy(value, a);
}
//Uses cstring, cstddef, and cstdlib:
StringVar::StringVar(const StringVar& string_object)
: max_length(string_object.length( ))
{
value = new char[max_length + 1];//+1 is for '\0'.
strcpy(value, string_object.value);
}
StringVar::~StringVar( )
{
delete [] value;
}
//Uses cstring:
int StringVar::length( ) const
{
return strlen(value);
}
//Uses iostream:
void StringVar::input_line(istream& ins)
{
ins.getline(value, max_length + 1);
}
//Uses iostream:
ostream& operator <<(ostream& outs, const
StringVar& the_string)
{
outs << the_string.value;
return outs;
}
//////////////////////////
void Stringvar::operator =(const StringVar& right_side)
{
int new_lenght = strlen(right_side.value);
if(new_lenght > max_length)
{
delete [] value;
max_length = new_length;
value = new char[max_lenth+1];
}
for(int i =0; i < new_length; i++)
value[i] = right_side.value[i];
value[new_length] = '\0';
}
Get Answers For Free
Most questions answered within 1 hours.