class menus with functions in other classes

This is just a fresh new thread for the question because I think you guys answer my initial question for the most part... my now question is I have the basics down I just want to call the MenuFunctions class using the choice in class Menu.

Okay no math equations in this just output text.. Here it is:

my menu.h file:

class Menu
{
public:
      void displayMenu();
      void displayChoice();
};

my menu.cpp file:

#include <iostream>
#include "menu.h"

using namespace std;

void Menu::displayMenu()
{
      cout << "*******MENU*******\n";
      cout << "(A)ddition\n";
      cout << "(S)ubtraction\n";
      cout << "(Q)uit\n";
}

void Menu::displayChoice()
{
      char choice;
      cout << "Choice: ";
      cin >> choice;
      cout << endl;
}

my main.cpp file:

#include <iostream>
#include "menu.h"
#include "menufunctions.h"

using namespace std;

int main()
{
      Menu menu;

      menu.displayMenu();
      menu.displayChoice();

      return 0;
}

***(Remember forget the equations for math)

my file menufunction.h file:

class Menufunctions
{
        void addition();
        void subtraction();
        void quit();
};

my file menufunctions.cpp file:

#include <iostream>
#include "menufunctions.h"

using namespace std;

void Menufunctions::addition()
{
          cout << "You have selected Addition...\n";
}
void Menufunctions::subtraction()
{
          cout << "You have selected Subtraction...\n";
}
void Menufunctions::quit();
{
          cout << "You have selected to quit program...\n";
}
jschmuffAsked:
Who is Participating?
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> my now question is I have the basics down I just want to call
>>>> the MenuFunctions class using the choice in class Menu.

// put that in any of the headers
enum Menuoperations { ADDITION = 'A', SUBTRACTION = 'S', QUIT = 'Q' };

void Menu::displayChoice()
{
      Menufunctions menufunction;   // a temporary
      char choice = ' ';
      do
      {
           cout << "Choice: ";
           cin >> choice;
           cout << endl;
           choice = toupper(choice);  // make upper case
           switch(choice)
           {
              case ADDITION:
                   menufunction.addition(); break;    
             case SUBTRACTION:
                   menufunction.subtraction(); break;    
             case QUIT:
                   break;
             default:
                   cout << "[" << choice << "], invalid choice " << endl << endl;
                   break;
            }  
      }
      while (choice != 'Q');
}


Regards, Alex
0
 
yuy2002Commented:
Here 's the way to call Menufunctions.

regards,
charles
#include <iostream>
 
using namespace std;
 
class Menu
{
public:
      void displayMenu();
      void displayChoice(char& choice);
};
 
void Menu::displayMenu()
{
      cout << "*******MENU*******\n";
      cout << "(A)ddition\n";
      cout << "(S)ubtraction\n";
      cout << "(Q)uit\n";
}
 
void Menu::displayChoice(char& choice)
{
      cout << "Choice: ";
      cin >> choice;
      cout << endl;
}
 
class Menufunctions
{
public:
        void addition();
        void subtraction();
        void quit();
};
 
void Menufunctions::addition()
{
          cout << "You have selected Addition...\n";
}
void Menufunctions::subtraction()
{
          cout << "You have selected Subtraction...\n";
}
void Menufunctions::quit()
{
          cout << "You have selected to quit program...\n";
}
 
int main()
{
      Menu menu;
      Menufunctions menufun;
      char choice=0;
      menu.displayMenu();
      menu.displayChoice(choice);
      switch(choice)
      {
          case 'A':
               menufun.addition();
               break;
          case 'S':
               menufun.subtraction();
               break;
          case 'Q':
               menufun.quit();
               break;
          default:
               cout<<"Wrong command"<<endl;
               break;
      }
                 
      
      return 0;
}

Open in new window

0
 
yuy2002Commented:
also you could implement the menufactor operation in menu class.
my menu.h file:
#include "menufunction.h" //include menufunction header file
class Menu
{
public:
      void displayMenu();
      void displayChoice();
private:
      Menufunctions menufun; // declare an Menufunctions  object
};

my menu.cpp file:

#include <iostream>
#include "menu.h"

using namespace std;

void Menu::displayMenu()
{
      cout << "*******MENU*******\n";
      cout << "(A)ddition\n";
      cout << "(S)ubtraction\n";
      cout << "(Q)uit\n";
}

void Menu::displayChoice()
{
      char choice;
      cout << "Choice: ";
      cin >> choice;
      switch(choice)
      {
          case 'A':
               menufun.addition();
               break;
          case 'S':
               menufun.subtraction();
               break;
          case 'Q':
               menufun.quit();
               break;
          default:
               cout<<"Wrong command"<<endl;
               break;
      }
      cout << endl;
}

other files is the same as origins.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
margajet24IT Business AnalystCommented:
add

friend class Menufunctions
0
 
jschmuffAuthor Commented:
Where do I add friend class MenuFunctions?
0
 
jschmuffAuthor Commented:
yuy2002 >>

I ran your version of what I need to do to call on the functions of MenuFunctions and these are the errors I get:

c:\documents and settings\john\my documents\visual studio 2005\projects\menu system\menu system\menufunctions.h(2) : error C2011: 'MenuFunctions' : 'class' type redefinition
        c:\documents and settings\john\my documents\visual studio 2005\projects\menu system\menu system\menufunctions.h(2) : see declaration of 'MenuFunctions'
c:\documents and settings\john\my documents\visual studio 2005\projects\menu system\menu system\menu.h(9) : error C2079: 'Menu::menuFun' uses undefined class 'MenuFunctions'
.\menufunctions.cpp(7) : error C2027: use of undefined type 'MenuFunctions'
        c:\documents and settings\john\my documents\visual studio 2005\projects\menu system\menu system\menufunctions.h(2) : see declaration of 'MenuFunctions'
.\menufunctions.cpp(11) : error C2027: use of undefined type 'MenuFunctions'
        c:\documents and settings\john\my documents\visual studio 2005\projects\menu system\menu system\menufunctions.h(2) : see declaration of 'MenuFunctions'
.\menufunctions.cpp(15) : error C2027: use of undefined type 'MenuFunctions'
        c:\documents and settings\john\my documents\visual studio 2005\projects\menu system\menu system\menufunctions.h(2) : see declaration of 'MenuFunctions'
menu.cpp
main.cpp
c:\documents and settings\john\my documents\visual studio 2005\projects\menu system\menu system\menufunctions.h(2) : error C2011: 'MenuFunctions' : 'class' type redefinition
        c:\documents and settings\john\my documents\visual studio 2005\projects\menu system\menu system\menufunctions.h(2) : see declaration of 'MenuFunctions'

****What am I doing wrong....
0
 
yuy2002Commented:
Friend class should be used with caution,If all the functions and classes are declared as friends, then the concept of encapsulation and data security will go for a toss.
Your requirement could be satified without friend class, friend class is not recommanded, it increases more complexity and danger.

Best regards,
charles
0
 
yuy2002Commented:
Your header file is recontained.See the code below.
menu.h
#ifndef _MENU_
#define _MENU_ 
#include "menufunction.h"
class Menu
{
public:
      void displayMenu();
      void displayChoice();
private:
	  Menufunctions menufun;
};
#endif
 
menu.cpp
#include <iostream>
#include "menu.h"
using namespace std;
 
void Menu::displayMenu()
{
      cout << "*******MENU*******\n";
      cout << "(A)ddition\n";
      cout << "(S)ubtraction\n";
      cout << "(Q)uit\n";
}
 
void Menu::displayChoice()
{
	  char choice;
      cout << "Choice: ";
      cin >> choice;
	  switch(choice)
      {
          case 'A':
               menufun.addition();
               break;
          case 'S':
               menufun.subtraction();
               break;
          case 'Q':
               menufun.quit();
               break;
          default:
               cout<<"Wrong command"<<endl;
               break;
      }
      cout << endl;
}
 
menufunction.h
#ifndef _MENUFUNC_
#define _MENUFUNC_ 
class Menufunctions
{
public:
        void addition();
        void subtraction();
        void quit();
};
#endif
 
menufunction.cpp
#include "menufunction.h"
#include <iostream>
using namespace std;
void Menufunctions::addition()
{
          cout << "You have selected Addition...\n";
}
void Menufunctions::subtraction()
{
          cout << "You have selected Subtraction...\n";
}
void Menufunctions::quit()
{
          cout << "You have selected to quit program...\n";
}
 
main.cpp
#include "menu.h"
int main()
{
      Menu menu;
      menu.displayMenu();
      menu.displayChoice();
           
      return 0;
}

Open in new window

0
 
itsmeandnobodyelseCommented:
>>>> #ifndef _MENU_
You shouldn't use leading or trailing _ characters in private macros. It is for system macros only.

The macro should be named like the header file, in your case it is

#ifndef MENUFUNCTION_H
#define MENUFUNCTION_H

// put here the class

#endif // MENUFUNCTION_H

The macro has the purpose to not include the class header twice in one cpp, e. g. if you have a

// main.h
#include "menufunction.h"
...

and a

// main.cpp

#include "main.h"
#include "menufunction.h"

the second include would cause above errors if you don't add a protection.

An alternative is using the

#pragma once

preprocessor statement in the header. But that is proprietary to MS and I don't use it for that reason.


Regards, Alex

0
 
yuy2002Commented:
alex,
>>You shouldn't use leading or trailing _ characters in private macros. It is for system macros only.
could u explain why?
and which system do u mean?

regards,
charles
0
 
itsmeandnobodyelseCommented:
>>>>>>You shouldn't use leading or trailing _ characters in private macros. It is for system macros only.
>>>> could u explain why? and which system do u mean?

I mean the compiler 'system'. Macros used by the compiler (mostly set automatically depending on project configuration) often use the _ or __ prefix (at least the VC compiler will do so). If using the prefixes yourself you have no advantage but cannot divide the one from the other. Moreover, you accidently can use a name already used by some standard library header (_MENU_H_ may not be unique in the world).

So it is good practice to simply use the filename and replace only the period character by a underline.

If using a different name for the protection macro you easily can use a name already used by another header what may lead to 'strange' compiler errors.


0
 
yuy2002Commented:
got it,alex, thanks so much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.