??can u check this program...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shana
    New Member
    • Oct 2006
    • 20

    ??can u check this program...

    #include<iostre am.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),lengt h(l) does not give error while declaring width= w, length = l is giving error.

    CAN ANYONE PLZZZ EXPLAIN THIS TO ME........
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by Shana
    #include<iostre am.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),lengt h(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 "constructo r 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

    Code:
    Room(int l,int w=0) {
            length = l;
            width = w;
    }
    since the members are always initialized and only changed in the constructor's body. The net effect, of course, is the same.

    Comment

    • Shana
      New Member
      • Oct 2006
      • 20

      #3
      cooll...... I hav never come across this kinda of constructor declaration.
      Thanks...!!

      Comment

      Working...