I just started to get a grasp of classes.

I can't find my answer anywhere, and I hope someone on here can answer it.
I am trying to simlate a battle USING CLASSES
Everywhere I look on the net people in C++ don't use classes to simulate battles. You could write the code in C. So I can't find any good examples!

I want to pass two DIFFERENT objects in one function.


Note: The objects are under the same class.

If I make no sense, just ignore me :-x

Edited 6 Years Ago by dadam88: n/a

When you say two different objects same class do you mean

myClass obj1;
myClass obj2;

or are they both built off the same class with inheritance?

If you have some code showing what you have tried that would be helpful.

#include <cstdlib>
#include <iostream>

using namespace std;

class player
{
     private:
             char name[50];
             
     public:
            int hp, atk;
            int playernum;
            void getinfo()
            {
                     cout << "Please enter your username:\t";
                     cin >> name;        
            }     
            void spitinfo()
            {
                      cout << "Player "<< playernum <<" is:\t" << name << endl;
                      
                      cin.get();
            }
            void getstats()
            {
                 cout << "Player "<<playernum<< "'s stats are:";
                 cout << "HP : "<<hp<<" ATK : " << endl;
                 }
            void fight(int p1,int p2) 
// I would like to run player 1 and player 2 so i can do p1atk p2atk p1hp p2hp            {
               
                 cout << "Player "<< playernum << " is attacking player " << playernum << endl;
                 cout << "Player "<< playernum << " dealt " <<atk << " damage!" << endl;
                 
                 }
};





int main()
{
    
    player one;
    player two;
    one.playernum = 1;
    two.playernum = 2;
    one.hp = 50;
    one.atk = 10;
    two.hp = 100;
    two.atk = 5;
    one.getinfo();
    two.getinfo();
    one.spitinfo();
    two.spitinfo();
    one.getstats();
    two.getstats();
    one.fight(1,2);// To pass them into a function, I want to pass person.one and person.two data through this function and then convert there data into other variables to make the code clearer and easier
    cin.get();
    return EXIT_SUCCESS;
}

I know it probabley look's retarded, but hey I have to start somewhere, not sure about inheritance...new to classes :-D

Thanks!

Edited 6 Years Ago by dadam88: n/a

In this case, if the two objects are of the same class, then you can fairly easily just pass one object to a method called on the other and act on both objects in that method. In code:

class player {
  .. all the stuff you posted ..
  public:
    void fight(player& opponent) {
      hp -= opponent.atk;
      opponent.hp -= atk;
    };
};

int main() {
  .. all the stuff you posted ..
  one.fight(two);
  ..
};

I assumed that atk means attack (power) and hp means health points. On that, I would recommend that you try and use more complete names rather than acronyms for variable names, this will help you in the long-run for debugging and understanding your own code (and for us to understand better too).

>>not sure about inheritance...new to classes :-D
Well, if you don't feel ready to tackle inheritance just yet, and that's understandable, just know that you shouldn't try to program anything too big in object-oriented programming before reaching the point of being able to use inheritance and all the advantages of it (it's essential in OOP). It's ok to start step by step, but you might want to get into inheritance soon rather than later.

Anyway, putting inheritance aside, one thing you could do in this case, that is typical of games or simulators, is to use a managing class. In your battle scenario, I would suggest you make a class called "battle" whose data members are the players and other data such as whose turn it is, etc. This way your main function just creates a battle object, adds two players to it and then calls fight(). I know it looks like it doesn't make much of a difference, and you're right, but when adding inheritance and the polymorphism that comes with it, it makes all the difference in the world.

I wrote out your code a bit cleaner and included a constructor to remove some of the clutter in main(). I also added a few comments saying briefly what is going on but for the most part I kept what you had.

#include <iostream>

using namespace std;

class Player
{
	string name;
	int hp, dmg, playerNum;

	public:

	Player(){}; //default constructor
	Player( int, int, int ); //make a constructor so you can assign health dmg and playerNum right away

	void SetName();
	void GetName();

	void GetStats();

	void Fight( Player& ); //take in the reference of the Player so you can modify their hp
};

Player::Player( int health, int damage, int num )
{
	hp = health;
	dmg = damage;
	playerNum = num;
}

void Player::SetName()
{
	cout << "Please enter Player " << playerNum << "'s username:\t";
	cin >> name;
	cout << endl;
}

void Player::GetName()
{
	cout << "Player " << playerNum << " is:\t" << name << endl;
	cin.get();
}

void Player::GetStats()
{
	cout << "Player " << playerNum << "'s stats are:" << endl;
	cout << "HP: " << hp << " DMG: " << dmg << endl << endl;
}

void Player::Fight( Player &opp )
{
	opp.hp -= dmg;
	cout << "Player " << playerNum << " is attacking player " << opp.playerNum << endl;
	cout << "Player " << playerNum << " delt " << dmg << " damage!" << endl << endl;
}

int main()
{
	Player one(50, 10, 1), two(100, 5, 2); //hp, dmg, playerNum

	one.SetName();
	two.SetName();

	one.GetName();
	two.GetName();

	one.GetStats();
	two.GetStats();

	one.Fight(two);

	two.GetStats(); //display health change

	cin.get();
	return 0;
}

If you have any questions about this feel free to ask and I'll try my best to explain.

Thank you so much guys

void fight(player& opponent) //Can you explain this bit of code?  I am just able to create a new player on the spot called opponet and link it the two's address?
{     
 hp -= opponent.atk;      opponent.hp -= atk;
}
void Player::Fight( Player &opp )//<----This calls two into the function and it's at the address of opp?{	opp.hp -= dmg;	
cout << "Player " << playerNum << " is attacking player " << opp.playerNum << endl;
cout << "Player " << playerNum << " delt " << dmg << " damage!" << endl << endl;}

@stfuo
I should stick with constructors when ever I deal with classes I take it, look's a lot cleaner.

I highlighted in red what I'm a little bit confused with, everythign else is really clear!

Thanks!

Pretty much what happens is that two's reference gets stored into opp when it gets passed into the function Fight() and when you modify opp it directly changes two because points to the place in memory where two is located. If you were to not pass by reference (pass by value) instead of taking storing the reference of two it now just stores the value and whatever you do to that value will not effect the original variable (in this case two).

In the case we have opp pretty much becomes two and anything you do to it will change two directly.

I know I typed a bunch of stuff that could probably be summarized in a few lines but I hope this clears it up a bit.

I understand but not fully YET
Sure I will as a little time goes on.

Thanks for everything guys (I think both of yall are guys from yoru avatars!), I should be posting something maybe once a week, like a code. I posted my first real app called DiceGame, it's lame but hey, applying my newbie programming skills.

THIS THREAD IS SOVLED!

This question has already been answered. Start a new discussion instead.