468,484 Members | 1,612 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

??can u check this program...

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
2 1199
arne
315 Expert 100+
#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
Shana
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.

Similar topics

6 posts views Thread by Pierre-Yves | last post: by
3 posts views Thread by Brian Henry | last post: by
4 posts views Thread by John Smith | last post: by
12 posts views Thread by tshad | last post: by
reply views Thread by =?Utf-8?B?ZGF2aWQ=?= | last post: by
9 posts views Thread by chutsu | last post: by
173 posts views Thread by Marty James | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.