Thoughts on Menu System for Book Inventory

This is a discussion on Thoughts on Menu System for Book Inventory within the C++ Programming forums, part of the General Programming Boards category; I have created a pure virtual class called Menu for my menu system. Code: #ifndef MENU_H #define MENU_H class Menu{ ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Thoughts on Menu System for Book Inventory

    I have created a pure virtual class called Menu for my menu system.
    Code:
    #ifndef MENU_H
    #define MENU_H
    
    class Menu{
        public:
            virtual void display_menu() const=0;
            int get_option() const;
            
            virtual bool validate_option(int) const=0;
            virtual void process_option(int) const=0;
        private:
            int option;
    };
    #endif
    I then create concrete mainmenu and will implement an edit menu and query menu. I will then have individual member functions that are called by the pure virtual function process_option, that way I can use a base class pointer to refer to a derived class object and still call derived class member functions within process_option.
    somthing like this (exerpt from definition for class MainMenu:public Menu)

    Code:
    void MainMenu::display_menu() const{
        cout << "\t\tMain Menu\n";
        cout << "1.  Display Library\n";
        cout << "2.  Edit Library\n";
        cout << "3.  Query Library\n";
        cout << "4.  Save Library\n";
        cout << "5.  Load Library\n";
        cout << "6.  Exit"<<endl;
    }
    
    bool MainMenu::validate_option(int noption){
        bool valid;
        if (noption>=0 && noption<=6)
            valid=TRUE;
        else
            valid=FALSE;
        return valid;
    }
    
    void MainMenu::process_option(int noption){
        switch (noption){
            case 1:
                    //call member function display library  
                    display_library(library &);
                    break;
            case 2: 
    
            case 3:
            case 4:
            case 5:
            case 6:
            default:
                    break;
        }
    }
    What do you think about this implementation?
    Brain Storming:with a base_class pointer can I call a derived class objects constructor ie Menu *ptrMenu=new MainMenu(library&);

    case 2 would create an instance of an EditMenu will I have problems with this

    here is the code for my default constructor for MainMenu how can I exit the program from process_option if case 6 is choosen
    Code:
    MainMenu::MainMenu(){
        
        display_menu();
        while (!(validate_option(Menu::option))){
            Menu::option=Menu::get_option();
        }
        process_option(Menu::option);
    }
    Again further input would be great as well as any suggestions or ideas on the above.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Looks pretty good to me. Encapsulating menus is something I've never done before. Maybe I'll give it a shot.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Make sure your base class has a virtual destructor or else calling delete on the base class pointer will not call the right destructor.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    This schema isn't all that I imagined. I have started my second Menu an edit menu which is also derived from Menu similar to MainMenu and must create EditMenu within the process_option() function of MainMenu. Ending edit I just return to MainMenu. The code is redundant the way I handle editmain.
    In main I have:
    Code:
    Menu *ptrBaseMenu = new MainMenu(library);
      while((*ptrBaseMenu).get_option()!=6){
            (*ptrBaseMenu).set_option(-1);
            while (!((*ptrBaseMenu).validate_option((*ptrBaseMenu).get_option()))){
                    (*ptrBaseMenu).display_menu();
                    (*ptrBaseMenu).set_option((*ptrBaseMenu).input_option());
            }
            (*ptrBaseMenu).process_option((*ptrBaseMenu).get_option());
            
      }
    in mainmenu.cpp my definitions for MainMenu I have:
    Code:
    void MainMenu::process_option(int noption)const{
        switch (noption){
            case 1:
                    display_library(get_ptrLibrary());
                    break;
            case 2: Menu *ptrEditMenu = new EditMenu(getptrLibrary());
                    while((*ptrEditMenu).get_option()!=6){
                         (*ptrEditMenu).set_option(-1);
                          while (!((*ptrEditMenu).validate_option((*ptrEditMenu).get_option()))){
                                (*ptrEditMenu).display_menu();
                                (*ptrEditMenu).set_option((*ptrEditMenu).input_option());
                         }
                    (*ptrEditMenu).process_option((*ptrEditMenu).get_option());
            
                    }
                    delete ptrEditMenu;
                    break;
                    
            case 3:
            case 4:
            case 5:
            case 6:
            default:
                    break;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lost BIOS menu options?
    By PJYelton in forum Tech Board
    Replies: 3
    Last Post: 11-14-2004, 07:23 AM
  2. Hiding System Menu
    By C4Code in forum Windows Programming
    Replies: 8
    Last Post: 10-08-2002, 10:15 AM
  3. Menu System using switch and case.
    By Spectrum48k in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 08:23 AM
  4. Menu system (Using the arrow keys)
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2001, 03:31 PM
  5. quick question about C Dos text menu pgm i was doing
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 09-16-2001, 10:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21