Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

AT&T; 10:36 AM 84%-. CS202_Project_X.pdf CS 202.1001 Computer Science II Done Ob

ID: 3702240 • Letter: A

Question

AT&T; 10:36 AM 84%-. CS202_Project_X.pdf CS 202.1001 Computer Science II Done Objectives: The main objectives of this project are to review and strengthen your ability to create and use dynamic memory wrapped in classes (and also to ameliorate your Grade! Description: For this project you will create your own SmartPtr (Smart Pointer) class. A Smart Pointer serves the purpose of wrapping a set of useful behaviors around a common Raw Pointer, such as: i. Automatically handle allocation of Dynamic Memory if necessary, when a SmartPointer ii. Automatically handle deallocation of Dynamic Memory if appropriate, when a SmartPointer ii. Provide access to the Dynamic Memory it encapsulates (via the actual Raw Pointer) using iv. Automatically handle cases such as a) when a Smart Pointer is used to point to the data object is created. object lifetime ends. the same notation (the same operators) as a Raw Pointer, so that it is exactly as easy to use. already allocated by another SmartPointer, and avoid re-allocation, or b) when a SmartPointer's lifetime ends but there also exists another SmartPointer data, and avoid deallocating early (understand when the last SmartPointer corresponding to that memory is destroyed, and only then delete the data). pointing to the same The following header file extract gives the required specifications for the class: //Necessary preprocessor #define(s) //Necessary include (s) //class specification class SmartPtrf public /7(2) SmartPtr()i SmartPtrDataTypedata); SmartPtr( const SmartPtr &other;) -SmartPtr SmartPtr &operator-; const SmartPtr& rhs ) DataType & operator DataTypeoperator-)i 11 (3) /7(4) /7 (5) 17 (6) 17 (7) private: size t mrefcount; DataTypem ptri /7(9)

Explanation / Answer

ANSWER:

#include "DataType.h"

#include <cstdlib>

#include "ArrayStack.h"

#include "nodestack.h"

DataType::DataType(){

m_intVal = 0;

m_doubleVal = 0.0;

}

DataType::DataType(int intVal, double doubleVal){

m_intVal = intVal;

m_doubleVal = 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;

}