Thread: I'm sucha newb (classes)

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    I'm sucha newb (classes)

    I'm writing a program for my dads bar downstairs, and im having trouble. Each section of the program has its own class, and each class has several functions declared within it. I havent been working on it long so its hard to follow the code because it isnt representing a finished thought.

    The problem is that i need to pass the "Choice" variable from a function in one class to a function in a different class. I remember doing this in CS2 back two years ago, but i cant member anything beyond that.

    I browesed through the FAQ and even some of my old code, but for some reason im not finding help LOL. Considering the opengl programming experience i have im amazed that i cant figure this out. Heres the sloppy code.

    Code:
    /*  Bartender version 1.0
        Build 1.0.0, 12/25/04
        Steven Billington
        Wacker7 Inc.
        [email protected]
    */
    
    /*  List our include files that we will
        use throughout the program
    */
    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    /* User_Menu class contains all functions
       and variables the program will use to
       take in the users menu decision
    */
    class User_Menu
    {
     public:
            void Disp_Options();
            void User_Choice();
            int Choice;
     };
    
    class Choice_Catalog
    {
     public:
            void Prompt_Drink();
            void Find_Drink();
            void Disp_Drink();
            string Drink_Name;
    };
    
    class Choice_NewEntry
    {
     public:
            void Drink_Info();
            void Save_Data();
            void Disp_Result();
     };
    
    class Choice_Random
    {
     public:
            void Random_Drink();
            void Disp_Drink();
    };
    
    class Choice_ListCatalog
    {
     public:
            void Display_All();
    };
    
    class Choice_About
    {
     public:
            void Software_Info();
    };
    
    class Choice_Exit
    {
     public:
            void Terminate();
    };
    
    /* Search_Catalog class contains all functions
       and variables that will be used to find
       specific drink entries in the database
    */
    class Search_Catalog
    {
     public:
            void Load_Catalog();
            void Search_For();
            void Display();
    };
    
    /* Main defines the entry point for our
       application
    */
    int main(int argc, char *argv[])
    {
      /* Declare class objects*/
      User_Menu        Menu_Obj;
      Choice_Catalog   Cata_Obj;
    
      /* Invoke disply function of User_Menu
         class*/
      Menu_Obj.Disp_Options();
    
      system("pause");
    
      return 0;
    }
    
    /* Declare the Disp_Options function. This function
       lets us present options to the user
    */
    void User_Menu::Disp_Options()
    {
        cout << "Bartender v1.0" << endl;
        cout << endl;
        cout << "1. Search Catalog" << endl;
        cout << "2. New Drink" << endl;
        cout << "3. Random Selection" << endl;
        cout << "4. Master Drink List" << endl;
        cout << "5. About Bartender v1.0" << endl;
        cout << "6. Exit Program" << endl;
    
        /* Call User_Choice function*/
        User_Choice();
    }
    
    /* Declare User_Choice function. With this function we
       allow the user to make a decision based on the
       choices we presented
    */
    void User_Menu::User_Choice()
    {
        cout << endl << endl;
        cout << "Enter menu selection (1-6): ";
        cin >> Choice;
        cout << endl;
    
        switch (Choice)
        {
         case 1:
              Prompt_Drink();
              break;
         case 2:
              Drink_Info();
              break;
         case 3:
              Random_Drink();
              break;
         case 4:
              Display_All();
              break;
         case 5:
              Software_Info();
              break;
         case 6:
              Terminate();
              break;
         default:
              cout << "Invalid Entry, please retry.";
              cout << endl;
              Display_Options();
        }
    }
    
    void Choice_Catalog::Prompt_Drink()
    {
        cout << endl << "Enter drink name: ";
        cin >> Drink_Name;
    
        //was testing something
        cout << "Name was: " << Drink_Name << endl;
    }
    [code]

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    The problem is that i need to pass the "Choice" variable from a function in one class to a function in a different class. I remember doing this in CS2 back two years ago, but i cant member anything beyond that.
    Where in the code do you need to do this?

    In your switch statement in User_Menu::User_Choice(), you'll need to create an object of whatever class is needed to perform the chosen action. Example:
    Code:
    switch (Choice)
    {
    	case 1:
    		{
    			Choice_Catalog tempObj;
    			tempObj.Prompt_Drink();
    		}
    		break;
    	case 2:
    		{
    			Choice_NewEntry tempObj;
    			tempObj.Drink_Info();
    		}
    		break;
    	case 3:
    		{
    			Choice_Random tempObj;
    			tempObj.Random_Drink();
    		}
    		break;
    	case 4:
    		{
    			Choice_ListCatalog tempObj;
    			tempObj.Display_All();
    		}
    		break;
    	case 5:
    		{
    			Choice_About tempObj;
    			tempObj.Software_Info();
    		}
    		break;
    	case 6:
    		{
    			Choice_Exit tempObj;
    			tempObj.Terminate();
    		}
    		break;
    	default:
    		cout << "Invalid Entry, please retry.";
    		cout << endl;
    		Disp_Options();
    }
    You might consider using fewer classes or a base class. Several of your classes will use some common data. For instance, both Choice_Random and Choice_Catalog will display drink information. That means that both classes will need access to a common resource i.e. a drink "database".
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Almost sounded like you wanted something like virtual inheritance, but you could inherit from a base class that simply contains a static data member that holds your choice (along with a couple functions to set/get this data member if the data is intended to be private within the base class):

    Code:
    class Choice
    {
        static int choice;
    public:
        int GetChoice() const { return choice; }
        void SetChoice(int val) { choice = val; }
    };
    
    int Choice::choice;
    
    class A: public Choice
    {
    public:
        int GetChoice() const { return Choice::GetChoice(); }
        void SetChoice(int val) { Choice::SetChoice(val); }
    };
    
    class B: public Choice
    {
    public:
        int GetChoice() const { return Choice::GetChoice(); }
        void SetChoice(int val) { Choice::SetChoice(val); }
    };
    
    int main()
    {
        A a;
        B b;
    
        a.SetChoice(14);
        std::cout << b.GetChoice() << std::endl;
        b.SetChoice(26);
        std::cout << a.GetChoice() << std::endl;
    
    }
    Should output:
    Code:
    14
    26
    The point being that b has access to the same value stored in Choice::choice that a has access to. Thus all your different classes could do something similar and have access to the same data without really needing to "pass it around" as a parameter. If you wanted to check/set the value from within any of the classes then you would just call the appropriate member function.

    Maybe there is a simpler more logical way, but this is the first thing that came up in my head.

    [edit]Just realized that the SetChoice and GetChoice member functions for class A and B aren't really needed in the above examples. Oh well...[/edit]
    Last edited by hk_mp5kpdw; 12-28-2004 at 11:52 AM. Reason: Forgot ending semicolon after defining classes
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Thanks guys, i had considered that i maybe should have fewer classes or at least larger ones for the functions that will need/rely on each other. Appreciate the help, ima go tinker with it after some more HL2 : )

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Appreciate the help, ima go tinker with it after some more HL2 : )
    Now that's the spirit!!!

    My philosophy:

    1. Code till your eyeballs fall out
    2. Get stuck on something in the code
    3. Play games till your eyeballs fall out
    4. Goto 1

    Sleep is somewhere in there but's it not nearly as important.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Now, goto isnt the best way to do it, a better way would be
    Code:
    for(;;)
    {
        code_like_mad();
        get_stuck_when_coding();
        play_games_like_mad();
    }

  7. #7
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    or maybe:
    Code:
    while(1)
    {
      code();
      get_stuck();
      play_games();
    }

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Almost.
    Code:
    while(1)
    {
       code();
       get_stuck();
       play_games();
       if (starving)
          eat();
       if (completely_exhausted)
          sleep();
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    nonononono, you only feel if you are starving when you are stuck with code! So that belongs to the get_stuck_when_coding()! Same goes for sleep!

  10. #10
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    My philosophy:

    1. Code till your eyeballs fall out
    2. Get stuck on something in the code
    3. Play games till your eyeballs fall out
    4. Goto 1
    Code:
    1. do {
    2. Code till your eyeballs fall out
    3. Get stuck on something in the code
    4. Play games till your eyeballs fall out
    5. } while (!(Coded untill 3 am or until you fix the problem))
    6. if(Problem == fixed && !(later than 3am))
            goto 1.
    7. else if(Problem == fixed)
            return smart_ass_coder;
    
    8. Sleep, waking to find magic elves have fixed your code \
        and while doing so have also redesigned and rewritten the thing in \
        one tenth the lines, leaving numerous insulting deragorator \
        comments concerning you and your code. \
    9. if(reaction > 0) // positive reaction 
        10. Thank elves, and try to figure out how to trick them into making  \
            you an awesome game or something.
    9. else if(reaction < 0) // negative reaction
        10. Smash your computer and go Elf hunting the smarmy elitist  \
            uppity little bastards, not to mention they gooped up your mouse
    9. else // no reaction
        10. Go back to sleep, realizing your having a really stupid dream
    11. ... im bored... somebody else can finish this...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NewB with C++ - Functions & Classes
    By Rider in forum C++ Programming
    Replies: 19
    Last Post: 11-23-2005, 11:32 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM