Question
Please help! Comments added in would be a great help. Thank you!
T-Mobile Wi-Fi 10:33 AM * 85% Objectives: The main objectives of this project are to test your ability to create and use dynamic memory, and to review your knowledge to manipulate classes, pointers and iostream to all extents For this project you will create your own String class. You may use square bracket-indexing pointers, references, all operators, as well as the <string.h> or <estring library functions however the std::atring type is still not allowed) The following header file extract gives the required specifications for the class: /Necessary preprocessoe Idefine(s) / /Necessary include(s) You can include ccstring> or <string.h //class specification class Hystringt publicr 1(3) 1(2) Mystring (const char* str) Mystring (const Mystring& other)i HystringO 143) sise t siseo const size t lengtho const const charstro const /F(s) 1(6) 1167) bool operator(const Mystring& other) const MyString& operators (const Hystrin 96 ) Mystring operators (const Hystringi otber ryStr) const 10) char& operatorl1 (size t index) const ehars operatorll (size t index) comst 748) 749) (31a) 1/(11b) friend ostreans operatorce(ostreams os, const Hystring& ystr) 12) private void buffer deallocateO void buffer allocate(size t size) char buffer size t size 7(33) 7(a4) The MyString Class will contain the following private data members m beffer, a chan-type pointer, pointing to the Dynamically Allocated data. Note: This is no longer a static array. Dynamie Memory management has to guarantee that it points to a properly allocated memory region, otherwise Segmentation Faults will occur in your program. Also, Dynamic Memory management has to guarantce that it is properly deallocated when appropriate, and when its size has to chamge r m size,asize t member, denoting how many characters are currently allocated for m buffer. Noe that this has to be properly initialized and updated each time the dynamically allocated tmemoy is changad will have the folowing private helper methodsa (13) buffer deallocate - will deallocate the dynamic memory pointed to by m buffet Note: The m size which keeps track of m buffer has to be updated too. (14) buffer allocate-will allocate the required size t size number of char elements and point m buffer to it. It also has to check whether there is an already allocated space foe m buffer, and properly deallocate it prior to reallocating the new memory required Nole The m size which keeps track of m buffer has so be updated too. Also you should at least be checking whether the nev expression used to allocate data succeded or failed (you can check if it evaluated to & NULL valac).(Hin: You may also want to try implementing the correct way via exception handling for the dynamic memory allocation) and will have the following publik member functions
media%2Fbdb%2Fbdb39252-92c3-4841-8622-35
media%2F549%2F549ac2a5-e796-493a-bb83-13
media%2Fc53%2Fc531a65f-70e1-4247-aad7-08
Input file
media%2F7f8%2F7f8804a5-b587-4855-b7c4-12
media%2Fc26%2Fc26d7120-83e2-48fc-82ac-e6
0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp


#include <iostream>

#include "MyString.h"

using namespace std;


int main(){

//(1) tested
cout << endl << "Testing Default ctor" << endl;
MyString ms_default;
cout << "Value: " << ms_default << endl;

//(2) tested
cout << endl << "Testing Parametrized ctor" << endl;
MyString ms_parametrized("MyString parametrized constructor!");
cout << "Value: " << ms_parametrized << endl;

//(3)
cout << endl << "Testing Copy ctor" << endl;
MyString ms_copy(ms_parametrized);
cout << "Value: " << ms_copy << endl;

//(4)
cout << endl << "Testing Dynamic Allocation with new / Deallocation with delete expressions" << endl;
MyString* ms_Pt = new MyString("MyString to be deleted…");
cout << "Ms_Pt Value: " << *ms_Pt << endl;
delete ms_Pt;
ms_Pt = NULL;

//(5),(6)
cout << endl << "Testing Size and Length helper methods" << endl;
MyString ms_size_length("Size and length test");
cout << ms_size_length.size() << endl;
cout << ms_size_length.length() << endl;


//(7)
cout << endl << "Testing c_str conversion helper method" << endl;
MyString ms_toCstring("C-String equivalent successfully obtained!");
cout << ms_toCstring.c_str() << endl;

//(8)
cout << endl << "Testing Equality operator==" << endl;
MyString ms_same1("The same"), ms_same2("The same");
if (ms_same1==ms_same2)
    cout << "Equality: Same success" << endl;
MyString ms_different("The same (NOT)");
if (!(ms_same1==ms_different))
    cout << "Equality: Different success" << endl;

//(9)
cout << endl << "Testing Assignment operator=" << endl;
MyString ms_assign("MyString before assignment");
cout << ms_assign << endl;
ms_assign = MyString("MyString after performing assignment");
cout << ms_assign << endl;

//(10)
cout << endl << "Testing operator+" << endl;
MyString ms_append1("Why does this");
MyString ms_append2(" work when its invokes?");
MyString ms_concat = ms_append1+ ms_append2;
cout << ms_concat << endl;

//(11)
cout << endl << "Testing operator[]" << endl;
MyString ms_access("Access successful (NOT)");
ms_access[17] = 0;

//(12)
cout << ms_access << endl;
return 0;
}


MyString.cpp


#include "MyString.h"

//default constructor
MyString::MyString()
{
m_buffer = NULL;
buffer_allocate(0);
}

//parameterized constructor
MyString::MyString (const char * str)
{
m_buffer = NULL;
size_t len = strlen(str);
buffer_allocate(len);
std::strcpy (m_buffer, str);
}

MyString::MyString (const MyString& srcStr)
{
buffer_allocate (srcStr.m_size);
std::strcpy (m_buffer, srcStr.m_buffer);
}

/*
~MyString:
    Deallocates the memory created for a char *
*/
MyString::~MyString()
{
delete [] m_buffer;
}

void MyString::buffer_allocate (size_t size)
{
if(m_buffer != NULL)
    buffer_deallocate();
m_size = size;
m_buffer = new char [m_size + 1];
m_buffer[0] = '\0';
}

/*
buffer_deallocate:
    Deallocates the memory pointed to
    by the m_buffer member and updates
    the size to zero.
*/
void MyString::buffer_deallocate()
{
    if(m_buffer != NULL)
    {
      delete [] m_buffer;
      m_buffer = NULL;
      m_size = 0;
    }
    else
      return;
}

/*
size:
    Returns the size of the buffer in bytes.
*/
size_t MyString::size () const
{
return (sizeof(m_buffer));
}

/*
length:
    returns the length of the buffer as a valid data semantic
*/
size_t MyString::length() const
{
size_t len = std::strlen(m_buffer);
return len;
}

const char* MyString::c_str() const
{
if(m_buffer)
    return m_buffer;
else
{
    char * cstr = new char [1];
    cstr[0] = '\0';
    return cstr;
}

}

/*
operator==:
    Checks if the calling object represents the same string as another
    MyString object, and return true (or false) respectively.
*/
bool MyString::operator== (const MyString& srcStr) const
{
return (std::strcmp(m_buffer, srcStr.m_buffer) == 0);
}

/*
opertor=:
    assign a new value to the calling object's string data
    based on the data passed as a parameter.
    Reference is returned for cascading.
*/
MyString& MyString::operator= (const MyString& srcStr)
{
//if this is the same object, just return it
if(this == &srcStr)
    return *this;
//if there is a value in the buffer, delete it
buffer_deallocate();
//initializing the contents of the srcStr
buffer_allocate(srcStr.m_size);
std::strcpy(m_buffer, srcStr.m_buffer);
return *this;
}

/*
operator+:
    concatenates C-string equivalent data of srcStr object
    and returns it by value, invoking the copy constructor.
*/
MyString MyString::operator+ (const MyString& srcStr) const
{
//create temporary object
MyString temp;
//store the total size of all the buffer sizes
temp.m_size = m_size + srcStr.m_size;
//allocate memory to hold the total size of all elements
temp.m_buffer = new char [temp.m_size + 1];
//copy the calling objects data into the newly allocated temp
std::strcpy(temp.m_buffer, m_buffer);
//concatenate the srcStr to the newly allocated buffer
std::strcat(temp.m_buffer, srcStr.m_buffer);
//return the object created (invokes the copy constructor).
return temp;
}

/*
operator[]:
    by reference accessing of a specific character at index of size_t
    within the allocated m_buffer. This allows access to MyString data
    by reference, with read/write access.
*/
char& MyString::operator[] (size_t index)
{
return m_buffer[index];
}

/*
operator[]:
    by reference access of a specific character at index
    but with read-only priviliges.
*/
const char& MyString::operator[] (size_t index) const
{
return m_buffer[index];
}

/*
operator<<
    Friend operator that writes the values of the MyString object
    to the calling routine requesting.
*/
std::ostream& operator<<(std::ostream& os, const MyString& myStr)
{
size_t i = 0;
while(i < myStr.m_size)
{
    os << myStr.m_buffer[i++];
}
os << std::endl << "Length: " << myStr.m_size;
return os;
}


MyString.h

#ifndef MYSTRING_H_
#define MYSTRING_H_

#include <iostream>
#include <cstring>

const int MAX_BUF = 255;

class MyString{

public:
    MyString();
    MyString(const char* srcStr);
    MyString(const MyString& srcStr);
    ~MyString();

    size_t size() const;
    size_t length() const;
    const char* c_str() const;

    bool operator== (const MyString& srcStr) const;
    MyString& operator= (const MyString& srcStr);
    MyString operator+ (const MyString& srcStr) const;
    char& operator[] (size_t index);
    const char& operator[] (size_t index) const;

friend std::ostream& operator<<(std::ostream& os, const MyString& myStr);

private:
    void buffer_deallocate();
    void buffer_allocate(size_t size);

    char * m_buffer;
    size_t m_size;
};

#endif //MYSTRING_H_


Add a comment
Know the answer?
Add Answer to:
Please help! Comments added in would be a great help. Thank you! Input file T-Mobile Wi-Fi...
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
  • 2. (50 marks) A string in C++ is simply an array of characters with the null...

    2. (50 marks) A string in C++ is simply an array of characters with the null character(\0) used to mark the end of the string. C++ provides a set of string handling function in <string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard Template Library), C++ now provides a string class. But for this assignment, you are to develop your own string class. This will give you a chance to develop and work with...

  • 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...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG can be instantiated. 2. To do so, we must override the pure virtual function clone() in the base class. • The class declaration is given below. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

  • 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...

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

  • I need help solving this question from the practice final exam given to us to prepare...

    I need help solving this question from the practice final exam given to us to prepare for the final in C++ #include <iostream> using namespace std; /* The following is code for an OrderedCollection container and its related iterator. The container has a capacity determined by a constructor parameter. The container does not grow. Code that adds elements to the container ensures that the capacity of the container is never exceeded. An attempt to add an item to a full...

  • Code in c++ please. Does not have to be complete, just write code for the ones you know. Thank y...

    Code in c++ please. Does not have to be complete, just write code for the ones you know. Thank you. template <typename T> class smart_ptr { public: smart_ptr(); // Create a smart_ptr that is initialized to nullptr. The reference count // should be initialized to nullptr. explicit smart_ptr(T* raw_ptr); // Create a smart_ptr that is initialized to raw_ptr. The reference count // should be one. smart_ptr(const smart_ptr& rhs); // Copy construct a pointer from rhs. The reference count should be...

  • In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 st...

    In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 standard input (stdin) 1 standard output (stdout) 2 standard error (stderr) By default, the command interpreter (shell) reads keyboard input from file descriptor 0 (stdin) and writes output to file descriptor 1 (stdout), which appears on the screen. As you explored in Lab 2, input/output can be...

  • what would be the solution code to this problem in c++? The Problem Write program that...

    what would be the solution code to this problem in c++? The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of...

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