Question: C++ Hw Problem 1 Textbook: Introduction to Programming C++ by Daniel Liang Implement the following operators: =, +, and += in the MyString class from

C++ Hw Problem 1

Textbook: Introduction to Programming C++ by Daniel Liang

Implement the following operators: =, +, and += in the MyString class from Programming Exercise 11.14 and 11.15.

Show tests for ALL operators from your main() function.

Here is the MyString Class from Programming Excercise 11.14

#include #include using namespace std;

const int MAXIMUM_SIZE = 100; // Maximum string size 100

class MyString { public: MyString() { size = 0; }

MyString(const char* chars) { int i = 0; for (; chars[i] != '\0'; i++) value[i] = chars[i];

size = i; }

char at(int index) const { return value[index]; }

int length() const { return size; }

void clear() { size = 0; }

bool empty() const { return size == 0; }

int compare(const MyString& s) const { return compare(0, size, s); }

int compare(int index, int n, MyString s) const { char* s1 = data(); char* s2 = s.data();

return strcmp(s1, s2); }

void copy(char s[], int index, int n) { int i = 0; for (; i < size && i < n; i++) s[i] = value[i + index];

i = i + 1; s[i] = '\0'; }

char* data() const { char * temp = new char[size + 1]; for (int i = 0; i < size; i++) temp[i] = value[i];

temp[size] = '\0';

return temp; }

int find(char ch) const { return find(ch, 0); }

int find(char ch, int index) const { for (int i = index; i < size; i++) if (value[i] == ch) return i;

return -1; }

int find(const MyString& s, int index) const { return -1; }

private: char value[MAXIMUM_SIZE]; int size; }; // Must place a semicolon here

int main() { MyString stringObject1("abc abc"); MyString stringObject2("efgth");

for (int i = 0; i < stringObject1.length(); i++) cout << stringObject1.at(i);

cout << endl;

cout << stringObject1.length() << endl;

cout << stringObject1.empty() << endl;

cout << stringObject1.compare(stringObject2) << endl;

cout << stringObject1.find('a') << endl; cout << stringObject1.find('a', 4) << endl; cout << stringObject1.find('a', 6) << endl;

char* temp = stringObject1.data(); cout << temp << endl;

stringObject2.clear(); cout << stringObject2.length() << endl;

return 0; }

Here is the MyString Class from 11.15

#include using namespace std; // Maximum string size 100 const int MAXIMUM_SIZE = 100; Define a class MyString that will implement the string methods. class MyString { public: //default constructor MyString() { size = 0; } Define a method MyString()that will create a constructor to declare string with char ch of size n. //constructor to declar string with char ch MyString(const char ch, int size) { value[0] = ch; this->size = size; } Define a method MyString()that will create a constructor to declare string with size //constructor to declare string with size MyString(const char chars[], int size) { for (int i = 0; i < size; i++) value[i] = chars[i]; this->size = size; } Define a method append () that will append two strings //method to append two strings MyString append (const MyString& s) { size += s.size; while(size >= MAXIMUM_SIZE) { grow(); } strcat(value, s.value); value[size + 1] = '\0'; return *this; } Define method append () to append second string from given index to n chars to string 1. //method to append second string from given index to n //chars to string 1 MyString append (MyString & s, int index, int n) { size =size+n-index; while(size >= MAXIMUM_SIZE)

#include using namespace std; // Maximum string size 100 const int MAXIMUM_SIZE = 100; Define a class MyString that will implement the string methods. class MyString { public: //default constructor MyString() { size = 0; } Define a method MyString()that will create a constructor to declare string with char ch of size n. //constructor to declar string with char ch MyString(const char ch, int size) { value[0] = ch; this->size = size; } Define a method MyString()that will create a constructor to declare string with size //constructor to declare string with size MyString(const char chars[], int size) { for (int i = 0; i < size; i++) value[i] = chars[i]; this->size = size; } Define a method append () that will append two strings //method to append two strings MyString append (const MyString& s) { size += s.size; while(size >= MAXIMUM_SIZE) { grow(); } strcat(value, s.value); value[size + 1] = '\0'; return *this; } Define method append () to append second string from given index to n chars to string 1. //method to append second string from given index to n //chars to string 1 MyString append (MyString & s, int index, int n) { size =size+n-index; while(size >= MAXIMUM_SIZE)

{ grow(); } value[index + 1] = '\0'; strncat(value, s.value,n); value[size + 1] = '\0'; return *this; } Define a method append () to append char n times to string 1s end //method to append char n times to string 1 end MyString append(int n,char ch) { size=size+n; while(size >= MAXIMUM_SIZE) { grow(); } for(int i=0;i

strcpy(value,"\0"); for(int i=0,j=0;isize) { cout<<" not enough length:"; return *this; } else { for(int i=index,j=0;i<=n;i++,j++) { s.value[j]=value[i]; } s.value[n-index]='\0'; return s; } } Define a method substr () to take substring from given index. //method to take substring from given index MyString substr(int index)const { MyString s;

for(int i=index,j=0;i

printf(" %s",str); MyString(); str=stringObject1.assign(stringObject2,2,5); printf(" %s",str); MyString(); str=stringObject1.assign(stringObject2,3); printf(" %s",str); MyString(); str=stringObject1.assign(10,'*'); printf(" %s",str); MyString(); str=stringObject2.substr(2,6); printf(" %s",str); MyString(); str=stringObject2.substr(2); printf(" %s",str); str=stringObject2.erase(2,3); printf(" %s",str); cout << endl; return 0; }

Sample Output: Hello World!Good day HellGoo HellGoo!!! Hello od Goo **********

od d od day Go -------------------------------- Process exited with return value 0 Press any key to continue . . .

Implement the following operators: =, +, and += in the MyString class from Programming Exercise 11.14 and 11.15.

Show tests for ALL operators from your main() function.

Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!