By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,755 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

??can u check this program...

P: 20
#include<iostream.h>
class Room
{
int length;
int width;

public:
Room()
{}
Room(int l,int w=0) : width(w),length(l)
{
}
};

void main()
{
Room objRoom1;
Room obj2Room2(12,8);
}

This program is not giving any error.Now my question is : wat is the signifcance of this type of constructor declaration Room(int l,int w): width(w),legth(l)

y data member is defined as a function declaration and w and l as argument?
Note that commenting :width(w),length(l) does not give error while declaring width= w, length = l is giving error.

CAN ANYONE PLZZZ EXPLAIN THIS TO ME........
Oct 22 '06 #1
Share this Question
Share on Google+
2 Replies


arne
Expert 100+
P: 315
#include<iostream.h>
class Room
{
int length;
int width;

public:
Room()
{}
Room(int l,int w=0) : width(w),length(l)
{
}
};

void main()
{
Room objRoom1;
Room obj2Room2(12,8);
}

This program is not giving any error.Now my question is : wat is the signifcance of this type of constructor declaration Room(int l,int w): width(w),legth(l)

y data member is defined as a function declaration and w and l as argument?
Note that commenting :width(w),length(l) does not give error while declaring width= w, length = l is giving error.

CAN ANYONE PLZZZ EXPLAIN THIS TO ME........
The part in bold letters in your post is called a "constructor initializer list". It is a sequence of statements that tell the compiler to initialize the corresponding members with the values in brackets. It is 'better' (more efficient) than

Expand|Select|Wrap|Line Numbers
  1. Room(int l,int w=0) {
  2.         length = l;
  3.         width = w;
  4. }
  5.  
since the members are always initialized and only changed in the constructor's body. The net effect, of course, is the same.
Oct 22 '06 #2

P: 20
cooll...... I hav never come across this kinda of constructor declaration.
Thanks...!!
Oct 22 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.