• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

Help with a sub class menu...

Ok I started a basic program from scratch that has a base class called BaseMenu, and a class called SubMenu this SubMenu is going to reuse the menuHeader() and menuChoice() methods implemented in it's base class BaseMenu and will override the menuDisplay() method to display the SubMenu options. SubMenu will inherit the functionality from it's base class BaseMenu. I tried to sum that up as easy as I can explain it. I left out messages.h and .cpp since there are strictly used in the BaseMenu and dont need to be used in SubMenu. After saying all that my actual question is How do I do that. (This is an old assignment I failed and I have the time to figure out what it should actually do) This is not the original code I used cause it was so far off from this I don't even know what I was thinking.

Here is my code for BaseMenu.h:
class BaseMenu
{
public:
      void menuHeader();
      void menuDisplay();
      void menuChoice();
};

Here is the code for BaseMenu.cpp:
#include <iostream>
#include "basemenu.h"
#include "messages.h"
using namespace std;

char choice;
bool done = false;
Messages mess;

void BaseMenu::menuHeader()
{
      cout << "******************\n";
      cout << "****** MENU ******\n";
      cout << "******************\n";
}
void BaseMenu::menuDisplay()
{
      cout << "***(H)ello     ***\n";
      cout << "***(W)elcome   ***\n";
      cout << "***(O)ther menu***\n";
      cout << "***(Q)uit      ***\n";
      cout << "******************\n";
}
void BaseMenu::menuChoice()
{
      do
      {
      cout << "Choice: ";
      cin >> choice;
      cout << endl;
      switch(choice)
      {
      case 'h':
            mess.hello();
            done = false;
            break;
      case 'w':
            mess.welcome();
            done = false;
            break;
      case 'o':
            done = false;
            break;
      case 'q':
            done = true;
            break;
      default:
            cout << "Invalid selection....\n";
            break;
      }
      }while(!done);
}

now for SubMenu.h
class SubMenu
{
public:
      void newMenu();
};

code in SubMenu.cpp:
#include <iostream>
#include "submenu.h"
#include "basemenu.h"

using namespace std;
BaseMenu bmenu;

void SubMenu::newMenu()
{
      bmenu.menuHeader();
      cout << "***(B)ye       ***\n";
      cout << "***(L)augh     ***\n";
      cout << "***(Q)uit      ***\n";
      cout << "******************\n";
}
 ***How would SubMenu inherit the choice from BaseMenu without the switch choices? Would I have to use char in stead of void for menuChoice() I am just not sure on what I need to do.
0
jschmuff
Asked:
jschmuff
  • 3
  • 2
2 Solutions
 
jschmuffAuthor Commented:
for basemenu.cpp I added

#include "submenu.h"

SubMenu smenu;

under case 'o': I put smenu.newMenu(); // so that option works from the menuChoice() in BaseMenu
0
 
jkrCommented:
>>How would SubMenu inherit the choice from BaseMenu without the switch
>>choices?

The same way it works for 'newMenu()' - you override the base class' function with your new implementation, that is called overriding. See http://www.codersource.net/cpp_tutorial_inheritance.html

Just use

class SubMenu : public BaseMenu
{
public:
      void displayMenu();
      void menuChoice();
};

void SubMenu::displayMenu()
{
      bmenu.menuHeader();
      cout << "***(B)ye       ***\n";
      cout << "***(L)augh     ***\n";
      cout << "***(Q)uit      ***\n";
      cout << "******************\n";
}
 
void SubMenu::menuChoice()
{
      do
      {
      cout << "Choice: ";
      cin >> choice;
      cout << endl;
      switch(choice)
      {
 
      }
      }while(!done);
}
0
 
jschmuffAuthor Commented:
Ok I was looking back at my old assignment I have. What it says is menuDisplay(); needs to be a virtual char and menuChoice(); needs to be a char I was supposed to demonstrate inheritance. For me to be certain  I was supposed to demonstrate a polymorphic invocation of the overriden method. I was supposed to create an instance of the derived class using a pointer of reference of the base class type, invoke a virtual method and demonstrate that the overriden method is being called. (Since this isn't my assignment anymore I don't need to show that the overriden method is being called.) LOL I am still confused on this even 4 weeks after it was due... wow good thing I am going back and practicing.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jschmuffAuthor Commented:
What if I created a choices class that controls the commands entered in... is there a way I can do that. I only understand the basics of classes using void I don't really get the virtual char or char for that matter how can I implement them into this code
0
 
Infinity08Commented:
>> Ok I was looking back at my old assignment I have. ...

Which parts of this assignment are you confused about. You can take jkr's post to get you started, and then slowly make the modifications needed to fit the requirements.


>> I only understand the basics of classes using void I don't really get the virtual char or char for that matter how can I implement them into this code

You can look at a method as a simple function that is executed inside a specific object. So, just like a normal function, methods can return something (a char for example), and they take parameters. In this for example :

        class Test {
          public :
            char fun(int i);
        };

fun is a method for the Test class that takes an int as parameter, and returns a char. You can call it like this :

        Test test;                        // <--- create an instance of the Test class
        char c = test.fun(5);        // <--- call the fun method for the test object with parameter 5. The result is stored in c

The virtual keyword changes the way the method is implemented :

        class Test {
          public :
            virtual char fun(int i);
        };

This is the same class with the same method, but now the method is declared virtual. This means that the fun method uses dynamic (run time) binding as opposed to static (compile time) binding for non-virtual methods. In other words, when overriding the method in a derived subclass, the subclass's implementation for fun will be different, and it will be decided at run time which implementation to run for which object. More information here :

        http://www.cplusplus.com/doc/tutorial/polymorphism.html
0
 
jkrCommented:
Sorry for not returning earlier, but we had a fatality among my friends here, which left little room for EE...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now