Question
DataType.cpp: #include DataType.h #include 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.cpp:
#include "DataType.h"
#include
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
os
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
class DataType{
friend std::ostream & operator
dataType);
friend std::istream & operator>>(std::istream & is, DataType & dataType);
public:
DataType();
DataType(int intVal, double doubleVal);
bool operator==(const DataType & other_dataType) const;
DataType & operator= (const DataType & other_dataType);
int getIntVal() const;
void setIntVal(int i);
double getDoubleVal() const;
void setDoubleVal(double d);
private:
int m_intVal;
double m_doubleVal;
};
#endif //DATATYPE_H_
SmartPtr.h
#ifndef SMARTPTR_H_
#define SMARTPTR_H_
#include "DataType.h"
class SmartPtr {
public:
SmartPtr( );
SmartPtr( DataType * data );
SmartPtr( const SmartPtr & other );
~SmartPtr( );
SmartPtr & operator=( const SmartPtr & rhs );
DataType & operator*( );
DataType * operator->( );
private:
size_t * m_refcount;
DataType * m_ptr;
};
#endif //SMARTPTR_H_
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 object is created Automatically handle deallocation of Dynamic Memory if appropriate, when a SmartPointer object lifetime ends il. Provide access to the Dynamic Memory it encapsulates (via the actual Raw Pointer) using the same notation (the same operators) as a Raw Pointer, so that it is exactly as easy to use. Automatically handle cases such as a) when a Smart Pointer is used to point to the data already allocated by another SmartPointer, and avoid re-allocation, or b) when a SmartPointer's lifetime ends but there also exists another SmartPointer pointing to the same data, and avoid deallocating early (understand when the last SmartPointer corresponding to tha iv. t memory is destroyed, and only then delete the data) The following header file extract gives the required specifications for the class //Necessary preprocessor #define(s) //Necessary include (s) //Class specification class SmartPtr{ public: SmartPtr; SmartPtr DataType *data ); SmartPtr Const SmartPtr &other ); -SmartPtr SmartPtr & operator-const SmartPtr& rhs) DataType & operator*) DataType operator->( private: size t * m refcount; DataType * m ptr; 1Step 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