In: Computer Science
In this assignment you are going to use the menu you created in Assignment 1 to test both your Double and Integer classes. You should add functionality to the menu to allow you test the add, sub, mul, div functions for instances of both classes
You are going to have make a modification to your menu class. Currently it uses an array to hold a fixed amount of menu items. While this may be OK for most applications we want our menu to be as flexible as possible. To make this happen I want you to remove the array of menu items and replace it with a vector.
We want all of our code to be as organized as possible. You should have separate header and .cpp files for each class with your main function being in a separate file.
Because the menu is setup to take pointers to void functions you are going to have to create a layer of functionality that sits between the two. This kind of structure can be thought of as a three tier approach.
Here is a simplistic example
void doubleAdd(); int main() { m.addMenu("1. Add Doubles",doubleAdd); m.runMenu(); } void doubleAdd() { // Code to add two Double classes here. Should get input from the user. waitKey(); }
Main.cpp
#include <iostream> #include <cstdlib> #include <conio.h> #include <string> #include "Menu.h" using namespace std; void func1(); void func2(); void func3(); void Exit(); int main() { Menu m; m.addMenu("1. Function1", func1); m.addMenu("2. Function2", func2); m.addMenu("3. Function3", func3); m.addMenu("4. Exit", Exit); m.runMenu(); } void func1() { Menu m; cout << "Hello from function 1." << endl; m.waitKey(); } void func2() { Menu m; cout << "Hello from function 2." << endl; m.waitKey(); } void func3() { Menu m; cout << "Hello from function 3." << endl; m.waitKey(); } void Exit() { Menu m; cout << "Goodbye." << endl; m.waitKey(); exit(0); }
Menu.h
#include <string> #ifndef MENU #define MENU const int MAXCOUNT = 20; struct menuItem { void (*func) (); std::string descript; }; class Menu { private: menuItem mi[MAXCOUNT]; int count; void runSelection(); public: Menu(); void addMenu(std::string Description, void (*f)()); void runMenu(); void waitKey(); }; #endif
Menu.cpp
#include <iostream> #include <cstdlib> #include <conio.h> #include <string> #include "Menu.h" using namespace std; Menu::Menu() :count(0) { } void Menu::addMenu(string Description, void (*f)(void)) { if (count < MAXCOUNT) { this->mi[count].func = f; this->mi[count].descript = Description; count++; } } void Menu::runMenu() { for (;;) { system("CLS"); for (int i = 0; i < count; i++) { cout << this->mi[i].descript << endl; } runSelection(); } } void Menu::runSelection() { int select; cin >> select; if (select <= count) this->mi[select - 1].func(); } void Menu::waitKey() { cout << "Press any key to continue." << endl; while (!_kbhit()); fflush(stdin); }
Here is the solution to above problem. In the Menu.h the Mi is changed to vector instead of array. Please find the code below
Menu.h
#include <string>
#include <vector> //import vector library
#ifndef MENU
#define MENU
const int MAXCOUNT = 20;
struct menuItem
{
void (*func) ();
std::string descript;
};
class Menu
{
private:
std::vector<menuItem> mi; //create
vector<menuItem>
int count;
void runSelection();
public:
Menu();
void addMenu(std::string Description, void (*f)());
void runMenu();
void waitKey();
};
#endif
Menu.cpp
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <string>
#include "Menu.h"
using namespace std;
Menu::Menu()
:count(0)
{
}
void Menu::addMenu(string Description, void (*f)(void))
{
//in the add selection the new item needs to create
and pushed in the vector
if (count < MAXCOUNT)
{
//create a temp object and add all details to it
menuItem m;
m.func=f;
m.descript=Description;
//push the temp object in the
vector
this->mi.push_back(m);
count++;
}
}
void Menu::runMenu()
{
for (;;)
{
system("CLS");
for (int i = 0; i < count; i++)
{
cout << this->mi[i].descript << endl;
}
runSelection();
}
}
void Menu::runSelection()
{
int select;
cin >> select;
if (select <= count)
this->mi[select - 1].func();
}
void Menu::waitKey()
{
cout << "Press any key to continue." << endl;
while (!_kbhit());
fflush(stdin);
}