so basically, have write program kind of rpg game there different types of creatures. each creature object of class creature, , has member variables hitpoints, strength, etc.
what having trouble writing function handles dealing , taking damage between classes.
#include <iostream> #include <string> #include <stdlib.h> using std::cout; using std::cin; using std::endl; using std::string; //creature base class (type 0) class creature { public: creature(int, int, int); int gettype() {return type;} int getstrength(){return strength;} int gethitpoints() {return hitpoints;} void sethitpoints(int); string getspecies(); void printstats(); void dealdamage(creature, creature); void takedamage(int); private: int type; int strength; int hitpoints; }; creature::creature(int t, int s, int hp) { type = t; strength = s; hitpoints = hp; } void creature::printstats() { cout << "this creature has: " << endl; cout << strength << " strength" << endl; cout << hitpoints << " hitpoints" << endl; cout << "and of type " << type << "(" << getspecies() << ")" << endl; } void creature::sethitpoints(int a) { hitpoints = a; } string creature::getspecies() { switch(type) { case 0: return "creature"; case 1: return "human"; case 2: return "elf"; case 3: return "demon"; case 4: return "balrog"; case 5: return "cyberdemon"; } } void creature::dealdamage(creature dealer, creature target) { srand(5); int damage; damage = rand() % strength+1; cout << dealer.getspecies() << " inflicts " << damage; cout << " damage " << target.getspecies() << "!" << endl; target.takedamage(damage); } void creature::takedamage(int damage) { sethitpoints((gethitpoints()-damage)); } int main() { creature monster1(0, 10, 100); creature monster2(1, 7, 90); monster1.printstats(); monster2.printstats(); monster1.dealdamage(monster1, monster2); monster2.printstats(); return 0; }
right now, output program gives me is:
this creature has: 10 strength 100 hitpoints , of type 0(creature) creature has: 7 strength 90 hitpoints , of type 1(human) creature inflicts 5 damage human! creature has: 7 strength 90 hitpoints , of type 1(human)
so dealdamage() function seems working, takedamage() function not changing hitpoints of creature taking damage.
any appreciated.
the problem void creature::dealdamage(creature dealer, creature target)
firstly, called "pass value". new "creature" objects constructed , values of "creature" objects call function copied them. procedure executes, , these temporary creature objects eol - original creature objects never touched.
you need take pointer or reference original object. unless intending support kind of 3-way fight, shouldn't requiring both items anyway -- non-static member function, it's operating in context of 1 of creatures, hence syntax invoked it: monster1.dealdamage(monster1, monster2);
change dealdamage this:
void creature::dealdamage(creature& target) // takes reference { //srand(5); <- cause rand() return same value. dont it. //int damage; <- don't separate declaration , assignment when can avoid it. int damage = rand() % strength+1; cout << getspecies() << " inflicts " << damage << " damage " << target.getspecies() << "!" << endl; target.takedamage(damage); }
you can use this->getspecies()
if find using getspecies() unclear.
instead of srand(constant value) try 'srand(time(null))', or better still, once @ start of program.
Comments
Post a Comment