Constructor overloading in cpp

If you have some confusion or you don't know what constructor is
go to this link 

what is constructor in cpp

Now, come to the topic ..what is constructor overloading  


constructor overloading  increase the flexibility of a class  

 see how
 => Using  constructor overloading we give different types of constructor
(differ by parameter within same class and thus we being able to initialize object in more than one way of same class .

Instead of one we can create many constructor in same class using the concept of constructor overloading...... 

understand  term using this code 

#include<iostream.h>
class room
  {
    int length ,width,height ;
     public:
   room() // default constructor 
    {

    }
   room(int x,int y) // constructor with
                      // two argument 
    {
      length=x;
      width=y;
    }
   room(int x,int y,int z) // constructor with 
                           // three argument 
    {
      length=x;
      width=y;
      height=z;
    }

  };
int main()
 {
   room obj; //calling default constructor 

   room obj1(20,10); // calling constructor with 
                     //     two argument 

   room obj2(20,10,12); // calling constructor with                         
                        //        three argument 
   }




 




Share on Google Plus

0 comments: