Question

In: Computer Science

C++ programming You are to implement a MyString class which is our own limited implementation of...

C++ programming

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;

}

Solutions

Expert Solution

/* MyString class */
//mystring.h
#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



//testMyString.cpp
/* Test for MyString class */

#include <iostream>
#include<stdlib.h>
#include "mystring.h"

using namespace std;

ostream& operator <<(ostream& os, MyString& s)
{
   //print string and length of a string
   os << "String: "<<s.str<<endl;
   os<<"Length: "<<s.len<<endl;
   return os;
}
MyString ::MyString ()
{
   //allocate memory for the string
   str = (char *)malloc(sizeof(char) * 100);
   len = 0;
}
MyString:: MyString(const char *s)
{
   //copy contents of char *s to the str
   int length = string(s).length();;
   str = (char *)malloc(sizeof(char) * length);
   int pos = 0;
   while(s[pos]!='\0')
   {
       str[pos] = s[pos];
       pos++;
   }
   str[pos] = '\0';
   len = length;
}
MyString:: MyString (MyString &s)
{
   //copy contents of s.str to the str
   len = s.len;
   str = (char *)malloc(sizeof(char) * len);
   int pos = 0;
   while(s.str[pos]!='\0')
   {
       str[pos] = s.str[pos];
       pos++;
   }
   str[pos] = '\0';
}
MyString& MyString:: operator=(MyString& s)
{
   //copy contents of s.str and s.len to the str and len respectively
   len = s.len;
   str = (char *)malloc(sizeof(char) * len);
   int pos = 0;
   while(s.str[pos]!='\0')
   {
       str[pos] = s.str[pos];
       pos++;
   }
   str[pos] = '\0';
   return *this;
}
MyString& MyString:: operator+(MyString& s)
{
   //add string
   int pos = 0;
  
   while(s.str[pos]!='\0')
   {
       str[pos + len] = s.str[pos];
       pos++;
   }
   str[pos +len] = '\0';
   len += s.len;
   return *this;
  
}
MyString:: ~MyString()
{
   cout<<this<<" Object Destroyed"<<endl;
}
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;

}


Related Solutions

C++ For this assignment, you will implement the MyString class. Like the string class in C++’s...
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s standard library, this class uses C-strings as the underlying data structure. Recall that a C-string is a null-terminated array of type char. See section 8.1 in Savitch for more information about C-strings; in particular, you will find it helpful to take advantage of several of the C-string functions mentioned in that section. What To Do. In the hw8 directory that is created, you will...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp respectively. Make sure to properly test your code on your own by creating a test driver that tests every function created in the MyString class. Deliverables: proj3-MyString.h proj3-MyString.cpp proj3-testMain.cpp Memory Requirements: Your MyString should start with 10 bytes of allocated memory and should grow in size by doubling. So, we should be able to predict the capacity of your MyString as acquiring a patten...
Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is...
Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is a sub class is derived from the Account class and implements the ITransaction interface. There are two class variables i.e. variables that are shared but all the objects of this class. A short description of the class members is given below: CheckingAccount Class Fields $- COST_PER_TRANSACTION = 0.05 : double $- INTEREST_RATE = 0.005 : double - hasOverdraft: bool Methods + «Constructor» CheckingAccount(balance =...
c++ You will implement a template class with the following members: An array of our generic...
c++ You will implement a template class with the following members: An array of our generic type. The size of this array should be determined by an integer argument to the constructor. An integer for storing the array size. A constructor capable of accepting one integer argument. The constructor should initialize the array and set the array size. A method, find Max, that returns the maximum value in the array. A method, find Min, that returns the minimum value in...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
C++ You are going to implement your own vector class. Actually, we are using the following...
C++ You are going to implement your own vector class. Actually, we are using the following small and simplified subset of the interface from std::vector to keep things manageable: template class DiyVector{     public:         DiyVector();         ~DiyVector();         T& at(unsigned int index) const;         // item at index         // throws OutOfRange         unsigned int size() const;         // number of items in the vector         void pushBack(const T& item);         // append item at the end of vector...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT