Thread: Classes

  1. #1
    Me
    Join Date
    Jul 2006
    Posts
    71

    Classes

    Alright, well I have the following code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class weapon
       {
          private: int dmg,spd,mgc;
          public:  int readstats();
                   void setstats(int,int,int);
       };
    
    void weapon::setstats (int a, int b, int c)
       {
          a = dmg;
          b = spd;
          c = mgc;
       }
    
    int weapon::readstats()
       {
          cout<<"\nDamage: "<<dmg;
          cout<<"\nSpeed: "<<spd;
          cout<<"\nMagic: "<<mgc;
          cout<<"\nTotal Damage: "<<(dmg+spd)*mgc;
       }
    
    int main()
       {
          weapon sword, axe;
    
          sword.setstats(2,3,2);
             axe.setstats(4,2,0);
    
          sword.readstats();
             axe.readstats();
    
          cin.get();
          return 0;
       }
    When I call readstats it just prints seemingly random numbers for dmg,spd, and mgc. Why?

    Thanks,
    Relyt

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    void weapon::setstats (int a, int b, int c)
       {
          dmg = a;
          spd = b;
          mgc = c;
       }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    BTW, it would appear that a better design would be to set the stats in the constructor.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Code:
          a = dmg;
          b = spd;
          c = mgc;
    You are trying to do reverse thing.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Also try to give meaningful names to variables (preferably also at the point of declaration). If this goes into a large-scale game, would you really enjoy looking up the order of parameters (in the method definition) for setstats each time you want to use it? With some IDE-s you might get a "helpful" tip that the parameters of setstats are (int, int, int) instead of perhaps (int damage, int speed, int magic).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Me
    Join Date
    Jul 2006
    Posts
    71
    Quote Originally Posted by anon View Post
    Also try to give meaningful names to variables (preferably also at the point of declaration).
    I'll keep that in mind.

    Quote Originally Posted by anon View Post
    would you really enjoy looking up the order of parameters (in the method definition) for setstats each time you want to use it? With some IDE-s you might get a "helpful" tip that the parameters of setstats are (int, int, int) instead of perhaps (int damage, int speed, int magic).
    How would you make it so you wouldn't have to look it up? I'm confused...

    And to the others. Yeah... Thanks for your help. Dumb mistake.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> How would you make it so you wouldn't have to look it up? I'm confused...
    Some IDEs automatically display popup text as you are coding that shows you the parameters for the function. If you give more meaningful parameter names, then you know immediately that damage is first, then speed, then magic.

    Even if your IDE doesn't do that for you, it's much easier to see at a glance if the parameter names are damage, speed, magic, than if they are a, b, c.

  8. #8
    Me
    Join Date
    Jul 2006
    Posts
    71
    Quote Originally Posted by Daved View Post
    BTW, it would appear that a better design would be to set the stats in the constructor.
    What do you mean? How would you do that?

    >>Daved

    Ah, I see now. I wasn't sure what he meant. Thanks.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    A constructor is a function that's called when you create a new object of that class
    Add a public "weapon(int a, int b, int c)" function without a return value like so

    Code:
    class weapon
    {
          private: int dmg,spd,mgc;
          public:  weapon(int, int, int)
                      int readstats();
                      void setstats(int,int,int);
    };
    
    weapon::weapon(int a, int b, int c)
    {
          dmg = a;
          spd = b;
          mgc = c;
    }
    and instead of
    Code:
          weapon sword, axe;
    
          sword.setstats(2,3,2);
             axe.setstats(4,2,0);
    you can do
    Code:
          weapon sword(2,3,2), axe(4,2,0);

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  10. #10
    Me
    Join Date
    Jul 2006
    Posts
    71
    Quote Originally Posted by h_howee View Post
    A constructor is a function that's called when you create a new object of that class
    Add a public "weapon(int a, int b, int c)" function without a return value like so

    Code:
    class weapon
    {
          private: int dmg,spd,mgc;
          public:  weapon(int, int, int)
                      int readstats();
                      void setstats(int,int,int);
    };
    
    weapon::weapon(int a, int b, int c)
    {
          dmg = a;
          spd = b;
          mgc = c;
    }
    and instead of
    Code:
          weapon sword, axe;
    
          sword.setstats(2,3,2);
             axe.setstats(4,2,0);
    you can do
    Code:
          weapon sword(2,3,2), axe(4,2,0);
    Ah, thankyou. I had just figured that out a few minutes before I checked this. Thanks.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just a note: you might want to use a constructor initialisation list instead in this case. By the time control reaches the constructor body, member variables are initialised. So, h_howee's example assigns a, b and c to dmg, spd, and mgc respectively, but with an initialisation list you can initialise them with the arguments passed and do nothing in the constructor body, e.g.,
    Code:
    weapon::weapon(int a, int b, int c) : dmg(a), spd(b), mgc(c)
    {
    }
    a, b, and c are probably poor choices for variable names, since it indicates more of the order by which the arguments were passed rather than the purpose of the argument.
    Last edited by laserlight; 06-18-2007 at 10:37 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    One reason that using the constructor is better for this is that it is clear what you are trying to do. The point of the constructor is to initialize the object's data, so if you use it for that purpose it makes it easy for you or someone else to see what's going on. Another reason is that it makes it harder to make a mistake when using the class. This class might not make sense if there is no data saved in the damage, speed and magic variables. If you forget to call SetStats, then you can easily get a bug where the wrong information is used. If you make the constructor do it, you force yourself (and anybody else working with the code) to initialize it properly.

    BTW, I agree with laserlight, using the initializer list is better practice so check it out when you have a chance.

  13. #13
    Me
    Join Date
    Jul 2006
    Posts
    71
    Hmm, OK. I tried the initialization list:

    Code:
    class unit
       {
             int *health, *strength;
          public:
             unit (int, int);
             ~unit();
       };
    
    unit::unit(int hlth, int str) : health(new int), strength(new int), *health(hlth), *strength(str)
       {
          /*health = new int;
          strength = new int;
          *health = hlth;
          *strength = str;*/
       }
    
    unit::~unit()
       {
          delete health;
          delete strength;
       }
    That gives me an error about hlth and str not being initialized, though. Any idea why?

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    That gives me an error about hlth and str not being initialized, though. Any idea why?
    Where does it give you that error, what line of code does it point out?

    Maybe...
    Code:
    unit::unit(int hlth, int str) : health(new int(hlth)), strength(new int(str))
    {
    }
    Is there a reason these member variables are pointers?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Me
    Join Date
    Jul 2006
    Posts
    71
    They're pointers so the destructor works.

    http://www.cplusplus.com/doc/tutorial/classes.html

    It's what they suggested to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM