I am trying to pass objects into functions, but which object to pass in will depend on user input. I'm not sure how to do this.
In int main(){
I've declared four objects all of a child class type:
Human Kite(20, 100, "Kite"); Cyberdemon Ky(20, 100, "Ky Ky"); Balrogs Xor(20, 100, "HaXoR"); Elf Peet(20, 100, "Peet");
Then depending on user input, I pass in one of the four objects into an object of type Creature, the Parent Class.
int player_creature_switch;
Creature player1creature;
Creature player2creature;
cout << "Player 1 choose your class: (1) Human (2) Cyberdemon (3) Balrogs (4) Elf" << endl;
cin >> player_creature_switch;
switch(player_creature_switch){
case 1:
player1creature = Kite;
break;
case 2:
player1creature = Ky;
break;
case 3:
player1creature = Xor;
break;
case 4:
player1creature = Peet;
break;
default:
break;
}
cout << "Player 2 choose your class: (1) Human (2) Cyberdemon (3) Balrogs (4) Elf" << endl;
cin >> player_creature_switch;
switch(player_creature_switch){
case 1:
player2creature = Kite;
break;
case 2:
player2creature = Ky;
break;
case 3:
player2creature = Xor;
break;
case 4:
player2creature = Peet;
break;
default:
break;
}
execute_battle(player1creature, player2creature);
return 0;
}
Then I wish to pass the two Creature objects holding any one of the four child class objects into a function, execute_battle, which will access the member function of the objects passed in as arguments.
void execute_battle(Creature& pl_creature, Creature& p2_creature){
cout << pl_creature.getType() << p1_creature.getName() << "has entered the fray!";
.... more code....
}
I have my header files and my base/child classes set up fine. I know this because my member functions are working fine in main if I do not pass an object to a function. I also have my function protocol, etc.
I know I am not correctly passing the objects or accessing member functions within the function execute_battle
, because currently I am getting varying errors such as In function execute_battle: p1_creature was not declared in this scope
. Which I am really confused about, because I clearly passed it in as an argument.
I have also tried creating another Creature instance within the function:
void execute_battle(Creature& pl_creature, Creature& p2_creature){
Creature p1_creat = p1_creature;
cout << pl_creat.getType() << p1_creat.getName() << "has entered the fray!";
}
But to no avail. How could I do this? Much more importantly, could someone explain what is going on in terms of references/addresses of the objects being passed?
For example, I understand that when, within the switch statement, I put the address of the child object into player1creature
of the Parent type Creature, I am not just putting all of its contents into player1creature
and overwriting everything in player1creature
. Instead I am just passing the address (hopefully?) so it retains all the parent functions as well. Then, I pass the parent object into the function execute_battle
, and the function will create a local copy of that object (right)? This is what I think is going on, but I wouldn't be surprised if I have something wrong.
I have also tried using pointers and references in other places, but things just got very messy quickly. Let me know if I could provide more code somehow.
Thank you!
execute_battle()
function sometimes you usepl_creature
(like your parameter) and sometimesp1_creature
(with a 1 instead of l – JDS May 25 at 23:45