C++ - Constructor




Constructors:

constructors is special member function whose task is initialize the object of it's class.
it is special because it's name is same that the class name.it is user define member function.constructor is declare as follows.
class integer
{
int m,n;
public:
             integer ();     //constructor declare
};
integer  :: integer()   //constructor define
{
m=n=0;
}

Properties of Constructors:
·         It allocates memory.
·         It initialize the data member automatically when they declear.
·         The class name and constructor name is always same.
·         Constructor do not return any value.(not even void).
·         Constructor can overload.
·         Like other C+ +  function ,constructor constructor can have default value.
·         constructors can not virtual.
·         we cant refer constructor using its address.

you may have question what is difference between constructor  and Normal  function.
·         Normal function return value.
·         Normal function  can be virtual.
·         We cant overload normal function.
·         Normal function can not  have
·         The class name and function name is not same.
·         Normal function are called explicitly.



Types of constructor:
1.       Default Constructor.
2.       Parameterise Constructor.
3.       Constructor with default Argument.
4.       Copy Constructor.
5.       Dynamic Constructor.

Default Constructor ::
                        The Constructor which do not have any Parameter is called Default Constructor.
Below Is the code  of default Constructor.

#include<iostream>
class sample
{
                  public :
                         sample()
                      {
                      cout<<"I am default constructor";
                      }
};

int main()
{
sample ob;
returm 0;

}



 Parameterised Constructor ::
C++ permits to pass argument to the constructors when the object created.
the constructor that can take arguments are called as parameterised constructor.
constructor can accept any kind of values as a formal parameter.

#include<iostream>
class sample
{
                  public :
                                                           sample(int x)
 {

   cout<<"I am Parametisezed constructor";
   cout<< "i have value "<< x
                                              }
};

int main()
{
sample ob(10);
returm 0;
}
Above code give the output:
I am   Parametisezed constructor
  i have value :10
Note:: When Constructor is parameterised Must provide Appropriate arguments.




Constructor with Default Argument  ::
             

#include<iostream>
class sample
{
                  public :
                                                           sample(int x=12);
};
sample :: sample(int num)  {
cout<<" you enter num : "            <<num;

}
int main()
{
int n;
int num;
sample ob();
cout<<"enter no ";
cin>>n;
sample ob1(n);
returm 0;
}



Copy Constructor ::
              the name copy suggest that the same reference can be used for anotherone, i.e either variable functionally element as we knlowwe can copy one variable into another.
the copy constructor takes reference to an object of same class as itself as argument.
syntax:
class name (class name & obj);
#include<iostream>
class sample
{
public:
     sample()
{
cout<<"default constructor";
}
sample(sample &p)
{
cout<<"copy constructor";
}
};
int main()
{
sample ob;
sample ob2(ob);  
// sample ob2= ob;
}




Dynamic constructor ::
When we create an object the memory allocation process done at the time of using constructor.
if we create object initially it create the memory which is not required so it results westage of memory initially.
since we create the object at the same time of construction that allocate memory according to size,this method called as dynamic constructor.

#include <cstdlib>
#include<iostream>
using namespace std;
class dynamic
{
    int i;
    float f;
public:
    void getdata();
    void putdata();
};
void dynamic::getdata()
{
    cout<<"enter the integer :-> ";
    cin>>i;
    cout<<"enter the float :-> ";
    cin>>f;
   
}
void dynamic::putdata()
{
    cout<<"\n the integer is "<<i;
    cout<<"\n the float is "<<f;
}
//malloc,new and delete is the dynamic memory allocation and dealoocation oprator.
int main(int argc, char** argv) {
    dynamic *ob;
    ob=new dynamic;  //when memory dynamically created we use                               pointer to point them because when memory                           dynamically created it
//generally created the address of that value so we need the pointer to point that value using its address.
    (*ob).getdata();
    /*ob->getdata();    
    ob->putdata(); */
    (*ob).putdata();
    delete ob;
    return 0;
}



Comments