Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Mystring1.h: #ifndef _MYSTRING_H #define _MYSTRING_H #include #include // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]);

image text in transcribed

Mystring1.h:

#ifndef _MYSTRING_H #define _MYSTRING_H #include #include // 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

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

#endif /* not defined _MYSTRING_H */

Mystring1.cpp?

#include"mystring1.h

String::String()

{

contents[0] = '\0';

len = 0;

}

String::String(const char s[])

{

len = strlen(s);

strcpy(contents, s);

}

void String::append(const String &str)

{

strcat(contents, str.contents);

len += str.len;

}

bool String::operator ==(const String &str) const

{

return strcmp(contents, str.contents) == 0;

}

bool String::operator !=(const String &str) const

{

return strcmp(contents, str.contents) != 0;

}

bool String::operator >(const String &str) const

{

return strcmp(contents, str.contents) > 0;

}

bool String::operator

{

return strcmp(contents, str.contents)

}

bool String::operator >=(const String &str) const

{

return strcmp(contents, str.contents) >= 0;

}

String String::operator +=(const String &str)

{

append(str);

return *this;

}

void String::print(ostream &out) const

{

out

}

int String::length() const

{

return len;

}

char String::operator [](int i) const

{

if (i = len) {

cerr

return '\0';

}

return contents[i];

}

ostream & operator

{

s.print(out);

return out;

}

MystringDriver:

/**

* cmpsc122 Assignment test file

* File Name: mystringDriver.cpp

*

* Description: This program demonstrates a basic String class that implements

* dynamic allocation and operator overloading.

*

*/

#include

#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

/*************************** 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

PrintString("str1", str1);

PrintString("str2", str2);

PrintString("str3", str3);

// Store some values in them...

cout

cin >> s1;

str1 = s1;

cout

cin >> s2;

str2 = s2;

cout

cin >> s3;

str3 = s3;

cout

PrintString("str1", str1);

PrintString("str2", str2);

PrintString("str3", str3);

// Access some elements...

int i;

cout

cin >> i;

cout

cout

cin >> i;

cout

cout

cin >> i;

cout

// Append some strings...

cout

cin >> s1;

// str1.append(s1); // Actually, the cstring is converted to String object here by the constructor

str1 += s1; // same as above

cout

cin >> s2;

str2 += s2;

cout

cin >> s3;

str3 += s3;

cout

PrintString("str1", str1);

PrintString("str2", str2);

PrintString("str3", str3);

// Compare some strings...

cout

cout

cout

cout

if (str1

cout

} else if (str1 > str2) {

cout

} else {

cout

}

cout

cout

cout

cout

str1 = str2;

PrintString("str1", str1);

PrintString("str2", str2);

str1 += s1;

cout

PrintString("str1", str1);

PrintString("str2", str2);

String str4(str3);

cout

PrintString("str3", str3);

PrintString("str4", str4);

cout

str3 += str2;

PrintString("str3", str3);

PrintString("str4", str4);

cout

char q;

cin >> q;

return 0;

}

/*********************** Function Definitions **********************/

void PrintString(const char *label,

const String &str)

{

cout

cout

cout

}

Objective: To gain experience with the dynamic data structures (allocation, automatic expansion, deletion) and the "big three" concepts: Destructors, Copy constructors, and Assignment operator. Project Description: You are given two files mystring1.hthe document and mystring1.cpp the document which define a String class implemented by using static array as the data member. The purpose of this assignment is to 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 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. The String class code must be put in its own module, i.e., a fie mystring2h (the header fie) should hold the class definition (use conditional compilation directives) and a file mystring2.cpp (the implementation file) should hold the method definitions. Download: mystring Driver.cppthe document -A driver program provides a main program to test your new class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

2. (1 point) Given AABC, tan A b b

Answered: 1 week ago