Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

class menus with functions in other classes

Posted on 2007-11-27
12
Medium Priority
?
352 Views
Last Modified: 2013-12-14
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";
}
0
Comment
Question by:jschmuff
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 4

Expert Comment

by:yuy2002
ID: 20363038
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
 
LVL 4

Expert Comment

by:yuy2002
ID: 20363097
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
 
LVL 10

Expert Comment

by:margajet24
ID: 20363338
add

friend class Menufunctions
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jschmuff
ID: 20363549
Where do I add friend class MenuFunctions?
0
 

Author Comment

by:jschmuff
ID: 20363556
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
 
LVL 4

Expert Comment

by:yuy2002
ID: 20363605
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
 
LVL 4

Expert Comment

by:yuy2002
ID: 20363641
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20364509
>>>> #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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 2000 total points
ID: 20364571
>>>> 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
 
LVL 4

Expert Comment

by:yuy2002
ID: 20365118
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20365268
>>>>>>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
 
LVL 4

Expert Comment

by:yuy2002
ID: 20370700
got it,alex, thanks so much.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 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