Included files and file outlines: ArrayList.cpp: #include \"ArrayList.h\" /* You
ID: 3707850 • Letter: I
Question
Included files and file outlines:
ArrayList.cpp:
#include "ArrayList.h"
/* Your implementation here */
ArrayList.h:
#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_
#include "DataType.h"
class ArrayList{
friend std::ostream & operator<<(std::ostream & os, //(i)
const ArrayList & arrayList);
public:
ArrayList(); //(1)
ArrayList(size_t count, const DataType & value); //(2)
ArrayList(const ArrayList & other); //(3)
~ArrayList(); //(4)
ArrayList & operator= (const ArrayList & rhs); //(5)
DataType * front(); //(6)
DataType * back(); //(7)
DataType * find(const DataType & target, //(8)
DataType * & previous,
const DataType * start = NULL);
DataType * insertAfter(const DataType & target, //(9)
const DataType & value);
DataType * insertBefore(const DataType & target, //(10)
const DataType & value);
DataType * erase(const DataType & target); //(11)
DataType & operator[] (size_t position); //(12)
size_t size() const; //(13)
bool empty() const; //(14)
void clear(); //(15)
private:
void resize(size_t count); //(16)
DataType * m_array;
size_t m_size;
size_t m_maxsize;
};
#endif //ARRAYLIST_H_
DataType.cpp:
#include "DataType.h"
#include <cstdlib>
DataType::DataType()
: m_intVal(0),
m_doubleVal(0)
{
}
DataType::DataType(int intVal, double doubleVal)
: m_intVal(intVal),
m_doubleVal(doubleVal)
{
}
DataType::DataType(const DataType & other)
: m_intVal(other.m_intVal),
m_doubleVal(other.m_doubleVal)
{
}
bool DataType::operator==(const DataType& rhs) const{
return m_intVal==rhs.m_intVal && m_doubleVal==rhs.m_doubleVal;
}
DataType& DataType::operator=(const DataType& rhs){
if (this != &rhs){
m_intVal = rhs.m_intVal;
m_doubleVal = rhs.m_doubleVal;
}
return *this;
}
int DataType::getIntVal() const{
return m_intVal;
}
void DataType::setIntVal(int i){
m_intVal = i;
}
double DataType::getDoubleVal() const{
return m_doubleVal;
}
void DataType::setDoubleVal(double d){
m_doubleVal = d;
}
std::ostream & operator<<(std::ostream& os, const DataType & dt){
os << "{" << dt.m_intVal << "," << dt.m_doubleVal << "}";
return os;
}
std::istream & operator>>(std::istream & is, DataType & dt){
char in_buf[255];
is >> in_buf;
dt.m_doubleVal = atof(in_buf);
dt.m_intVal = (int)dt.m_doubleVal;
dt.m_doubleVal -= dt.m_intVal;
return is;
}
DataType.h:
#ifndef DATATYPE_H_
#define DATATYPE_H_
#include <iostream>
class DataType{
friend std::ostream & operator<<(std::ostream & os, const DataType & dataType);
friend std::istream & operator>>(std::istream & is, DataType & dataType);
public:
DataType();
DataType(int intVal, double doubleVal);
DataType(const DataType & other);
bool operator==(const DataType & rhs) const;
DataType & operator= (const DataType & rhs);
int getIntVal() const;
void setIntVal(int i);
double getDoubleVal() const;
void setDoubleVal(double d);
private:
int m_intVal;
double m_doubleVal;
};
#endif //DATATYPE_H_
NodeList.cpp:
#include "NodeList.h"
/* Your implementation here */
NodeLIst.h:
#ifndef NODELIST_H_
#define NODELIST_H_
#include "DataType.h"
class Node{
friend class NodeList; //allows direct accessing of link and data from class NodeList
public:
Node() :
m_next( NULL )
{
}
Node(const DataType& data, Node * next = NULL) :
m_next( next ),
m_data( data )
{
}
Node(const Node & other) :
m_next( other.m_next ),
m_data( other.m_data )
{
}
DataType & getData(){ //gets non-const reference, can be used to modify value of underlying data
//return const_cast<DataType &>(static_cast<const Node &>(*this).getData()); //an alternative implementation, just for studying reference
return m_data;
}
const DataType & getData() const{ //gets const reference, can be used to access value of underlying data
return m_data;
}
private:
Node * m_next;
DataType m_data;
};
class NodeList{
friend std::ostream & operator<<(std::ostream & os, //(i)
const NodeList & nodeList);
public:
NodeList(); //(1)
NodeList(size_t count, const DataType & value); //(2)
NodeList(const NodeList & other); //(3)
~NodeList(); //(4)
NodeList & operator= (const NodeList & rhs); //(5)
Node * front(); //(6)
Node * back(); //(7)
Node * find(const DataType & target, //(8)
Node * & previous,
const Node * start = NULL);
Node * insertAfter(const DataType & target, //(9)
const DataType & value);
Node * insertBefore(const DataType & target, //(10)
const DataType & value);
Node * erase(const DataType & target); //(11)
DataType & operator[] (size_t position); //(12a)
const DataType & operator[] (size_t position) const; //(12b)
size_t size() const; //(13)
bool empty() const;
Objectives Te main objectives of this project are to test your ability to create and use list-based dynamic data structures. Aeview of your knowledge to manipulate dynamic memory, classes, pointers and iostream to all extents, is also included. You may from now on freely use square bracket-indexing, pointers, references, all operators, the ecstring> library, and the std: string type as you deem proper For this project you will create List classes. There will have to be two separate List-based implementations, one Array-based, and the other Node-based. Array-based List: The following beader file extract is used to explain the required specifcations for the class (the actual header Array List.h file is provided and accompanies the Project description): class ArrayList friend std::ostream &operator;Explanation / Answer
#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.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_
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);
}
/*
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& 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;
}
/*
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] = '';
}
/*
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;
}
/*
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] = '';
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;
}