Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I am a beginner student of C++. I have struggled to a home work about obj

ID: 3918223 • Letter: H

Question

Hello, I am a beginner student of C++. I have struggled to a home work about object-oriented prgoraming, but it is very hard to understand. Please give me a example of solution of this problem.

In additon, my instructer does't allow us to use string class ; I have to use C-strings instead.

Define a class named MyString to define the string type (i.e. a sequence of characters) based on the following specification:

1. This class declares two attributes to represent a string in memory: a character pointer that holds the start address of the string in memory and an integer that holds the length of the string.

NOTE This is not a null-terminated string; do not assume any null character showing the end of a string in memory.

2. This class uses dynamic allocation to prevent wasting memory. In other words, a string including the word “hello” will take only 5 bytes for its characters and no more byte is reserved or wasted.

Try to Answer: • How many bytes are taken by a MyString object based on this implementation? • How many bytes are wasted? • How many bytes are you using for your real data? • How many overhead bytes are you considering?

3. The default constructor of this class creates a null string object.

4. Overload the constructor to make it possible to create a string object initialized from a C-String.

5. Define a Copy Constructor to create a MyString object initialized by another MyString object.

6. Define a destructor for this class to release the dynamically-allocated memory.

7. Define a member function to return the length (i.e. the number of characters) of the MyString object.

8. Define a member function to return a C-String representation of the MyString object.

9. Define a member function to reverse the order of the characters included in a MyString object. This function modifies the object.

10. Define a member function to check if a MyString object is null.

11. Overload the equality operator (i.e. “==”) to be able to check the equality of a MyString object with another MyString Object or a C-String.

12. Overload the subscript operator (i.e. “[]”) to return a character stored at a specific index of the string of a MyString object.

13. Overload the addition operator (i.e. “+”) to concatenate a C-String or another MyString object to an existing MyString object.

14. Overload the stream insertion and extraction operators (i.e. “<<” and “>>”) to send and receive a MyString object from an output and input stream.

NOTE Separate the implementation of your class from its specification by organizing this code in two files: a header file including the class specification and a cpp file including the class implementation.

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


MyString.h
------

#ifndef MyString_h
#define MyString_h

#include <iostream>
using namespace std;
class MyString{
private:
char *str;
int len;
public:
MyString();//default constructor
MyString(char *s); //parameterized construtor to initialize from C string
MyString(const MyString& s2); //copy constructor
~MyString(); //destructor
int length() const; //return length of this string
const char * getStr() const; //return C-String representation
void reverse(); //reverse the string
bool isEmpty(); //to check if null
char operator[](int index);//overlaoded [] operator
MyString operator+(const MyString& s2); //concatenate the strings
friend istream& operator >> (istream& in, MyString& s);
friend ostream& operator << (ostream& out, const MyString& s);

};
#endif /* MyString_h */

MyString.cpp
-------

#include "MyString.h"
#include <cstring>
MyString::MyString()//default constructor
{
str = NULL;
len = 0;
}
MyString::MyString(char *s) //parameterized construtor to initialize from C string
{
len = strlen(s);
str = new char[len];
for(int i = 0; i < len; i++)
str[i] = s[i];
}
MyString::MyString(const MyString& s2) //copy constructor
{
len = s2.len;
str = new char[len];
for(int i = 0; i < len; i++)
str[i] = s2.str[i];
}
MyString::~MyString() //destructor
{
delete []str;
}
int MyString::length() const //return length of this string
{
return len;
}
const char * MyString::getStr() const //return C-String representation
{
return str;
}
void MyString::reverse() //reverse the string
{
char temp;

for(int i = 0, j = len-1; i<j; i++, j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
bool MyString::isEmpty() //to check if null
{
return len == 0;
}
char MyString::operator[](int index)//overlaoded [] operator
{
return str[index];
}
MyString MyString::operator+(const MyString& s2) //concatenate the strings
{
int newLen = len + s2.len;
MyString result;
int i, j;

result.str = new char[newLen];
result.len = newLen;

for(i = 0;i < len; i++)
result.str[i] = str[i];

for(j = 0; j < s2.len; j++)
result.str[i++] = s2.str[j];

return result;
}

istream& operator >> (istream& in, MyString& s){
char temp[512];
in.getline(temp, 512);
s.len = strlen(temp);
if(s.str != NULL)
delete []s.str;
s.str = new char[s.len];
for(int i =0 ;i < s.len; i++)
s.str[i] = temp[i];
return in;
}

ostream& operator << (ostream& out, const MyString& s){
for(int i = 0; i < s.length(); i++)
out << s.str[i];

return out;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote