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_
Please help! Comments added in would be a great help. Thank you! Input file T-Mobile Wi-Fi...
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 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 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 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 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 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 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 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 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 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...