Question

You are to implement a MyString class which is our own limited implementation of the std::string...

You are to implement a MyString class which is our own limited implementation of the std::string

Header file and test (main) file are given in below, code for mystring.cpp.

Here is header file

mystring.h

/* MyString class */

#ifndef MyString_H

#define MyString_H

#include <iostream>

using namespace std;

class MyString {

private:

   char* str;

   int len;

public:

   MyString();

   MyString(const char* s);

   MyString(MyString& s);

   ~MyString();

   friend ostream& operator <<(ostream& os, MyString& s); // Prints string

   MyString& operator=(MyString& s); //Copy assignment

   MyString& operator+(MyString& s); // Creates a new string by concantenating input string

};

#endif

Here is main file, the test file

testMyString.cpp

/* Test for MyString class */

#include <iostream>

#include "mystring.h"

using namespace std;

int main()

{

char greeting[] = "Hello World!";

MyString str1(greeting); // Tests constructor

cout << str1 << endl; // Tests << operator. Should print Hello World!

char bye[] = "Goodbye World!";

MyString str2(bye);

cout << str2 << endl; // Should print Goodbye World!

MyString str3{str2}; // Tests copy constructor

cout << str3 << endl; // Should print Hello World!

str3 = str1; // Tests copy assignment operator

cout << str3 << endl; // Should print Goodbye World!

str3 = str1 + str2; // Tests + operator

cout << str3 << endl; // Should print Hello World!Goodbye World!

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include "mystring.h"
#include <cstring>

MyString::MyString() {
    str = new char[1];
    str[0] = '\0';
}

MyString::MyString(const char *s) {
    str = new char[strlen(s) + 1];
    strcpy(str, s);
}

MyString::MyString(MyString &s) {
    str = new char[strlen(s.str) + 1];
    strcpy(str, s.str);
}

MyString::~MyString() {
    delete[] str;
}

MyString &MyString::operator=(MyString &s) {
    str = new char[strlen(s.str) + 1];
    strcpy(str, s.str);
    return *this;
}

MyString &MyString::operator+(MyString &s) {
    MyString *result = new MyString();
    result->str = new char[strlen(s.str) + strlen(str) + 1];
    strcpy(result->str, str);
    strcat(result->str, s.str);
    return *result;
}

ostream &operator<<(ostream &os, MyString &s) {
    os << s.str;
    return os;
}

Add a comment
Know the answer?
Add Answer to:
You are to implement a MyString class which is our own limited implementation of the std::string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #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...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • Using C++ Use the below program. Fill in the code for the 2 functions. The expected...

    Using C++ Use the below program. Fill in the code for the 2 functions. The expected output should be: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: #include #include #include using namespace std; //Assume a line is less than 256 //Prints the characters in the string str1 from beginIndex //and inclusing endIndex void printSubString( const char* str1 , int beginIndex, int endIndex ) { } void printWordsInAString( const char* str1 ) { } int main() { char str1[] = "The fox jumps over the...

  • Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will...

    Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...

  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • C programming Write the implementation for the three functions described below. The functions are called from...

    C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. a) Write a print_string function that prints the characters in a string to screen on- by-one. b) Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT