would this be the best way to set this up?

did i do classes like there supposed to?. i know it compiles i just wanted to know if everything's like its supposed to be professionally.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>
using namespace std;
class attack
{
	int playeratt; 
	int enemydef;
public:
	void set_stuff(int,int);
	int hit () {return (playeratt - enemydef);}
	int playerattack() {return (playeratt);}
	int enemydefence() {return (enemydef);}
};
void attack::set_stuff (int a, int b) {
	playeratt = a;
	enemydef = b;
}

int main ()
{
	attack playerone;
  playerone.set_stuff(40,30);
  cout << "player attack: " << playerone.playerattack() << endl;
  cout << "enemy defence: " << playerone.enemydefence() << endl;
  cout << "enemy hp left: " << playerone.hit() << endl;

  system("pause");
  return 0;
}
Professionally... for one, don't use system("pause") (thread in the beginner forum describing why). Two, comment your code. You have no idea how helpful it is when you can look at a code and see all of the comments of what is going on in the code, rather than having to figure it out yourself. Three... you could use a constructor to set playeratt and enemydef versus calling a function. Four, it would be nicer if there was a function inside of the class attack which displayed the player attack, enemy defense, and the enemy HP left. That, versus having to display it in main(). Five, some people will nitpick you for using namespace std versus placing std:: in front of cout, et cetera... Actually, if you do create a function inside of attack that displays all the information needed to be displayed, you can get rid of all of those functions that just state the values from the code. It will make it more compact in the end.
I hit a lot, but the enemy doesn't die.
yeah i guess i should have commented sorry :/ i will if i ever post again. and yeah its been a LONG time since ive done any of this i had completely forgot about how bad using system("pause"); was.and what do you mean by " you could use a constructor to set playeratt and enemydef versus calling a function."?

and for " a function inside of attack that displays all the information needed to be displayed, you can get rid of all of those functions that just state the values from the code. It will make it more compact in the end." well if i did make this bigger just for practice i would need those separate so i could set them to what i need and displayed in different ways, orders.
Why is 'attack' a class? Shouldn't it be behaviour? Something like this:

1
2
3
4
5
6
7
8
9
10
11
struct enemy 
{
     // ...    
};

struct player
{
    // ....
    
    int attack( enemy& an_enemy ) ;
};


Note: There could be cogent, compelling reasons for representing an attack as an object. What are those?

Topic archived. No new replies allowed.