-
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;
}
}
-
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.
-
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.
-
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.
-
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.
-
>> 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.
-
-
I thought I answered that. oh well.