Solved

Help with a sub class menu...

Posted on 2007-12-01
6
268 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

919 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

16 Experts available now in Live!

Get 1:1 Help Now