Enhance the definition of the class StringVar given in displays 11.11 and 11.12
ID: 3539265 • Letter: E
Question
Enhance the definition of the class StringVar given in displays 11.11 and 11.12 by adding all of the following:
1. Member function copy_piece, which returns a specified substring; member function one_char, which returns a specified single character; and member function set_char, which changes a specified character.
2. An overloaded version of the == operator (note that only the string values have to be equal; teh values of max_length need not be the same).
3. an overloaded version of + that performs concatenation of strings of type StringVar.
4. an overloaded version of the extraction operator >> that reads one word (as opposed to input_line, which reads a whole line).
5. If you did the section on overloading the assignment operator, then add it as well. Also write a suitable test program and thoroughly test your class definition.
Here is what I have so far:
#include <iostream>
#include <cstdlib>
#include <cstddef>
#include <cstring>
using namespace std;
class StringVar
{
public:
StringVar(int size);
//Initializes the objects 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 charaters terminated with 'o'.
//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 ' ', 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.
};
//Uses cstddef and cstdlib:
StringVar::StringVar(int size) : max_length(size)
{
value = new char[max_length + 1]; //+1 is for ''.
value[0] = '';
}
//Uses cstddef and cstdlib:
StringVar::StringVar() : max_length(100)
{
value = new char[max_length + 1]; //+1 is for ''.
value[0] = '';
}
//Uses cstddef, cstdlib, and cstring:
StringVar::StringVar(const char a[]) : max_length(strlen(a))
{
value = new char[max_length + 1]; //+1 is for ''.
strcpy(value, a);
}
//Uses cstddef, cstdlib, and cstring:
StringVar::StringVar(const StringVar& string_object) : max_length(string_object.length())
{
value = new char[max_length + 1]; //+1 is for ''.
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 conversation(int max_name_size);
//Carries on a conversation with the user.
int main ( )
{
using namespace std;
conversation (30);
cout << "End of demonstration. ";
return 0;
}
%u3000
// 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 << "Hello, what is your name? ";
your_name.input_line(cin);
cout << "Glad to meet you "<< your_name << ". My name is " << our_name << "." << endl;
cout << "I'm sure we will run into each other again " << your_name << "." << endl;
}
Explanation / Answer
Hey! I can help you out in this programming question and others if you just add me on gmail...my gmail id is hassanakbar125@gmail.com
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.