I have created a pure virtual class called Menu for my menu system.
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.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
somthing like this (exerpt from definition for class MainMenu:public Menu)
What do you think about this implementation?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; } }
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
Again further input would be great as well as any suggestions or ideas on the above.Code:MainMenu::MainMenu(){ display_menu(); while (!(validate_option(Menu::option))){ Menu::option=Menu::get_option(); } process_option(Menu::option); }



LinkBack URL
About LinkBacks


