Thread: class manipulation

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    11

    Question class manipulation

    If I have declared more than one variable of a "class" type I have already defined, how can I be sure which variable I am manipulating. If I have one function which uses "switch" to call functions from the class, can I safely assume that I am altering the "class" variable I am using to call that function with. I'm not at a complier right now, but I am typing up my code based on what I think would work. I would hate to get too deep into this idea if its not even right. Please help!

    For example:
    Code:
    //#include blah blah
    
    Names Male; //can I declare a global class variables like this?
    Names Female;
    
    void Menu ();
    
    class Names {
    
    public:
    void Add ();
    void Drop ();
    
    private:
    //blah blah
    
    };
    
    void main ()
    {
    Menu ();
    }
    
    void Menu ()
    {
    
    cout <<"1. blah blah\n2. blah blah\n";
    cout <<"Please run option: ";
    cin >>choice;
    
    if (choice == 1)
    Male.RunOption ();
    
    else if (option == 2)
    Female.RunOption ();
    
    }
    
    void Names :: RunOption ()
    {
    
    switch (choice)
    {
    case 1: Add ();
    break;
    
    case 2: Drop ();
    break;
    
    default:
    cout <<"not an option";
    Menu ();
    break;
    }
    
    }

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    You made the call with the appropriate instance.
    Male.RunOption() is much the same as if you did this in C like this:
    RunOption(&Male) where Male is a struct instance. You know that you are acting on the right data because that's the object that made the call.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    choice doesnt exist for RunOption. You should pass it in.

    RunOption(choice);

    and change your function declaration to suit.

    void Names :: RunOption (int choice)

    I'm assuming this is wrong:

    if (choice == 1) //choice here?
    Male.RunOption ();

    else if (option == 2)
    //option here?
    Female.RunOption ();

    I'd assume this:

    if (option == 1)
    Male.RunOption (choice);
    else if (option == 2)
    Female.RunOption (choice);


    Also, your instances of the class will have to be declared _after_ your class definition. i.e. This:

    Names Male;
    Names Female;


    should be after this:

    class Names {

    public:
    void Add ();
    void Drop ();

    private:
    //blah blah

    };


    Thats all I noticed at a quick glance.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    I guess what I mean is... as long as I call the RunOption() function under my class variable (like Male), can I assume that all functions called FROM the RunOption() function will make changes to the class variable Male and not the class variable Female.

    Another wording... once I call Male.RunOption(), can I consider myself inside that class variable and all functions called from the RunOption() to alter just that specific class.

    Its kind of hard to get my question across... I'm just trying to figure out a way to minimize duplication of the same functions.

    Thanks for your help! I'm usually hard-headed and don't want help, but... I guess I've got to learn when to humble myself.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    11

    re: lightatdawn

    It is a little sloppy. In general, choice is the same as option. I mean to declare global variables: Names Male, Names Female, and int choice = 0.

    I decided that might be easier so I don't have to pass any variables in. I know global variables can be dangerous though.

  6. #6
    >> Another wording... once I call Male.RunOption(), can I consider myself inside that class variable and all functions called from the RunOption() to alter just that specific class.

    Yes.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    THANK YOU BOTH!

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I thought I answered that. oh well.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM