A doubt about friendship

I was making a very simple text based RPG.
I decided to split the code into various files because that would make managing the code easier (hopefully).

Now I had two header-files, Hero.h & Enemy.h

Now I am making a function int Fight() which is friend to both the classes, Hero & Enemy.

Now where should I declare the function?
In the file having the main function? or Enemy.cpp? or Hero.cpp?
Assuing it's a global function, along the lines of

int Fight(Hero& hero, Enemy& enemy);

I would put it in main file for now, as it's not specific to either of the classes.

Later on, when it grows enough, it might merit its own file. Or you might want to put it in a file with other related functions, if you end up needing enough global functions.

But don't make Fight a friend to either of the classes. That breaks encapsulation, which is one of the main aims of OO design.

Andy

P.S. Might it be better to implement your fighting functionality using another class, instead of a function?
Last edited on
I had initially intended there to be a single kind of enemy (Named Enemy).
So I had thought that making a friend function will make it easier to handle and the original function was:
int Fight ();
only which would return 1 if hero wins, else 0.
Now I think making more than one enemy would be more interesting, and so I will be using the
int Fight(Hero& hero, Enemy& enemy);
function prototype.

As for making it a class, it would be going over-board. The fighting is very streamlined, using a dice to determine the victor.
The fight goes like this:

1. Hero attacks Enemy:
**Hero.Attack = 5. Five dices are rolled.
**Enemy.Defence = 3. Three dices are roller.
**Their Values are added. If Attack > Defence, Enemy is hurt, else dodges.
2.Similarly for enemy.
3.Repeated until one of the fighters loses.
Last edited on
Topic archived. No new replies allowed.