Solved

class menus with functions in other classes

Posted on 2007-11-27
12
321 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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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

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

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 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

12 Experts available now in Live!

Get 1:1 Help Now