Archived

This topic is now archived and is closed to further replies.

hellz

Understanding Classes

Recommended Posts

Hey all, Before I start looking into making a game, I''m just going over C++ thoroughly to make sure I''ve got it all down, but I''ve run into some more questions, which again looking through the forum, haven''t quite been answered (or at least, in a sense that is clarified for me). I''ve been writing a lot of pointless programs (TM lol) all morning, and here''s something based on using class constructors:
  
#include <iostream.h>

class Player
{
	private:
		int strength;
		int agility;
		int health;
	public:
		void getAttribs(void);

		Player(void);
		Player(int strength_, int agility_, int health_) :
			strength(strength_),
			agility(agility_),
			health(health_)
		{}
};

int main(void)
{
	Player hellz, hellz2(5, 5, 5);

	hellz.getAttribs();
	hellz2.getAttribs();

	return 0;
}

Player::Player(void)
{
	strength = agility = health = 10;
}

void Player::getAttribs(void)
{
	cout << "Attributes:"
	     << "\nStrength: " << strength
		 << "\nAgility: " << agility
		 << "\nHealth: " << health << endl;
}
  
My questions are as follows: 1.) If I define a constructor to take parameters, am I always going to need a second constructor to cope with instantiating objects without parameters? Or is there a way to use an initialisation list constructor (as above), to cater for both scenarios? 2.) Is it possible to have varying parameter lists with initialisation list constructors, using stdarg.h? 3.) I saw someone mention the following in another post and I wondered if I should always aim to use parameter initialisation lists: "Not only is it used to initialise the (possible) base class, it is also used to initialise member variables by directly calling their constructors. If you assign to them in the body of the constructor, they will actually first be default-constructed THEN assigned to, which is not only inefficient (you essentially initialise the variable twice), but may be impossible if the member variable is a constant, a reference, or doesn''t have a default constructor. Initializer lists are your friends." --Fruny Thanks for your time. --hellz

Share this post


Link to post
Share on other sites
quote:
Original post by hellz
1.) If I define a constructor to take parameters, am I always going to need a second constructor to cope with instantiating objects without parameters? Or is there a way to use an initialisation list constructor (as above), to cater for both scenarios?



The only way I could see getting around this is if all your arguments in the constructor have defaults.

quote:

2.) Is it possible to have varying parameter lists with initialisation list constructors, using stdarg.h?



No idea, sorry.

quote:

3.) I saw someone mention the following in another post and I wondered if I should always aim to use parameter initialisation lists:



Yes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by rypyr
The only way I could see getting around this is if all your arguments in the constructor have defaults.


I''m going to clarify on what he meant just in case you don''t understand default values.


  
void someFunc(int x = 100, int y = 100)
{
cout << "Your coordinates are: " << x << '','' << y << endl;
}

int main()
{
someFunc();
someFunc(2);
someFunc(34,44);
}

Output:
------

Your coordinates are: 100,100
Your coordinates are: 2,100
Your coordinates are: 34,44


Regarding question three..


  
class foo
{
public:
foo(int x_) { x = x_; } // ERROR can''t assign value to a const


private:
const int x_;
};

This code will not work because you are trying to assign a value to a const that has already been default constructed. You use the initializer list to assign the value while it''s being constructed.


Share this post


Link to post
Share on other sites
Ah, that makes perfect sense! Thank you very much folks. I''ll start coding some more ''pointless'' class programs now and see what I can break.

Thanks again,

--hellz

Share this post


Link to post
Share on other sites