Can you please help me with this it\'s for C++ ! Will give thumbs up! ******* SA
ID: 3915102 • Letter: C
Question
Can you please help me with this it's for C++ ! Will give thumbs up! ******* SAMPLE.CPP FILE IS BELOW!!!!! *********
************************************** SAMPLE.CPP FILE BELOW **********************************************
#include <iostream>
#include "MyString.h"
using namespace std;
int main(){
//(1)
cout << endl << "Testing Default ctor" << endl;
MyString ms_default;
//(2)
cout << endl << "Testing Parametrized ctor" << endl;
MyString ms_parametrized("MyString parametrized constructor!");
//(3)
cout << endl << "Testing Copy ctor" << endl;
MyString ms_copy(ms_parametrized);
//(4)
cout << endl << "Testing Dynamic Allocation with new / Deallocation with
delete expressions" << endl;
MyString* ms_Pt = new MyString("MyString to be deletedâ
¦");
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("The first part");
MyString ms_append2(" and the second");
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;
}
Objectives: The main objectives of this project are to test your ability to create and use dynamic Description: For this project you will create your own String class. You may use square bracket-indexing. pointers, references, all operators, as well as the or library functions (however the std::string type is still not allowed) The following header file extract gives the required specifications for the class //Necessary preprocessor #define (s) //Necessary include(s) //You can include or //class specification class Mystringt public MyString) Mystring (const char str)i MyString (const Mystring& other)i MyString); 17(2) 1/(3)Explanation / Answer
here is your file : ----------->>>>>>>>>
MyString.h : ------------->>>>>>>>>>>>
#ifndef MYSTRING__H__
#define MYSTRING__H__
#include<iostream>
#include<cstring>
using namespace std;
class MyString{
public:
MyString();
MyString(const char* str);
MyString(const MyString& other);
~MyString();
size_t size()const;
size_t length()const;
const char* c_str()const;
bool operator==(const MyString& other)const;
MyString& operator=(const MyString& other);
MyString operator+(const MyString& other)const;
char& operator[](size_t index);
const char& operator[](size_t index)const;
friend ostream& operator<<(ostream& os,const MyString& other);
private:
void buffer_deallocate();
void buffer_allocate(size_t size);
char *m_buffer;
size_t m_size;
};
MyString::~MyString(){
buffer_deallocate();
}
void MyString::buffer_deallocate(){
if(m_buffer == nullptr){
m_size = 0;
return;
}
delete[] m_buffer;
m_size = 0;
}
void MyString::buffer_allocate(size_t size){
if(m_buffer != nullptr){
buffer_deallocate();
}
m_buffer = new char[size];
m_size = size;
}
ostream& operator<<(ostream& os,const MyString& other){
for(int i = 0;i<other.length();i++){
os<<other.c_str()[i];
}
return os;
}
const char& MyString::operator[](size_t index)const{
return m_buffer[index];
}
char& MyString::operator[](size_t index){
return m_buffer[index];
}
const char* MyString::c_str()const{
return m_buffer;
}
bool MyString::operator==(const MyString& other)const{
if(this == &other){
return true;
}
if(strcmp(m_buffer,other.m_buffer) == 0){
return true;
}
return false;
}
size_t MyString::length()const{
return strlen(m_buffer);
}
size_t MyString::size()const{
return m_size;
}
MyString::MyString(){
m_buffer = nullptr;
m_size = 0;
}
MyString::MyString(const MyString& other):MyString(){
*this = other;
}
MyString::MyString(const char *str):MyString(){
buffer_allocate(strlen(str));
strcpy(m_buffer,str);
}
MyString MyString::operator+(const MyString& other)const{
MyString m(*this);
m.buffer_allocate(m.size()+other.size());
strcat(m.m_buffer,other.m_buffer);
return m;
}
MyString& MyString::operator=(const MyString& other){
if(this == &other){
return *this;
}
if(m_buffer != nullptr){
buffer_deallocate();
}
buffer_allocate(other.size());
strcpy(m_buffer,other.m_buffer);
return *this;
}
#endif