?
Solved

Help with a sub class menu...

Posted on 2007-12-01
6
Medium Priority
?
284 Views
Last Modified: 2010-04-01
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
Comment
Question by:jschmuff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:jschmuff
ID: 20389526
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
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 20389540
>>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
 

Author Comment

by:jschmuff
ID: 20389563
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jschmuff
ID: 20389615
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1000 total points
ID: 20390859
>> 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
 
LVL 86

Expert Comment

by:jkr
ID: 20393534
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question