Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

********USE C++ CODE********** USE C++ CODE********** USE C++ CODE*********************************************************************** ***THE THREE TEXT FILES FOR ***mystring1.h***and***mystring1.cpp***and***Assign_Driver.cpp*** ARE GIVEN BELOW********** *********************************************************************************************************************************************** MYSTRING1.H ******************************************************************************************************************************************************** //File: mystring1.h // ================

********USE C++ CODE********** USE C++ CODE********** USE C++ CODE***********************************************************************

image text in transcribed

***THE THREE TEXT FILES FOR ***mystring1.h***and***mystring1.cpp***and***Assign_Driver.cpp*** ARE GIVEN BELOW**********

***********************************************************************************************************************************************

MYSTRING1.H

********************************************************************************************************************************************************

//File: mystring1.h

// ================

// Interface file for user-defined String class.

#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

operator "

#endif /* not defined _MYSTRING_H */

******************************************************************************************************************************************************************************************************

MYSTRING1.CPP

*************************************************************************************************************************************************************************************

//File: mystring1.h

// ================

// Interface file for user-defined String class.

#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

operator "

{

s.print(out);

return out;

}

********************************************************************************************************************************************************************************************************

ASSIGN6DRIVER.CPP IS GIVEN BELOW USE IT TO TEST YOUR NEW CLASS

*****************************************************************************************************************************************************************************************************

/**

* cmpsc122 Assignment 6 test file

* File Name: Assign4driver.cpp

*

* Description: This program demonstrates a basic String class that

implements

* dynamic allocation and operator overloading.

*

*/

#include

#include "mystring.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

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

/**

* File Name: Assign4driver.cpp

*

* Description: This program demonstrates a basic String class that

implements

* dynamic allocation and operator overloading.

*

*/

#include

#include "mystring.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

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

}

******************************************************************************************************************************************************************************************************

Project Description: You are given two files mystring1.hthe document and mystring1.cppthe 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 file mystring2.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

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

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 2 Lncs 13427

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124251, 978-3031124259

More Books

Students also viewed these Databases questions

Question

4. Systematic use of measures of HRM.

Answered: 1 week ago

Question

1.what is dew ?

Answered: 1 week ago

Question

1.The difference between climate and weather?

Answered: 1 week ago

Question

1. What is Fog ?

Answered: 1 week ago

Question

How water vapour forms ?

Answered: 1 week ago

Question

What is Entrepreneur?

Answered: 1 week ago