Number of Variables in Constructor

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 4
Reputation: Vallnerik is an unknown quantity at this point 
Solved Threads: 0
Vallnerik's Avatar
Vallnerik Vallnerik is offline Offline
Newbie Poster

Number of Variables in Constructor

 
0
  #1
Sep 24th, 2009
I have a question that pertains to any programming language but since C/C++ is my primary language I'll ask it here.

I'm designing a test program that will simulate the outcome of a 12 round fight between 2 boxers. Now my question is, is there a limit or number I should shoot for in regards to the number of member variables a class should have?

Since my objects are representing people, very specific types of people, I seem to have a large number of member variables. These represent everything from first and last name to strength, stamina, speed, agility etc.

The reason I ask this, is because my constructor is very large and I have like 40 variables in the constructor initialization list. I'm just wondered if this is a common occurrence in games or in other large applications? Or is my class too bloated?

I'm doing everything all my books have told me too do as far as breaking down the parts of a person into smaller logical parts, but none of the books I have read so far delve into what to do with classes that have this many members. I just wanted to know if I'm missing some fundamental concept I should be using to address the growing number of members my classes have.

Any help or opinions would be great!
Thx
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,889
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Number of Variables in Constructor

 
0
  #2
Sep 24th, 2009
Originally Posted by Vallnerik View Post
The reason I ask this, is because my constructor is very large and I have like 40 variables in the constructor initialization list. I'm just wondered if this is a common occurrence in games or in other large applications? Or is my class too bloated?
40 members are a bit too much IMO. Have you considered using more then 1 class? For example:

  1. class Human {
  2. public:
  3. Human::Human(std::string name, std::string last_name, int age);
  4. private:
  5. std::string name;
  6. std::string last_name;
  7. int age;
  8. //etc
  9. };
  10.  
  11. class Skills {
  12. public:
  13. Skills::Skills(int att, int defense) ;
  14. private:
  15. int attack;
  16. int defense;
  17. //etc
  18. };
  19.  
  20. class Boxer {
  21. public:
  22. Boxer(Human* hum, Skills* sk);
  23. private:
  24. Human* human;
  25. Skills* skills
  26. };
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: Vallnerik is an unknown quantity at this point 
Solved Threads: 0
Vallnerik's Avatar
Vallnerik Vallnerik is offline Offline
Newbie Poster

Re: Number of Variables in Constructor

 
0
  #3
Sep 24th, 2009
niek_e

Thank You for your response.

yes, I am currently inheriting my specialized persons classes from a more general human class, but my skills class and my attributes class are still really large. And your response brings about another question for me, should my skills class and my attributes class be nested within the more specialized boxer class or should I just use pointers in the constructor to point to them as you did?

Thx again
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: Vallnerik is an unknown quantity at this point 
Solved Threads: 0
Vallnerik's Avatar
Vallnerik Vallnerik is offline Offline
Newbie Poster

Re: Number of Variables in Constructor

 
0
  #4
Sep 24th, 2009
Ahh I see why I shouldn't nest them inside the CBoxer class, because if I ever wanted to add another specialized class like CKarateMaster, I would have to nest the attributes class and the stats class within each specialized class instead of just pointing to it in the constructor. Your way future proofs it from needless over-typing.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,282
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 158
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Number of Variables in Constructor

 
0
  #5
Sep 24th, 2009
Also you might want to think about abstract classs.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC