Question: Inside main.cpp and anywhere you have used the menu replace the creation of any Double or Integer class to use a pointer and dynamic memory.
Inside main.cpp and anywhere you have used the menu replace the creation of any Double or Integer class to use a pointer and dynamic memory. For instance:
1 | Double d = new Double("123.45") |
Please make sure to check that the memory was allocated correctly. In addition don't forget you need to use the arrow operator.
Double.h
#ifndef DOUBLE
#define DOUBLE
#include "Integer.h"
#include
#include
class Double
{
private:
double data;
bool nan;
void isNan(std::string s);
public:
bool isNan() { return this->nan; }
void equals(double d);
void equals(std::string s);
Double add(const Double &d);
Double sub(const Double &d);
Double mul(const Double &d);
Double div(const Double &d);
double toDouble() const;
friend std::istream &operator>>(std::istream &is, Double& m);
// Overloaded Functions
Double add(double d);
Double sub(double d);
Double mul(double d);
Double div(double d);
// Constructors
Double();
Double(double d);
Double(const Double &d);
Double(const Integer &i);
Double(const std::string str);
//Operator Overloads
Double operator + (const Double &d);
Double operator - (const Double &d);
Double operator * (const Double &d);
Double operator / (const Double &d);
Double &operator = (const Double&d);
Double &operator = (double d);
Double &operator = (std::string s);
bool operator == (const Double &d);
bool operator == (double d);
bool operator != (const Double &d);
bool operator != (double d);
std::string toString();
};
#endif
Double.cpp
#include
#include
#include
#include "Double.h"
#include "Integer.h"
using namespace std;
// Constructors
Double::Double()
:nan(false)
{
this->equals(0);
}
Double::Double(double d)
: nan(false)
{
this->equals(d);
}
Double::Double(const Double &d)
: nan(false)
{
this->equals(d.toDouble());
}
Double::Double(const Integer &i)
: nan(false)
{
this->equals(static_cast
}
//Overloaded Functions
Double Double::add(double d)
{
return Double(this->toDouble() + d);
}
Double Double::sub(double d)
{
return Double(this->toDouble() - d);
}
Double Double::mul(double d)
{
return Double(this->toDouble() * d);
}
Double Double::div(double d)
{
return Double(this->toDouble() / d);
}
void Double::equals(double d)
{
this->data = d;
this->nan = false;
}
void Double::equals(std::string s)
{
this->isNan(s);
if (this->nan)
{
this->data = 0.0;
}
else
{
this->data = stod(s);
}
}
double Double::toDouble() const
{
return this->data;
}
Double Double::add(const Double &d)
{
Double tmp;
tmp.equals(this->toDouble() + d.toDouble());
return tmp;
}
Double Double::sub(const Double &d)
{
Double tmp;
tmp.equals(this->toDouble() - d.toDouble());
return tmp;
}
Double Double::mul(const Double &d)
{
Double tmp;
tmp.equals(this->toDouble() * d.toDouble());
return tmp;
}
Double Double::div(const Double &d)
{
Double tmp;
tmp.equals(this->toDouble() / d.toDouble());
return tmp;
}
istream& operator>>(istream &is, Double &m)
{
is >> m.data;
return is;
}
Double Double::operator + (const Double &d)
{
return this->add(d);
}
Double Double::operator - (const Double &d)
{
return this->sub(d);
}
Double Double::operator * (const Double &d)
{
return this->mul(d);
}
Double Double::operator / (const Double &d)
{
return this->div(d);
}
Double &Double::operator = (const Double &d)
{
this->equals(d.toDouble());
return *this;
}
Double &Double::operator = (double d)
{
this->equals(d);
return *this;
}
Double &Double::operator = (string s)
{
this->equals(s);
return *this;
}
bool Double::operator == (const Double &d)
{
return this->toDouble() == d.toDouble();
}
bool Double::operator == (double d)
{
return this->toDouble() == d;
}
bool Double::operator != (const Double &d)
{
return this->toDouble() != d.toDouble();
}
bool Double::operator != (double d)
{
return this->toDouble() != d;
}
std::string Double::toString()
{
std::stringstream ss;
ss << this->data;
return ss.str();
}
void Double::isNan(string s)
{
int pos;
this->nan = false;
pos = s.find(".", 0);
if (pos != string::npos)
{
pos = s.find(".", pos + 1);
if (pos != string::npos)
{
this->nan = true;
return;
}
}
string::iterator p;
for (p = s.begin(); p < s.end(); p++)
{
if (!isdigit(*p) && *p != '.')
{
this->nan = true;
return;
}
}
return;
}
This is just a few functions, I believe I can get the rest done from here.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
