Question

Write in C++. Define a class called Text whose objects store lists of words. The class...

  1. 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).

  2. 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.

Homework Answers

Answer #1

//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';
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT