Question: I've completed most of the operators except =, ==, and !=. Those are the only ones that need to be completed. For each of your
I've completed most of the operators except =, ==, and !=. Those are the only ones that need to be completed.
For each of your classes overload the following operators
+
-
*
/
=
==
!=
Each of these operators should take as an argument a class of the base type.
Note, the = and == operator should be overloaded so that they take both a primitive and a class type.
In your menu, use the code you created in the previous assignment and simply swap out the functions for the operator overloads.
Double.h
#ifndef DOUBLE
#define DOUBLE
#include "Integer.h"
#include
class Double
{
private:
double data;
public:
void equals(double d);
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);
//Operator Overloads
Double operator + (const Double &d);
Double operator - (const Double &d);
Double operator * (const Double &d);
Double operator / (const Double &d);
};
#endif
Integer.h
#ifndef INTEGER
#define INTEGER
#include
class Integer
{
private:
int data;
public:
void equals(int i);
const int& Data() const { return data; }
friend std::istream &operator>>(std::istream &is, Integer& m);
Integer add(const Integer &i);
Integer sub(const Integer &i);
Integer mul(const Integer &i);
Integer div(const Integer &i);
int toInt() const;
// Overloaded Functions
Integer add(int i);
Integer sub(int i);
Integer mul(int i);
Integer div(int i);
// Constructors
Integer();
Integer(int i);
Integer(const Integer &i);
//Overloaded Operators
Integer operator + (const Integer &i);
Integer operator - (const Integer &i);
Integer operator * (const Integer &i);
Integer operator / (const Integer &i);
};
#endif
Menu.h
#ifndef MENU
#define MENU
#include
#include
#include
#include
const int MAX_COUNT = 20;
//struct
struct menuItem
{
void(*func)();
std::string description;
};
class Menu
{
private: //data section
menuItem mi[MAX_COUNT];
int count;
void runSelection();
public:
//constructors
Menu() : count(0) {};
//mutators
void addMenu(std::string description, void(*f)(void));
//accessors
void runMenu();
void waitKey();
};
#endif
Double.cpp
#include
#include "Double.h"
using namespace std;
// Constructors
Double::Double()
{
this->equals(0);
}
Double::Double(double d)
{
this->equals(d);
}
Double::Double(const Double &d)
{
this->equals(d.toDouble());
}
Double::Double(const Integer &i)
{
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;
}
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);
}
Integer.cpp
#include
#include "Integer.h"
using namespace std;
//Constructors
Integer::Integer()
{
this->equals(0);
}
Integer::Integer(int i)
{
this->equals(i);
}
Integer::Integer(const Integer &i)
{
this->equals(i.toInt());
}
//Overloaded Functions
Integer Integer::add(int i)
{
return Integer(this->toInt() + i);
}
Integer Integer::sub(int i)
{
return Integer(this->toInt() - i);
}
Integer Integer::mul(int i)
{
return Integer(this->toInt() * i);
}
Integer Integer::div(int i)
{
return Integer(this->toInt() / i);
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
void Integer::equals(int i)
{
this->data = i;
}
int Integer::toInt() const
{
return this->data;
}
Integer Integer::add(const Integer &i)
{
Integer tmp;
tmp.equals(this->toInt() + i.toInt());
return tmp;
}
Integer Integer::sub(const Integer &i)
{
Integer tmp;
tmp.equals(this->toInt() - i.toInt());
return tmp;
}
Integer Integer::mul(const Integer &i)
{
Integer tmp;
tmp.equals(this->toInt() * i.toInt());
return tmp;
}
Integer Integer::div(const Integer &i)
{
Integer tmp;
tmp.equals(this->toInt() / i.toInt());
return tmp;
}
istream& operator>>(istream& is, Integer& m)
{
is >> m.data;
return is;
}
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);
}
main.cpp
#include
#include
#include
#include "Double.h"
#include "Integer.h"
#include "Menu.h"
using namespace std;
Menu m;
Double du;
void doubleAdd();
void doubleSub();
void doubleMul();
void doubleDiv();
void intAdd();
void intSub();
void intMul();
void intDiv();
void Exit();
int main()
{
m.addMenu("1. Add Doubles.", doubleAdd);
m.addMenu("2. Sub Doubles.", doubleSub);
m.addMenu("3. Mul Doubles.", doubleMul);
m.addMenu("4. Div Doubles.", doubleDiv);
m.addMenu("5. Add Integers.", intAdd);
m.addMenu("6. Sub Integers.", intSub);
m.addMenu("7. Mul Integers.", intMul);
m.addMenu("8. Div Integers.", intDiv);
m.addMenu("9. Exit.", Exit);
m.runMenu();
return 0;
}
void doubleAdd()
{
Double d, d2, d3;
cout << "Enter your first double." << endl;
cin >> d;
cout << "Enter your second double." << endl;
cin >> d2;
d3 = d + d2;
cout << d3.toDouble() << endl;
m.waitKey();
}
void doubleSub()
{
Double d, d2, d3;
cout << "Enter your first double." << endl;
cin >> d;
cout << "Enter your second double." << endl;
cin >> d2;
d3 = d - d2;
cout << d3.toDouble() << endl;
m.waitKey();
}
void doubleMul()
{
Double d, d2, d3;
cout << "Enter your first double." << endl;
cin >> d;
cout << "Enter your second double." << endl;
cin >> d2;
d3 = d * d2;
cout << d3.toDouble() << endl;
m.waitKey();
}
void doubleDiv()
{
Double d, d2, d3;
cout << "Enter your first double." << endl;
cin >> d;
cout << "Enter your second double." << endl;
cin >> d2;
d3 = d / d2;
cout << d3.toDouble() << endl;
m.waitKey();
}
void intAdd()
{
Integer i, i2, i3;
cout << "Enter your first Integer." << endl;
cin >> i;
cout << "Enter your second Integer." << endl;
cin >> i2;
i3 = i + i2;
cout << i3.toInt() << endl;
m.waitKey();
}
void intSub()
{
Integer i, i2, i3;
cout << "Enter your first Integer." << endl;
cin >> i;
cout << "Enter your second Integer." << endl;
cin >> i2;
i3 = i - i2;
cout << i3.toInt() << endl;
m.waitKey();
}
void intMul()
{
Integer i, i2, i3;
cout << "Enter your first Integer." << endl;
cin >> i;
cout << "Enter your second Integer." << endl;
cin >> i2;
i3 = i * i2;
cout << i3.toInt() << endl;
m.waitKey();
}
void intDiv()
{
Integer i, i2, i3;
cout << "Enter your first Integer." << endl;
cin >> i;
cout << "Enter your second Integer." << endl;
cin >> i2;
i3 = i / i2;
cout << i3.toInt() << endl;
m.waitKey();
}
void Exit()
{
cout << "Thanks for using the calculator." << endl;
exit(0);
}
Menu.cpp
#include "Menu.h"
#include
#include
#include
void Menu::runSelection()
{
int select;
std::cin >> select;
if (select <= this->count)
this->mi[select - 1].func();
}
void Menu::addMenu(std::string description, void(*f)(void))
{
if (this->count < MAX_COUNT)
{
this->mi[count].func = f;
this->mi[count].description = description;
count++;
}
}
void Menu::runMenu()
{
for (;;)
{
system("CLS");
for (unsigned int pos = 0; pos < this->count; pos++)
std::cout << this->mi[pos].description << std::endl;
runSelection();
}
}
void Menu::waitKey()
{
std::cout << "Press any key to continue" << std::endl;
while (!_kbhit());
std::fflush(stdin);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
