You are to code the following three source files in C++: • MyString.h – In this
ID: 3854916 • Letter: Y
Question
You are to code the following three source files in C++:
• MyString.h – In this file, you declare a class named MyString.
– This class contains 3 private attributes, str, size, and strCount.
– str is a char* that points to a dynamic array of characters.
– size is an integer that contains the current size of the string.
– strCount is a static member variable that keeps track of the number of MyString objects that currently exist in memory. Its value is initially 0 before any MyString object is created. – Define public getters and setters for the 2 instance variables, as well as a getter for the static variable.
– Define public getters and setters for the 2 instance variables, as well as a getter for the static variable.
• MyString.cpp
– In this file, you provide definitions for 3 constructors and a destructor for MyString.
– The first constructor takes a string parameter, then creates a dynamic char array in the heap and copies the input string content into the array. Note that the size of the dynamic array should match the length of the input string. Also, strCount should be incremented by 1 since a new string has been created.
– The second constructor is a default constructor that takes no parameters. Here, the 2 instance attributes are initialized as “” (empty string) and 0, respectively. Again, strCount should be incremented by 1.
– The third constructor is a copy constructor that allows the following statement: MyString y = x; // at declaration so that the MyString y is initialized as the same string as x. However, make sure their respective arrays are separately created in the heap (even though their contents are the same). Again, strCount should be incremented by 1.
– Also define a destructor function for the class that releases the dynamic array. The destructor must also decrement strCount by 1.
– Add a display function that displays the current contents of the string on the screen.
– Overload operator = to allow assignment (away from declaration statement) as follows: x = y; // where x and y are both MyString objects 1 Note that this assignment must perform deep copy, which means the entire source dynamic array must be copied to the target dynamic array, not just the pointer.
– Overload operator += to allow a statement like: x += y; // where x and y are both MyString objects This operator must append MyString y at the end of x, so that x becomes a merged string. Note that the previous dynamic array of x must be destroyed and a new dynamic array must be created with an updated size.
• hw4.cpp
– In this file, you define main function that tests MyString class. You must create at least 3 MyString objects.
– Show that the first constructor properly works via a statement like: MyString s1(‘‘abc’’);
– Show that the default constructor properly works via a statement like: MyString s1;
– Show that the copy constructor properly works via a statement like: MyString s2 = s1;
– Show that the overloaded operator = properly works via a statement like: s2 = s1;
– Show that the overloaded operator += properly works via a statement like: s1 += s1;
– Show that the strCount is properly updated via a statement like: cout << getStrCount();
– Show that the display function properly works via a statement like: s1.display();
– Note that the display function must be called every time a MyString object is created or updated. Otherwise the respective functionality will not be properly demonstrated and could lead to loss of points.
Explanation / Answer
Here is the MyString.cpp
#include <iostream>
#include "MyString.h"
using namespace std;
MyString::MyString(const char* cstr)
{
str = new char[strlen(cstr)+1];
strcpy(str, cstr);
setSize(strlen(cstr) + 1);
strCount++;
display();
}
int MyString::strCount = 0;
MyString::MyString()
{
str = new char[1];
*str = '';
setSize(1);
strCount++;
display();
}
MyString::MyString(const MyString& mstr)
{
str = new char[mstr.getSize()+1];
strcpy(str, mstr.str);
setSize(mstr.getSize()+1);
strCount++;
display();
}
MyString::~MyString()
{
delete [] str;
setSize(0);
strCount--;
display();
}
void MyString::display()
{
cout<<"String is "<<getStr()<<endl;
}
MyString& MyString::operator=(const MyString& mstr)
{
if (&mstr == this)
{
return *this;
}
delete [] str;
str = new char[mstr.getSize() + 1];
strcpy(str, mstr.str);
cout<<"mstr is "<<str<<endl;
return *this;
}
MyString MyString::operator+=(const MyString& mstr)
{
MyString tmp;
delete [] str;
str = new char[mstr.getSize() + this->getSize() + 1];
strcpy(str, str);
strcat(str, mstr.str);
return str;
}
int main()
{
MyString s1("abc");
MyString s3;
s3 = s1;
s3 += s1;
cout << s1.getStrCount()<<endl;
s1.display();
return 0;
}
Here is MtString.h
#include <iostream>
using namespace std;
class MyString
{
private:
char * str;
int size;
static int strCount;
public:
void setStr(char *str) {
this->str = new char (strlen(str) +1);
strcpy(this->str, str);
}
char * getStr() const {
return str;
}
void setSize(int size) {
this->size = size;
}
int getSize() const {
return size;
}
int getStrCount() {
return strCount;
}
void display();
MyString operator+=(const MyString& mstr);
MyString& operator=(const MyString& mstr);
MyString(const char*);
MyString();
MyString(const MyString&);
~MyString();
};
Thanks
Nikhil Jain
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.