HW2 Constructing a Class Overview This homework assignments
HW2 – Constructing a Class
Overview
This homework assignment\'s purpose is to get you familiar with constructing classes in C++. Project data are included for the CLion IDE. Tests for the project are also included and can be run from within the development environment. Please note: you must split the declaration and definition of the class into the header file and source file, respectively, so that tests may include the header file by itself. Files to be submitted are as follows:
NumericalMenu Class: NumericalMenu.cpp NumericalMenu.h
The class must be in the files specified above and follow the specification in this document. If you use Eclipse, you will need to create a project for the assignment.
Objectives
Upon completion of this activity, students should be able to...
• Design, declare, and define a simple C++ class
• Implement C++ class methods and attributes (also known as member functions and variables)
• Match a class specification so that code can be imported into another program
Instructions
You will construct a class for the creation and management of menus for console-based programs,
according to the specifications outlined in this document.
WARNING: Your output must match the examples EXACTLY to receive credit. Deviations will result in reduction of score on the assignment. Thoroughly test your code to be sure it compiles and executes properly according to standard C++! Submissions that do not compile will be marked zero.
NumericalMenu
The class NumericalMenu should be defined in NumericalMenu.h and contain the following methods:
// Default constructor
NumericalMenu()
Default constructor for this class.
int addOption(string option)
Add the passed in option to the menu. Returns the number assigned to the new option in the menu.
void setCancelText (string cancelText)
Set the text displayed for cancellation to cancelText. (Defaults to \"Cancel\" if not called.)
void setErrorText (string errorText)
Set the text displayed on receipt of invalid input to errorText. (Defaults to \"Error!\" if not called.)
void setPrompt(string prompt)
Set the initial prompt that precedes the menu options. (Defaults to \"Choose an option:\")
void setRepeatPromptOnError(bool shouldRepeat)
Set whether or not the prompt is repeated on receipt of invalid input; prompt will be repeated if
shouldRepeat is true. (Defaults to true if not called.)
int run() const
Runs the menu; does not return until the user provides valid input. Returns -1 on cancellation, and the
option number otherwise.
int size() const
Returns the current number of menu options in this menu.
You may, at our option, add any private class methods that help you accomplish this goal. DO NOT add
any additional public methods or variables, as this violates encapsulation safety.
Here is an example of a program that might use this class:
#include <iostream>
#include \"NumericalMenu.h\"
int main()
{
NumericalMenu menu;
menu.setPrompt(\"Select an option:\");
menu.addOption(\"Enter new values\");
menu.addOption(\"Help\");
menu.addOption(\"Save\");
menu.setCancelText(\"Exit\");
int result = menu.run();
cout << \"Returned: \" << result << endl;
}
When run, here is what the output would look like:
Select an option:
1 – Enter new values
2 – Help
3 – Save
4 – Exit
17
Error!
Select an option:
1 – Enter new values
2 – Help
3 – Save
4 – Exit
0
Error!
Select an option:
1 – Enter new values
2 – Help
3 – Save
4 – Exit
4
Returned -1
I\'m having the hardest time getting started on this project. I understand the concept of what the program is supposed to do, but I\'m stuck at implementation.
Solution
Solution:
Note: program using the given funcrion names and variable name.
NumericalMenu.h
#include <vector>
#ifndef NUMERICALMENU_H_
#define NUMERICALMENU_H_
using namespace std;
//class members
class NumericalMenu
{
public:
//Default constructor
NumericalMenu();
//addoption function
int addOption(string);
//Setcancel text function
void setCancelText(string);
//Set error test function
void setErrorText(string);
//set prompt function
void setPrompt(string);
//SetRepeatprompt on error function
void setRepeatPromptOnError(bool);
//Int run constant and size constant
int run() const;
int size() const;
string cancel;
string error;
string prompt;
int numOptions;
bool repeatPrompt;
vector<string> options;
};
#endif
NumericalMenu.cpp
#include <iostream>
using namespace std;
//include the header file
#include \"NumericalMenu.h\"
//constructor definition
NumericalMenu::NumericalMenu()
{
//set the default values
prompt = \"Select an option: \";
cancel = \"Cancel\";
error = \"Error!\";
repeatPrompt = true;
numOptions = 0;
}
int NumericalMenu::addOption(string opt)
{
//increment the option count
numOptions++;
//insert option on the vector
options.push_back(opt);
return numOptions;
}
//Setprompt function
void NumericalMenu::setPrompt(string pro)
{
prompt = pro;
}
//setCancelText definition
void NumericalMenu::setCancelText(string Can)
{
cancel = Can;
}
//function setErrorText definition
void NumericalMenu::setErrorText(string err)
{
error = err;
}
//setRepeatprompt on error
void NumericalMenu::setRepeatPromptOnError(bool rep)
{
repeatPrompt = rep;
}
//function run() constdefinition
int NumericalMenu::run() const
{
int selec;
printf(\"%s\ \",prompt);
for(int vi = 1; vi <= size(); vi++)
{
cout << vi << \" - \";
printf(\"%s\ \",options[vi - 1]);
}
cout << size() + 1 << \" - \";
printf(\"%s\ \ \",cancel);
while(!(cin >> selec) || selec < 1 || selec > size() + 1)
{
if(!repeatPrompt)
{
printf(\"\ %s\ \ \",error);;
cin.clear();
cin.ignore(1000, \'\ \');
}
else
{
printf(\"\ %s\ \ \",error);;
cin.clear();
cin.ignore(1000, \'\ \');
printf(\"%s\ \",prompt);
for(int vi = 1; vi <= size(); vi++)
{
cout << vi << \" - \";
printf(\"%s\ \",options[vi - 1]);
}
cout << size() + 1 << \" - \";
printf(\"%s\ \ \",cancel);
}
}
if(selec == size() + 1)
{
return -1;
}
else
{
return selec;
}
}
int NumericalMenu::size() const
{
return numOptions;
}
Note: Use driver program given in the question to run this program




