I need help designing the following program. This EXACT problem I don't think was posted
previously.
/**
* Driver File
* File Name: LabProj6Driver.cpp
*
* Description: This program demonstrates a basic String class that implements
* dynamic allocation and operator overloading.
*
*/
#include <iostream>
#include "mystring2.h"
using namespace std;
/************************ Function Prototypes ************************/
/*
* Function: PrintString
* Usage: PrintString(str);
*
* Prints out the value and length of the String object passed to it.
*/
void PrintString(const char *label,
const String &str); // overloaded ostream operator << is used in the definition.
/*************************** Main Program **************************/
int main()
{
String str1, str2("init2"), str3 = "init3"; // Some String objects. Using constructor for copy
char s1[100], s2[100], s3[100]; // Some character strings.
// Print out their initial values...
cout << "Initial values:" << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Store some values in them...
cout << "\nEnter a value for str1 (no spaces): ";
cin >> s1;
str1 = s1;
cout << "\nEnter a value for str2 (no spaces): ";
cin >> s2;
str2 = s2;
cout << "\nEnter a value for str3 (no spaces): ";
cin >> s3;
str3 = s3;
cout << "\nAfter assignments..." << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Access some elements...
int i;
cout << "\nEnter which element of str1 to display: ";
cin >> i;
cout << "Element #" << i << " of str1 is '" << str1[i]
<< "'" << endl;
cout << "\nEnter which element of str2 to display: ";
cin >> i;
cout << "Element #" << i << " of str2 is '" << str2[i]
<< "'" << endl;
cout << "\nEnter which element of str3 to display: ";
cin >> i;
cout << "Element #" << i << " of str3 is '" << str3[i]
<< "'" << endl;
// Append some strings...
cout << "\nEnter a value to append to str1 (no spaces): ";
cin >> s1;
// str1.append(s1); // Actually, the cstring is converted to String object here by the constructor
str1 += s1; // same as above
cout << "\nEnter a value to append to str2 (no spaces): ";
cin >> s2;
str2 += s2;
cout << "\nEnter a value to append to str3 (no spaces): ";
cin >> s3;
str3 += s3;
cout << "\nAfter appending..." << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Compare some strings...
cout << "\nComparing str1 and str2..." << endl;
cout << "\"";
cout<< str1; // test the overloading of ostream operator <<
cout << "\" is ";
if (str1 < str2) { // test the overloading of comparison operator <
cout << "less than";
} else if (str1 > str2) {
cout << "greater than";
} else {
cout << "equal to";
}
cout << " \"";
cout << str2;
cout << "\"" << endl;
cout << "\ntest the = operator, after str1 = str2; "<< endl;
str1 = str2;
PrintString("str1", str1);
PrintString("str2", str2);
str1 += s1;
cout << "\nAfter str1 = str1 + s1: "<< endl;
PrintString("str1", str1);
PrintString("str2", str.
I need help designing the following program- This EXACT problem I don'.pdf
1. I need help designing the following program. This EXACT problem I don't think was posted
previously.
/**
* Driver File
* File Name: LabProj6Driver.cpp
*
* Description: This program demonstrates a basic String class that implements
* dynamic allocation and operator overloading.
*
*/
#include <iostream>
#include "mystring2.h"
using namespace std;
/************************ Function Prototypes ************************/
/*
* Function: PrintString
* Usage: PrintString(str);
*
* Prints out the value and length of the String object passed to it.
*/
void PrintString(const char *label,
const String &str); // overloaded ostream operator << is used in the definition.
/*************************** Main Program **************************/
int main()
{
String str1, str2("init2"), str3 = "init3"; // Some String objects. Using constructor for copy
char s1[100], s2[100], s3[100]; // Some character strings.
// Print out their initial values...
cout << "Initial values:" << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Store some values in them...
2. cout << "nEnter a value for str1 (no spaces): ";
cin >> s1;
str1 = s1;
cout << "nEnter a value for str2 (no spaces): ";
cin >> s2;
str2 = s2;
cout << "nEnter a value for str3 (no spaces): ";
cin >> s3;
str3 = s3;
cout << "nAfter assignments..." << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Access some elements...
int i;
cout << "nEnter which element of str1 to display: ";
cin >> i;
cout << "Element #" << i << " of str1 is '" << str1[i]
<< "'" << endl;
cout << "nEnter which element of str2 to display: ";
cin >> i;
cout << "Element #" << i << " of str2 is '" << str2[i]
<< "'" << endl;
cout << "nEnter which element of str3 to display: ";
cin >> i;
cout << "Element #" << i << " of str3 is '" << str3[i]
<< "'" << endl;
// Append some strings...
cout << "nEnter a value to append to str1 (no spaces): ";
cin >> s1;
// str1.append(s1); // Actually, the cstring is converted to String object here by the constructor
str1 += s1; // same as above
cout << "nEnter a value to append to str2 (no spaces): ";
cin >> s2;
str2 += s2;
3. cout << "nEnter a value to append to str3 (no spaces): ";
cin >> s3;
str3 += s3;
cout << "nAfter appending..." << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Compare some strings...
cout << "nComparing str1 and str2..." << endl;
cout << """;
cout<< str1; // test the overloading of ostream operator <<
cout << "" is ";
if (str1 < str2) { // test the overloading of comparison operator <
cout << "less than";
} else if (str1 > str2) {
cout << "greater than";
} else {
cout << "equal to";
}
cout << " "";
cout << str2;
cout << """ << endl;
cout << "ntest the = operator, after str1 = str2; "<< endl;
str1 = str2;
PrintString("str1", str1);
PrintString("str2", str2);
str1 += s1;
cout << "nAfter str1 = str1 + s1: "<< endl;
PrintString("str1", str1);
PrintString("str2", str2);
4. String str4(str3);
cout << "ntest the copy constructor, after str4(str3);"<< endl;
PrintString("str3", str3);
PrintString("str4", str4);
cout << "nafter appending str3 by str2" << endl;
str3 += str2;
PrintString("str3", str3);
PrintString("str4", str4);
cout<< "nstr2, str4 are not changed. Type any letter to quit." << endl;
char q;
cin >> q;
return 0;
}
/*********************** Function Definitions **********************/
void PrintString(const char *label,
const String &str)
{
cout << label << " holds "";
cout << str; // << is overloaded
cout << "" (length = " << str.length() << ")" << endl;
}
/* Program Sample Output:
=========================
Initial values:
str1 holds "" (length = 0)
str2 holds "init2" (length = 5)
str3 holds "init3" (length = 5)
Enter a value for str1 (no spaces): red
Enter a value for str2 (no spaces): yellow
Enter a value for str3 (no spaces): blue
After assignments...
str1 holds "red" (length = 3)
str2 holds "yellow" (length = 6)
str3 holds "blue" (length = 4)
5. Enter which element of str1 to display: 1
Element #1 of str1 is 'e'
Enter which element of str2 to display: 2
Element #2 of str2 is 'l'
Enter which element of str3 to display: 3
Element #3 of str3 is 'e'
Enter a value to append to str1 (no spaces): rose
Enter a value to append to str2 (no spaces): house
Enter a value to append to str3 (no spaces): sky
After appending...
str1 holds "redrose" (length = 7)
str2 holds "yellowhouse" (length = 11)
str3 holds "bluesky" (length = 7)
Comparing str1 and str2...
"redrose" is less than "yellowhouse"
test the = operator, after str1 = str2;
str1 holds "yellowhouse" (length = 11)
str2 holds "yellowhouse" (length = 11)
After str1 = str1 + s1:
str1 holds "yellowhouserose" (length = 15)
str2 holds "yellowhouse" (length = 11)
test the copy constructor, after str4(str3);
str3 holds "bluesky" (length = 7)
str4 holds "bluesky" (length = 7)
after appending str3 by str2
str3 holds "blueskyyellowhouse" (length = 18)
str4 holds "bluesky" (length = 7)
str2, str4 are not changed. Type any letter to quit.
q
*/
=========================================================
======================================
8. #ifndef _MYSTRING_H
#define _MYSTRING_H
#include <iostream>
#include <cstring> // for strlen(), etc.
using namespace std;
#define MAX_STR_LENGTH 200
class String {
public:
String();
String(const char s[]); // a conversion constructor
void append(const String &str);
// Relational operators
bool operator ==(const String &str) const;
bool operator !=(const String &str) const;
bool operator >(const String &str) const;
bool operator <(const String &str) const;
bool operator >=(const String &str) const;
String operator +=(const String &str);
void print(ostream &out) const;
int length() const;
char operator [](int i) const; // subscript operator
private:
char contents[MAX_STR_LENGTH+1];
int len;
};
ostream & operator<<(ostream &out, const String & r); // overload ostream operator "<<" -
External!
#endif /* not defined _MYSTRING_H */
CMPSC122 Lab Proj 6 - List and "Big Three" Project. C + + String Class (20 points - Submit
your source files named mystring2.h and mystring2.cpp online before the due time next week)
Objective: To gain experience with dynamic data structures (allocation, automatic expansion,
deletion) and the "big three" concepts: Destructors, Copy constructors, and Assignment
operators. Project Description: Two files mystringl.h and mystringl.cpp, define a String class
implementation by using a static array as the data member. The purpose of this assignment is to
9. rewrite this class by replacing the static array with a pointer (dynamic array). You are required to
modify the String class accordingly: Data: we wish to allow our String class to accommodate
strings of different sizes. To that end, instead of storing a string's characters in a fixed-size array,
each String object should contain the following data as part of its internal representation: (1) A
character pointer is meant to point to a dynamically allocated array of characters. (2) A length
field that will hold the current length of the string at any given moment. Operations: same as the
methods in String class, plus the "big three" (destructor, copy constructor, and assignment
operator overloading). Modularity: Write the String class code in a modular fashion, i.e., a file
mystring 2. h (the header file) should hold the class definition (use conditional compilation
directives) and a file mystring2.cpp (the implementation file) should hold the method definitions.
Download: LabProj6Driver.cpp -- A driver program provides a main program to test your new
class. Note: 1. In your implementation of String's methods, you may use the library functions
provided by < cstring>, which operate on the C-style string. Our textbook contains a good review
of it from pages 212 to 214 and Appendix D5 if you have the textbook. However, the difference
with the last assignment is that you need to allocate memory first in the implementations of
constructors. Do not change the implementations of overloading operators. 2. You need to
upload the two source files: your mystring2.h and mystring 2. c pp Grading Policy: (total 20
points) Grading of this programming assignment will reflect two factors, weighted as shown
below: 1. (17 points) Correctness -- does the program run correctly and follow the requirement?
2. (3 points) Style -- does the code follow the "Documentation and Submission Guidelines"? Is
the code well-structured, and readable? Do you paste your output as the comment after the main
function?