modifying a value in an object?

This is a discussion on modifying a value in an object? within the C++ Programming forums, part of the General Programming Boards category; I made the following code as practice with classes and objects. However, when I try to modify the value of ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    31

    modifying a value in an object?

    I made the following code as practice with classes and objects. However, when I try to modify the value of int hp inside the takedam function, it doesn't change anything outside the function. What is the best/most efficient way to change this value? I'm willing to read up on stuff, so if you point me in the right direction I'll try to figure it out.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <unistd.h>
    
    class zergling
    {
    	char name[20];
    	int hp;
    	int basedamage;
    	public:
    	zergling(int starthp, int dmg, char *label); //constructor declaration
    	~zergling() {}; //destructor declaration and definition.
    	
    	int attack(int damage, zergling target);
    	int takedam(int dam);
    };
    
    zergling::zergling(int starthp, int basedmg, char *label) //definition of the constructor.
    {
    	strcpy(name,label);
    	hp=starthp;
    	basedamage=basedmg;
    }
    
    int zergling::attack(int damage, zergling target)
    {
    	int dmgtot=rand()%3+target.basedamage;
    	printf("\nabout to inflict %d damage on a zergling, named %s\n",dmgtot,target.name);
    	target.takedam(dmgtot);
    }
    
    int zergling::takedam(int dam)
    {
    	hp-=dam;
    	printf("\na zergling named %s has taken %d dam, and now has %d hp left.\n",name,dam,hp);
    }
    
    
    int main()
    {
    	srand(time(NULL)); //starting the RNG base.
    	printf("\nThis is a game of chance.\n");
    	char name1[20];
    	char name2[20];
    	printf("Enter the name of player 1: ");
    	scanf("%s",name1);
    	printf("Enter the name of player 2: ");
    	scanf("%s",name2);
    	
    	zergling z1(35,5,name1);
    	zergling z2(35,5,name2);
    	
    	printf("\nLet the battle begin!\n\n");
    	
    	while (1)
    	{
    	z1.attack(3,z2);
    	sleep(1);
    	//z2.attack(3,z1);
    	//sleep(1);
    	}
    	
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Try "pass by reference".

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    I've used pointers before, so that makes a lot of sense. However, this was (probably) the closest I got to doing it right:

    Code:
    int zergling::takedam(int dam)
    {
    	int *temp=&hp;
    	*temp=hp-dam;
    	printf("\na zergling named %s has taken %d dam, and now has %d hp left.\n",name,dam,hp);
    }
    I have no idea why that won't work. Any tips?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    hp is a member variable, so what you did originally should work.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Problem is
    Code:
    int zergling::attack(int damage, zergling target)
    {
    	int dmgtot=rand()%3+target.basedamage;
    	printf("\nabout to inflict %d damage on a zergling, named %s\n",dmgtot,target.name);
    	target.takedam(dmgtot);
    }
    "target" is passed by value.

    To take a wild guess, this should work -
    Code:
    int zergling::attack(int damage, zergling &target)
    {
    	int dmgtot=rand()%3+target.basedamage;
    	printf("\nabout to inflict %d damage on a zergling, named %s\n",dmgtot,target.name);
    	target.takedam(dmgtot);
    }
    And this is C++, pointers != references.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    hmm... but why does attack take another zergling as a target, and yet the attack seems to have nothing to do with this zergling?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    I think what he meant is "this" zergling attacks the "target" zergling.

    "damage" is unused, though, and doesn't really make sense here.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    attack calculates the damage, then inflicts that much damage on the target via target.takedam.

    And
    int zergling::attack(int damage, zergling &target)
    worked perfectly. Thanks!

    edit: Damage will be used in the function later, at this point I'm actually using the targets damage to calculate how much damage is being done. I just wanted to see if it'd work and forgot to change it back... My bad.
    Last edited by sharrakor; 03-27-2009 at 12:13 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by sharrakor
    attack calculates the damage, then inflicts that much damage on the target via target.takedam.
    So, at some later point, you may want to have the damage inflicted depend on the attacker?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Quote Originally Posted by laserlight View Post
    So, at some later point, you may want to have the damage inflicted depend on the attacker?
    Exactly. Planning on implementing hydralisks and such, again just as practice so I've got some experience using these.

    By the way, on a completely different note, is there some sort of documentation I could read up on on why you would ever want to make something private? I don't see why you would need to hide something from your own code minus code injection...

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by sharrakor
    By the way, on a completely different note, is there some sort of documentation I could read up on on why you would ever want to make something private? I don't see why you would need to hide something from your own code minus code injection...
    Read: How Non-Member Functions Improve Encapsulation. The parts on "encapsulation" and "degrees of encapsulation" directly answer your question, but the article as a whole is what I was getting at when I asked about your attack member function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. Modifying CDocument Member Objects :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 04-12-2002, 05:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21