Dynamic Memory-C++ Inside main.cpp and anywhere you have used the menu replace t
ID: 3727748 • Letter: D
Question
Dynamic Memory-C++
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:
Please make sure to check that the memory was allocated correctly. In addition don't forget you need to use the arrow operator.
My Code
Main.cpp
#include <iostream>
#include "Double.h"
#include "Integer.h"
#include "menu.h"
using namespace std;
void intAdd();
void intSub();
void intMul();
void intDiv();
void dblAdd();
void dblSub();
void dblMul();
void dblDiv();
void Exit();
Menu m;
int main()
{
m.addMenu("1. Integer Add ", intAdd);
m.addMenu("2. Integer Subtract", intSub);
m.addMenu("3. Integer Multiply ", intMul);
m.addMenu("4. Integer Divide ", intDiv);
m.addMenu("5. Double Add ", dblAdd);
m.addMenu("6. Double Subtract", dblSub);
m.addMenu("7. Double Multiply ", dblMul);
m.addMenu("8. Double Divide ", dblDiv);
m.addMenu("9. Exit ", Exit);
m.runMenu();
return 0;
}
void intAdd()
{
int a, b;
cout << "Enter two Integers " << endl;
cin >> a >> b;
Integer x(a);
Integer y(b);
Integer z = x.sub(y);
cout << "The difference is " << z.toInt() << endl;
m.waitKey();
}
void intSub()
{
int a, b;
cout << "Enter two Integers " << endl;
cin >> a >> b;
Integer x(a);
Integer y(b);
Integer z = x.sub(y);
cout << "The difference is " << z.toInt() << endl;
m.waitKey();
}
void intMul()
{
int a, b;
cout << "Enter two Integers " << endl;
cin >> a >> b;
Integer x(a);
Integer y(b);
Integer z = x.mul(y);
cout << "The product is " << z.toInt() << endl;
m.waitKey();
}
void intDiv()
{
int a, b;
cout << "Enter two Integers " << endl;
cin >> a >> b;
Integer x(a);
Integer y(b);
Integer z = x.div(y);
cout << "The quotient is " << z.toInt() << endl;
m.waitKey();
}
void dblAdd()
{
double a, b;
cout << "Enter two doubles " << endl;
cin >> a >> b;
Double x(a);
Double y(b);
Double z = x.add(y);
cout << "The sum is " << z.toDouble() << endl;
m.waitKey();
}
void dblSub()
{
double a, b;
cout << "Enter two doubles " << endl;
cin >> a >> b;
Double x(a);
Double y(b);
Double z = x.sub(y);
cout << "The difference is " << z.toDouble() << endl;
m.waitKey();
}
void dblMul()
{
double a, b;
cout << "Enter two doubles " << endl;
cin >> a >> b;
Double x(a);
Double y(b);
Double z = x.mul(y);
cout << "The product is " << z.toDouble() << endl;
m.waitKey();
}
void dblDiv()
{
double a, b;
cout << "Enter two doubles " << endl;
cin >> a >> b;
Double x(a);
Double y(b);
Double z = x.div(y);
cout << "The quotient is " << z.toDouble() << endl;
m.waitKey();
}
void Exit()
{
exit(0);
}
1
Double *d = new Double("123.45")
Explanation / Answer
#include <iostream>
#include "Double.h"
#include "Integer.h"
#include "menu.h"
using namespace std;
void intAdd();
void intSub();
void intMul();
void intDiv();
void dblAdd();
void dblSub();
void dblMul();
void dblDiv();
void Exit();
Menu m;
int main()
{
m.addMenu("1. Integer Add ", intAdd);
m.addMenu("2. Integer Subtract", intSub);
m.addMenu("3. Integer Multiply ", intMul);
m.addMenu("4. Integer Divide ", intDiv);
m.addMenu("5. Double Add ", dblAdd);
m.addMenu("6. Double Subtract", dblSub);
m.addMenu("7. Double Multiply ", dblMul);
m.addMenu("8. Double Divide ", dblDiv);
m.addMenu("9. Exit ", Exit);
m.runMenu();
return 0;
}
void intAdd()
{
int a, b;
cout << "Enter two Integers " << endl;
cin >> a >> b;
//Integer x(a);
Integer *x = Integer(a);
//Integer y(b);
Integer *y = Integer(b);
//Integer z = x.sub(y);
Integer *z = x.sub(y);
cout << "The difference is " << z.toInt() << endl;
m.waitKey();
}
void intSub()
{
int a, b;
cout << "Enter two Integers " << endl;
cin >> a >> b;
//Integer x(a);
Integer *x = Integer(a);
//Integer y(b);
Integer *y = Integer(b);
//Integer z = x.sub(y);
Integer *z = x.sub(y);
cout << "The difference is " << z.toInt() << endl;
m.waitKey();
}
void intMul()
{
int a, b;
cout << "Enter two Integers " << endl;
cin >> a >> b;
//Integer x(a);
Integer *x = Integer(a);
//Integer y(b);
Integer *y = Integer(b);
//Integer z = x.mul(y);
Integer *z = x.mul(y);
cout << "The product is " << z.toInt() << endl;
m.waitKey();
}
void intDiv()
{
int a, b;
cout << "Enter two Integers " << endl;
cin >> a >> b;
//Integer x(a);
Integer *x = Integer(a);
//Integer y(b);
Integer *y = Integer(b);
//Integer z = x.div(y);
Integer *z = x.div(y);
cout << "The quotient is " << z.toInt() << endl;
m.waitKey();
}
void dblAdd()
{
double a, b;
cout << "Enter two doubles " << endl;
cin >> a >> b;
//Double x(a);
Double *x = new Double(a);
//Double y(b);
Double *y = new Double(b);
// Double z = x.add(y);
Double *z = x.add(y);
cout << "The sum is " << z.toDouble() << endl;
m.waitKey();
}
void dblSub()
{
double a, b;
cout << "Enter two doubles " << endl;
cin >> a >> b;
//Double x(a);
Double *x = new Double(a);
//Double y(b);
Double *y = new Double(b);
//Double z = x.sub(y);
Double *z = x.sub(y);
cout << "The difference is " << z.toDouble() << endl;
m.waitKey();
}
void dblMul()
{
double a, b;
cout << "Enter two doubles " << endl;
cin >> a >> b;
//Double x(a);
Double *x = new Double(a);
//Double y(b);
Double *y = new Double(b);
Double *z = x.mul(y);
cout << "The product is " << z.toDouble() << endl;
m.waitKey();
}
void dblDiv()
{
double a, b;
cout << "Enter two doubles " << endl;
cin >> a >> b;
//Double x(a);
Double *x = new Double(a);
//Double y(b);
Double *y = new Double(b);
//Double z = x.div(y);
Double *z = x.div(y);
cout << "The quotient is " << z.toDouble() << endl;
m.waitKey();
}
void Exit()
{
exit(0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.