Public Group

#### Archived

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

# Understanding Classes

This topic is 5709 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
..You can always try it before you ask it...

.lick

##### Share on other sites
That''s fair enough, but that only really answers question 2...

Thanks though,

--hellz

##### 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 on other sites
Thanks mate, that''s what I was looking for.

--hellz

##### Share on other sites
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:
------

Regarding question three..

  class foo{public:    foo(int x_) { x = x_; } // ERROR can''t assign value to a constprivate:    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 on other sites
The above post was mine.

##### 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

1. 1
2. 2
Rutin
19
3. 3
khawk
19
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633771
• Total Posts
3013761
×