Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with a sub class menu...

Posted on 2007-12-01
6
Medium Priority
?
292 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

618 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