Solved

Help with a sub class menu...

Posted on 2007-12-01
6
265 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
  • 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 250 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 250 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now