What is constructor in cpp

To know about constructor firstly have a look on object and class , concept of constructor is generated due to object so its better to go to this below link if you are not familiar with object and class of cpp

class and object in cpp

Now ,constructor is  a function which give the memory existence of an object 


1.Constructor is used  to initialize the  data member of an object .

2.It's allocate the memory for object .

3.Constructor  has the same name as name of class.

 4.Whenever a object is created ,constructor is called 

5. Constructor doesn't return any value and it's get called whenever an object is created.

Ex-

#include<iostream>

using namespace std;

class Box

   {
int length,width,height ;
public: Box() // By default Constructor { } int volume() ; }; int Box ::volume() { cout <<"Enter the length height and width of box"; cin>>length>>width>>height ; return length*width*height; } int main() { Box b; 
//object created(constructor calling without parameter 

    
   cout<<"volume of the Box is :"<< b.volume();    
    return 0 ;

  }
                                                      output :  
                                           
 Enter the length height and width of box 
 
12 10 20  
                                                                                                 
 The volume of Box is :2400 

you can see here the constructor Box() ,with the same name of class but it's doing nothing. this is By Default constructor called by compiler
if we pass any value to this constructor that will be user defined constructor and benefit of user defined constructor is we can initialize the data member of object according to our need  .

Next program will explain this concept  .

#include<iostream>

using namespace std;

class Box

   {
     int length,width,height ;

     public:

     Box(int x,int y,int z)// user defined Constructor     {
       length=x;
       width=y;
       height=z;

     }
 int volume() ;
 };

int Box ::volume()

  {
       return length*width*height; 
  }

int main()
 { 
     Box b(10,20,12); // object created(constructor with parameter) 

    
   cout<<"volume of the Box is :"<< b.volume();    
    return 0 ;

  }

    
OUTPUT

volume of the Box is :2400

As we know a constructor is called whenever a object is created So in both example the constructor will be called but the only difference is
In 1st program the default constructor is called and in 2nd program user defined constructor is called .


we can categories the constructor in three types 

1.Default constructor  -Default Constructor is also called as Empty Constructor which has no arguments and It  is Automatically called when we creates the object of class  but Remember name of Constructor is same as name of class and Constructor never declared with the help of Return Type. Means we cant Declare a Constructor with the help of void Return Type. , if we never Pass or Declare any Arguments then this called as the Copy Constructors.


2. Parameterized Constructor  :-This is Another type  Constructor which has some Arguments and same name as class name but it uses some Arguments So For this We have to create object of Class by passing some Arguments at the time of creating object with the name of class. When we pass some Arguments to the Constructor then this will automatically pass the Arguments to the Constructor and the values will retrieve by the Respective Data Members of the Class.


class show 
 {
    show(int a,int b)  // constructor with parameter 
      {

      }   

 };


3.Copy constructor :-This is also Another type of Constructor. In this Constructor we pass the object of class into the Another Object of Same Class. As name Suggests you Copy, means Copy the values of one Object into the another Object of Class .This is used for Copying the values of class object into an another object of class So we call them as Copy Constructor and For Copying the values We have to pass the name of object whose values we wants to Copying and When we are using or passing an Object to a Constructor then we must have to use the & (Ampersand or Address Operator.

#include<iostream>
using namespace std;
class show 
  {  
     public:
    int a ,b;
  //copy constructor 
    show(show &arg)  // taking object as parameter
     {
        show obj ;
        obj.a=arg.a;  
      }
};
int main 
 {
    show ob1,ob2;
     ob1.a=10; 
     ob1.b=20;
    ob2=show(ob1) // sending ob1 as parameter 
   return 0 ;

}




           

Next article is Constructor overloading 


 





Share on Google Plus

0 comments: