Using data from other classes
Author |
Message |
DrummaBoyFB
|
Posted: Sun Apr 18, 2010 7:33 pm Post subject: Using data from other classes |
|
|
Hi, I'm trying to make a text based battle game and I'm having trouble with one part.
I want to be able to interact with other classes but I don't know how to and I'm not to keen on how to use
Inheritance. Whenever I try to use inheritance I understand that it copies the contents of the class file into the
the other class.
But I declare the values for my variables my classes in a .cpp file. so the other classes don't recognize the other classes files.
I don't know if there's another way to do it or if somebody could explain how to use inheritance properly.
I'm going to post my code to show you my problem.
(where it's bold, underlined, or both is where the problems are)
// player.h
#pragma once
#include "stdafx.h"
#include <iostream>
#include "enemy.h"
using namespace std;
class player
{
public:
player (int atkvalue, double totalexp, int healthvalue);
void attack();
void defend();
int heal();
void level();
void death();
void hurt ();
void expgain ();
double expvalue;
int totalhealth;
double totalExp;
int totalatk;
int damage;
int health;
double exppoints;
int lives;
int defense;
int healthgain;
int healthloss;
};
//player.cpp
#pragma once
#include "stdafx.h"
#include <iostream>
#include "player.h"
#include "enemy.h"
using namespace std;
player::player (int atkvalue, double totalexp, int healthvalue)
{
damage = totalatk / 4;
health = healthvalue;
exppoints = 0;
lives = 3;
defense = edamage / 4;
healthgain = 10;
healthloss = 0;
totalhealth = healthvalue;
expvalue = etotalatk / 3;
totalExp = totalexp;
totalatk = atkvalue;
}
void player::attack ()
{
cout << "You've inflicted " << damage << " points of damage to your opponent \n";
health -= damage;
}
void player::defend ()
{
cout << "You've been inflicted by " << defense << " points of damage \n" ;
health -= damage;
}
void player::hurt ()
{
cout << "You've been inflicted by " << edamage << "points of damage \n";
}
int player::heal ()
{
health += healthgain;
if (health > totalhealth)
healthloss = health - totalhealth;
health = health - healthloss;
return health ;
}
void player::level ()
{
if (expvalue >= totalExp)
totalatk += 10;
totalhealth += 10;
expvalue = expvalue - totalExp;
totalExp += 10;
}
//enemy.h
#pragma once
#include "stdafx.h"
#include <iostream>
#include "player.h"
using namespace std;
class enemy
{
public:
enemy (int eatkvalue, int ehealthvalue);
void eattack();
void edefend();
int eheal();
void elevel();
void edeath();
void ehurt ();
void eexpgain ();
int etotalhealth;
int etotalExp;
int etotalatk;
int edamage;
int ehealth;
int edefense;
int ehealthgain;
int ehealthloss;
};
//enemy.cpp
#pragma once
#include "stdafx.h"
#include <iostream>
#include "player.h"
#include "enemy.h"
using namespace std;
enemy::enemy (int eatkvalue,int ehealthvalue)
{
ehealth = ehealthvalue;
edefense = damage / 4;
ehealthgain = 10;
ehealthloss = 0;
etotalhealth = ehealthvalue;
etotalatk = eatkvalue;
}
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Sun Apr 18, 2010 7:57 pm Post subject: RE:Using data from other classes |
|
|
Classes (as seen in C++ anyway) are blueprints for objects. That "damage" variable doesn't exist outside of an object. You haven't created an object, so...
Now, I could write a textbook here, but you need to read up a lot more on object-oriented theory before you can continue with this. |
|
|
|
|
 |
TerranceN
|
Posted: Sun Apr 18, 2010 8:19 pm Post subject: Re: Using data from other classes |
|
|
The edamage and etotalatk are not dependant on the player, but dependant on what enemy it is interacting with the player. This interaction is in methods like player::attack, and since it is dependant on the enemy that it is attacking, the player needs to know what enemy it is attacking. You can send the enemy variable to the player so that it can use the enemies values to determine things like the expvalue of defeating it. Something like this:
c++: | // somewhere in player.h
void attack(enemy &enemyToAttack);
// somewhere in player.cpp
void player::attack(enemy &enemyToAttack)
{
enemyToAttack.ehurt(/*some damage calculation here*/);
if (enemyToAttack.isAlive == false)
{
expvalue = enemyToAttack.etotalatk / 3;
totalExp += expvalue;
}
} |
Just so you know, its faster to pass large objects by refrence (by using the &), which is why I put that there. Hope that helps. |
|
|
|
|
 |
DrummaBoyFB
|
Posted: Tue Apr 20, 2010 7:37 pm Post subject: Re: Using data from other classes |
|
|
Thanks TerranceN. although your reply was short it was very helpful. and wtd I understand what you were trying to say, i thought about making it outside an object but i just wanted to know if there was another way to go about it. so thanks to both of you and wtd I will surely read up more on object oriented programming. I am just learning c++I'm just trying to make this little text based game so I can prove my teacher wrong. He basically told me I'm not capable and not ready to learn c++, and that it would make my brain explode and what not. I don't really want to get into detail about that cause i know you really don't care but anyways thanks sooo much. |
|
|
|
|
 |
DrummaBoyFB
|
Posted: Tue Apr 20, 2010 7:45 pm Post subject: Re: Using data from other classes |
|
|
oh. I have one more question. if i put
void player::attack (enemy &enemyhurt)
{
...
}
what do i put in the brackets when i call the attack function?
Ex: player drummaboy (40,40.0,40)
drummaboy.attack (what do i put in here?)
that's all i need to know and thanks in advance |
|
|
|
|
 |
TerranceN
|
Posted: Tue Apr 20, 2010 9:04 pm Post subject: Re: Using data from other classes |
|
|
Like I said before, you need an enemy to attack, so why not just create one:
c++: | player drummaboy(40, 40.0, 40);
enemy someEnemy(5, 25);
drummaboy.attack(someEnemy); |
No offense, but I would agree with your teacher that you are most likely not ready to learn C++ (it took me a few years and getting pretty good at a few different languages before I could fluently comprehend C++), of course go ahead and try, its always good to have a goal in your sights. Just don't feel bad if it gets a little too complicated, C++ sacrifices usability for slightly more speed, and there's a plethora of other languages that are much easier to learn, allowing you to actually create stuff and not worry about syntax, or pointer arithmetic, or memory management, and so on.
Also, if you want to learn C++ I recommend finding a good C++ book. They are usually more in-depth than online tutorials, and cover a wider range of material. |
|
|
|
|
 |
DrummaBoyFB
|
Posted: Wed Apr 21, 2010 3:53 pm Post subject: Re: Using data from other classes |
|
|
oh that was simple, lol thanks.
and i have learned turing and I'm pretty good at it if I say so myself.
I've made games and fairly complicated programs using turing and it just got boring to me.
I did in fact get a book on c++ that's how I know how to make classes and such.
I'm a fast learner and I have the mentalitly to keep trying even if I fail. So I'm going to keep going at it.
I like challenges and stuff of that sort, and just so you know, there's no offense taken, I just understand that, that's your opinion.
so thanks for all your help. |
|
|
|
|
 |
|
|