所以,假设我有一个项目。这个项目使用两个类。第一个类 MenuClass,第二个是 Pentagon Class。
假设我在 Menu 类中获得输入,并且我想移动或复制,无论你说什么,到 Pentagon Class。
你是怎样做的?这是我的 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;
因此,每当我将这些输入移动/复制到五角大楼类时,我都希望它这样做,例如:
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;
}
}
然后在 main() 中输出这些区域和内容;
有任何想法吗?谢谢!