0

所以,假设我有一个项目。这个项目使用两个类。第一个类 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() 中输出这些区域和内容;

有任何想法吗?谢谢!

4

1 回答 1

2

看来您的问题是程序设计之一。如果您希望菜单类表示 UI,则将任何代码从 Menu 类复制到 Pentagon 类是多余的。将它们分开,然后您可以重新使用/扩展菜单类以包括圆形、矩形、圆柱体等。

但是首先要做的事情是-如何做到这一点。

您可以让菜单类有一个五边形成员变量,并执行类似的操作

cout << "Please enter an integer value for the length of the sides: ";
cin >> numberA;
thePentagon.SetSidesLength(numberA)

在哪里

void Pentagon::SetSidesLength(int length)
{
   this.length = length;
}

或者您可以在 main 中声明一个菜单和一个五边形,并将指向五边形的指针传递给菜单类,如下所示

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);
  .
  .
  .
}

并且在使用五边形的成员函数时,如果你不知道,请使用 -> 运算符

thePentagon->SetSidesLength(a);
于 2013-10-23T04:38:04.197 回答