Reputation: 389
So, let's say I have a project. This projects uses two classes. First class MenuClass and a second is Pentagon Class.
Say I get inputs in Menu class, and I want to move or copy, whatever you say, to Pentagon Class.
How do you do that? Here's a piece of code form my MenuClass:
Menu::Menu( void ) {
userMenuSelection = Quit;
} // Constructor Menu
// =====================
Menu::~Menu( void ) {
cout << "====================================" << endl;
} // Destructor ~Menu
// =====================
// ==============================
// Accessor Member-Function Get()
// ==========================
MenuChoices Menu::Get( ) {
return userMenuSelection;
} // Accessor Method Get
// ========================
// =============================
// Mutator Member-Function Set()
// ========================================
void Menu::Set( MenuChoices newValue ) {
userMenuSelection = newValue;
} // Mutator Method Set
// =======================
// ==========================
// Member-Function Display( )
// ==========================
void Menu::Display( ) {
cout << "======================================" << endl;
cout << " MENU SELECTION " << endl;
cout << "======================================" << endl;
cout << "1: Calculate the Perimeter of Pentagon" << endl;
cout << "2: Calculate the Area of Pentagon" << endl;
cout << "3: Quit" << endl;
cout << "======================================" << endl;
cout << endl;
} // Member-Function Display
// ============================
// =========================
// Member-Function QueryUser
// =========================
void Menu::QueryUser( ) {
int selection;
cout << "Enter Menu Selection: ";
cin >> selection;
switch (selection){
case 1: userMenuSelection = Perimeter;
break;
case 2: userMenuSelection = Area;
break;
case 3: userMenuSelection = Quit;
default: userMenuSelection = Quit;
} // switch
// ===========
cout << endl;
} // Method QueryUser()
// =======================
// =================
// Method Continue()
// ========================
bool Menu::Continue( ) {
return userMenuSelection != Quit;
} // Method Continue
// ====================
// ==============================
// Member-Function ProcessCommand
// ==============================
void Menu::ProcessCommand( ) {
int numberA; // Length of Sides
if (userMenuSelection == Quit ){
cout << "Thank you for using this type of program. Have a nice day!" << endl;
}
else if (userMenuSelection != Quit) {
cout << "Please enter an integer value for the length of the sides: ";
cin >> numberA;
So whenever I move/copy these input to Pentagon class I want it to do, for example something like this:
void Pentagon::ProcessCommand( ) {
int numberA; // Length of Sides
if (userMenuSelection == Quit ){
cout << "Thank you for using this type of program. Have a nice day!" << endl;
}
else if (userMenuSelection != Quit) {
cout << "Please enter an integer value for the length of the sides: ";
cin >> numberA;
// ==============================
switch ( userMenuSelection ) {
case Perimeter:
cout << "Perimeter = " << (5 * numberA) << endl;
break;
case Area:
cout << "Area = " << numberA * numberA * sqrt(25.0 + 10.0 * sqrt(5.0)) / 4.0;
break;
default: cout << "Warning: error state encountered." << endl;
}
cout << endl;
}
}
And then output this areas and stuff in main();
Any ideas? Thank you!
Upvotes: 0
Views: 819
Reputation: 616
It seems that your problem is one of program design. If you want the menu class to represent the UI it is redundant to copy any code from the Menu class to the Pentagon class. Keep them seperate, then you can re-use/extend the menu class to include circles, rectangles, cylinders whatever.
However first thing`s first - how to do this.
You could have the menu class have a pentagon member variable, and do something like
cout << "Please enter an integer value for the length of the sides: ";
cin >> numberA;
thePentagon.SetSidesLength(numberA)
where
void Pentagon::SetSidesLength(int length)
{
this.length = length;
}
OR you could have a menu and a pentagon declared in main, and pass a pointer to the pentagon to the menu class like so
class Menu
{
private:
//some other stuff omitted for brevity
Pentagon* thePentagon;
public:
//constructor
Menu(Pentagon* pentaPointer) : thePentagon(pentaPointer) {};
}
//main
int main()
{
Pentagon myPentagon;
Menu myMenu(&myPentagon);
.
.
.
}
and when using the member functions of the pentagon, in case you don`t know, use the -> operator
thePentagon->SetSidesLength(a);
Upvotes: 2