Link to home
Start Free TrialLog in
Avatar of jschmuff
jschmuffFlag for United States of America

asked on

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";
}
Avatar of yuy2002
yuy2002

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

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.
add

friend class Menufunctions
Avatar of jschmuff

ASKER

Where do I add friend class MenuFunctions?
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....
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
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

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

ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
>>>>>>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.


got it,alex, thanks so much.