如何从班级中获取数字?C++

How to get a number from a class? C++

本文关键字:获取 数字 C++      更新时间:2023-10-16

假设我有一个项目。此项目使用两个类。第一类是菜单类,第二类是五角大楼类。

假设我在菜单类中得到输入,我想移动或复制,无论你说什么,到五角大楼类。

你是怎么做到的?这是我的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()中输出这个区域和东西;

有什么想法吗?谢谢!

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

然而,第

一件事是第一件事 - 如何做到这一点。

你可以让菜单类有一个五边形成员变量,并做类似的事情

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