Question: C++ Level 2 Assignment : CSIS Lab 8 - Namespaces, Static Variables 1. Create a namespace for your project. You can use your name, or

C++ Level 2

Assignment:

CSIS Lab 8 - Namespaces, Static Variables

1. Create a namespace for your project. You can use your name, or a factious company name if you would like. Make sure to wrap your menu, Integer, and Double classes within your namespace.

2. Currently we are using the menu in global scope which is not an ideal situation. Implement a singleton design pattern for your menu.

Here are my double, integer, and menu classes:

/*

Double.h

*/

#include "Integer.h"

#include

#ifndef DOUBLE

#define DOUBLE

class Double

{

private:

double doubleData;

bool nan;

void isNan(std::string s);

public:

//Constructors:

Double();

Double(const Double &d);

Double(double d);

Double(const Integer &i);

Double(const std::string str);

double toDouble() const;

void equals(std::string s);

void equals(double d);

Double add(const Double &d);

Double sub(const Double &d);

Double mul(const Double &d);

Double div(const Double &d);

//Primitive Functions

Double add(double &d);

Double sub(double &d);

Double mul(double &d);

Double div(double &d);

//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);

std::string toString();

bool isNan();

};

/*

Double.cpp

*/

#include"Double.h"

#include"Integer.h"

#include

#include

#include

//Overloaded Constructors

Double::Double()

:doubleData(0.0), nan(false)

{

}

Double::Double(const Double &d)

: nan(false)

{

equals(d.doubleData);

}

Double::Double(double d)

: doubleData(d), nan(false)

{

}

Double::Double(const Integer &i)

: nan(false)

{

equals((double)i.toInt());

}

Double::Double(const std::string str)

{

this->equals(str);

}

double Double::toDouble() const

{

return this->doubleData;

}

void Double::equals(std::string s)

{

this->isNan(s);

if (this->nan)

this->doubleData = 0.0;

else

this->doubleData = stod(s);

}

void Double::equals(double d)

{

doubleData = d;

}

Double Double::add(const Double &d)

{

Double ans(doubleData + d.toDouble());

return ans;

}

Double Double::sub(const Double &d)

{

Double ans(doubleData - d.toDouble());

return ans;

}

Double Double::mul(const Double &d)

{

Double ans(doubleData * d.toDouble());

return ans;

}

Double Double::div(const Double &d)

{

Double ans(doubleData / d.toDouble());

return ans;

}

//Primitive Function Definitions:

Double Double::add(double &d)

{

Double ans;

ans.equals(this->toDouble() + d);

return ans;

}

Double Double::sub(double &d)

{

Double ans;

ans.equals(this->toDouble() - d);

return ans;

}

Double Double::mul(double &d)

{

Double ans;

ans.equals(this->toDouble() * d);

return ans;

}

Double Double::div(double &d)

{

Double ans;

ans.equals(this->toDouble() / d);

return ans;

}

// Operator Overloads:

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 = (std::string s)

{

this->equals(s);

return *this;

}

bool Double::operator == (const Double &d)

{

return this->doubleData == d.doubleData;

}

bool Double::operator == (double d)

{

return this->doubleData == d;

}

std::string Double::toString()

{

std::stringstream ss;

ss << this->doubleData;

return ss.str();

}

void Double::isNan(std::string s)

{

int pos;

this->nan = false;

pos = s.find(".", 0);

if (pos != string::npos)

{

pos = s.find(".", +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;

}

bool Double::isNan()

{

return nan;

}

/*

Integer.h

*/

#include

using namespace std;

#ifndef INTEGER

#define INTEGER

class Integer

{

private:

int intData;

bool nan;

void isNan(std::string s);

public:

//Constructors:

Integer();

Integer(const Integer &i);

Integer(int i);

Integer(const std::string str);

int toInt() const;

void equals(int i);

void equals(std::string s);

Integer add(const Integer &i);

Integer sub(const Integer &i);

Integer mul(const Integer &i);

Integer div(const Integer &i);

//Primitive Constructors

Integer add(int &i);

Integer sub(int &i);

Integer mul(int &i);

Integer div(int &i);

// Overloaded Operators

Integer operator + (const Integer &i);

Integer operator - (const Integer &i);

Integer operator * (const Integer &i);

Integer operator / (const Integer &i);

Integer &operator = (const Integer &i);

Integer &operator = (int i);

Integer &operator = (std::string s);

bool operator == (const Integer &i);

bool operator == (int i);

std::string toString();

bool isNan();

};

#endif

/*

Integer.cpp

*/

#include

#include

#include

#include "Integer.h"

using namespace std;

//Overloaded Constructors:

Integer::Integer()

:intData(0), nan(false)

{

}

Integer::Integer(const Integer &i)

: nan(false)

{

equals(i.intData);

}

Integer::Integer(int i)

: intData(i), nan(false)

{

}

Integer::Integer(const std::string str)

{

this->equals(str);

}

int Integer::toInt() const

{

return this->intData;

}

void Integer::equals(int i)

{

intData = i;

}

void Integer::equals(std::string s)

{

this->isNan(s);

if (this->nan)

this->intData = 0;

else

this->intData = stod(s);

}

Integer Integer::add(const Integer &i)

{

Integer ans(this->intData + i.toInt());

return ans;

}

Integer Integer::sub(const Integer &i)

{

Integer ans(this->intData - i.toInt());

return ans;

}

Integer Integer::mul(const Integer &i)

{

Integer ans(this->intData - i.toInt());

return ans;

}

Integer Integer::div(const Integer &i)

{

Integer ans(this->intData - i.toInt());

return ans;

}

// Primitives

Integer Integer::add(int &i)

{

Integer ans;

ans.equals(this->toInt() + i);

return ans;

}

Integer Integer::sub(int &i)

{

Integer ans;

ans.equals(this->toInt() - i);

return ans;

}

Integer Integer::mul(int &i)

{

Integer ans;

ans.equals(this->toInt() * i);

return ans;

}

Integer Integer::div(int &i)

{

Integer ans;

ans.equals(this->toInt() / i);

return ans;

}

//Operator Overloads

Integer Integer::operator + (const Integer &i)

{

return this->add(i);

}

Integer Integer::operator - (const Integer &i)

{

return this->sub(i);

}

Integer Integer::operator * (const Integer &i)

{

return this->mul(i);

}

Integer Integer::operator / (const Integer &i)

{

return this->div(i);

}

Integer & Integer::operator=(const Integer & i)

{

this->equals(i.toInt());

return *this;

}

Integer & Integer::operator = (int i)

{

this->equals(i);

return *this;

}

Integer & Integer::operator = (std::string s)

{

this->equals(s);

return *this;

}

bool Integer::operator==(const Integer & i)

{

return this->intData == i.intData;

}

bool Integer::operator == (int i)

{

return this->intData = i;

}

std::string Integer::toString()

{

std::stringstream ss;

ss << this->intData;

return ss.str();

}

void Integer::isNan(std::string s)

{

int pos;

this->nan = false;

pos = s.find(".", 0);

if (pos != string::npos)

{

pos = s.find(".", +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;

}

bool Integer::isNan()

{

return nan;

}

/*

Menu.h

*/

#ifndef MENU

#define MENU

#include

#include

using std::vector;

struct menuItem

{

std::string descript;

void(*func)();

};

class Menu

{

private:

vector mI;

void run();

public:

Menu();

void addMenu(std::string Descript, void(*f)());

void runMenu();

void waitKey();

};

#endif

/*

Menu.cpp

*/

#include

#include "Menu.h"

using namespace std;

Menu::Menu()

{

//Default constructor.

}

void Menu::addMenu(std::string Descript, void(*f)())

{

menuItem m;

m.descript = Descript;

m.func = f;

mI.push_back(m); // Do more research on this.

}

void Menu::run()

{

menuItem tmp;

for (;;) // Forever loop

{

for (int i = 0; i < (int)mI.size(); i++)

{

tmp = mI.at(i);

cout << tmp.descript << endl;

}

run();

}

}

void Menu::runMenu()

{

int select;

cin >> select;

if (select <= (int)mI.size())

{

mI[select - 1].func();

}

}

void Menu::waitKey()

{

cout << "Press any key to continue..." << endl;

getchar();

cin.ignore();

}

/*

main.cpp

*/

#include

#include "Double.h"

#include "Integer.h"

#include "Menu.h"

using namespace std;

void intAdd();

void intSub();

void intMul();

void intDiv();

void doubleAdd();

void doubleSub();

void doubleMul();

void doubleDiv();

void Exit();

Menu m;

int main()

{

cout << "Welcome to the Double and Integer Menu. Select one from the menu: " << endl;

cout << " 1. Add Integer. 2. Subract Integer. 3. Multiply Integer. 4. Divide Integer. 5. Add Double. 6. Subtract Double 7. Multiply Double. 8. Divide Double 9. Exit" << endl;

m.addMenu("1. Add Integer ", intAdd);

m.addMenu("2. Subtract Integer", intSub);

m.addMenu("3. Multiply Integer ", intMul);

m.addMenu("4. Divide Integer ", intDiv);

m.addMenu("5. Add Double ", doubleAdd);

m.addMenu("6. Subtract Double ", doubleSub);

m.addMenu("7. Multiply Double ", doubleMul);

m.addMenu("8. Divide Double ", doubleDiv);

m.addMenu("9. Exit ", Exit);

m.runMenu();

return 0;

}

/******************

Integer Menu Items:

*******************/

void intAdd()

{

int i, i2;

cout << "Enter two Integers: " << endl;

cin >> i >> i2;

Integer *x = new Integer(i);

Integer *y = new Integer(i2);

Integer z = *x + *y;

delete x, y;

cout << "Sum = " << z.toInt() << endl;

m.waitKey();

}

void intSub()

{

int i, i2;

cout << "Enter two Integers: " << endl;

cin >> i >> i2;

Integer *x = new Integer(i);

Integer *y = new Integer(i2);

Integer z = *x - *y;

delete x, y;

cout << "Difference = " << z.toInt() << endl;

m.waitKey();

}

void intMul()

{

int i, i2;

cout << "Enter two Integers: " << endl;

cin >> i >> i2;

Integer *x = new Integer(i);

Integer *y = new Integer(i2);

Integer z = *x * *y;

delete x, y;

cout << "Product = " << z.toInt() << endl;

m.waitKey();

}

void intDiv()

{

int i, i2;

cout << "Enter two Integers: " << endl;

cin >> i >> i2;

Integer *x = new Integer(i);

Integer *y = new Integer(i2);

Integer z = *x / *y;

delete x, y;

cout << "Quotient = " << z.toInt() << endl;

m.waitKey();

}

/*****************

Double Menu Items:

******************/

void doubleAdd()

{

double d, d2;

cout << "Enter two Doubles: " << endl;

cin >> d >> d2;

Double *x = new Double(d);

Double *y = new Double(d2);

Double z = *x + *y;

delete x, y;

cout << "Sum = " << z.toDouble() << endl;

m.waitKey();

}

void doubleSub()

{

double d, d2;

cout << "Enter two Doubles: " << endl;

cin >> d >> d2;

Double *x = new Double(d);

Double *y = new Double(d2);

Double z = *x - *y;

delete x, y;

cout << "Difference = " << z.toDouble() << endl;

m.waitKey();

}

void doubleMul()

{

double d, d2;

cout << "Enter two Doubles: " << endl;

cin >> d >> d2;

Double *x = new Double(d);

Double *y = new Double(d2);

Double z = *x * *y;

delete x, y;

cout << "Product = " << z.toDouble() << endl;

m.waitKey();

}

void doubleDiv()

{

double d, d2;

cout << "Enter two Doubles: " << endl;

cin >> d >> d2;

Double *x = new Double(d);

Double *y = new Double(d2);

Double z = *x / *y;

delete x, y;

cout << "Quotient = " << z.toDouble() << endl;

m.waitKey();

}

void Exit()

{

cout << "Come again! " << endl;

exit(0);

}

Thank you in advance! :)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!