Question
i dont understand whats wrong .... //main.cpp #include #include MyString.h using namespace std; int main(){ //(1) tested cout < < endl < < Testing Default
i dont understand whats wrong ....
//main.cpp
#include
#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");
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"
#include
#include
using namespace std;
const int MAX_BUF = 255;
//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);
strcpy (m_buffer, str);
}
/*
copy constructor:
Takes in an object and calls the buffer_allocate()
Then the value in the srcStr buffer is copied to the
newly allocated m_buffer to ensure a sucessful
copy is completed
*/
MyString::MyString (const MyString & other_myStr){
buffer_allocate (other_myStr.m_size);
strcpy (m_buffer, other_myStr.m_buffer);
}
/*
~MyString:
Deallocates the memory created for a char *
*/
MyString::~MyString(){
delete [] m_buffer;
}
/*
buffer_allocate:
Takes a size passed in and initializes the m_buffer
to be of length size. After the first value is set to NULL
to ensure that a valid value exists to prevent memory leaks
*/
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 = strlen(m_buffer);
return len;
}
/*
c_str:
Returns a c_string of the current object to the calling client.
NULL terminated. This implementation will return m_buffer if it is
a non-null result. Otherwise a zero allocated buffer is created and
will be returned.
*/
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 & other_myStr) const{
return (strcmp(m_buffer, other_myStr.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 & other_myStr){
//if this is the same object, just return it
if(this == &other_myStr)
return *this;
//if there is a value in the buffer, delete it
buffer_deallocate();
//initializing the contents of the srcStr
buffer_allocate(other_myStr.m_size);
strcpy(m_buffer, other_myStr.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 & other_myStr) const{
//create temporary object
MyString temp;
//store the total size of all the buffer sizes
temp.m_size = m_size + other_myStr.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
strcpy(temp.m_buffer, m_buffer);
//concatenate the srcStr to the newly allocated buffer
strcat(temp.m_buffer, other_myStr.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 << endl << "Length: " << myStr.m_size;
return os;
}
//MyString.h
#ifndef MYSTRING_H_
#define MYSTRING_H_
#include
class MyString{
public:
MyString(); //(1)
MyString(const char * str); //(2)
MyString(const MyString & other_myStr); //(3)
~MyString(); //(4)
size_t size() const; //(5)
size_t length() const; //(6)
const char* c_str() const; //(7)
bool operator== (const MyString & other_myStr) const; //(8)
MyString & operator= (const MyString & other_myStr); //(9)
MyString operator+ (const MyString & other_myStr) const; //(10)
char & operator[] (size_t index); //(11a)
const char & operator[] (size_t index) const; //(11b)
friend std::ostream & operator<<(std::ostream & os, const MyString & myStr); //(12)
private:
void buffer_deallocate(); //(13)
void buffer_allocate(size_t size); //(14)
char * m_buffer;size_t m_size;
};
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started