Skip to main content

Constructor in c++

Hello friends, in this post today, we learn about  what is constructor in c++ and types of constructor. so lets start...

Constructor, default constructor, parameterized constructor, copy constructor
Constructor in c++




Constructor:-

 A constructor is a “special” member function which initializes the objects of class.

 Constructor is used to solve problem of initialization.



Properties of constructor:-


Constructors are used to construct the object of the class.


Constructor name must be same as class name.


Constructor is invoked automatically when objects of the class are created.


The constructors are always declared in the public section. If declared in the private section then objects are can only be created inside the member functions but serve no purpose.


It must be an instance member function , that is, it can never be static.


Constructors  do not have any return type not even void so they cannot return any value.


Constructor can also be overloaded.


Constructors cannot be inherited, but they can be called from the constructors of derived class.


Constructors cannot be virtual.


Addresses of constructors cannot be taken.


An object with a constructor cannot be used as a member of a union.


Constructor make implicit calls to operators new and delete in case memory allocation and de-allocation is to be performed.



Types of constructor :-

There are mainly three types of constructors as follows:


Constructor in c++, default constructor, parameterized constructor, copy constructor
Types of constructor



1. Default constructor:-

 Default constructor is the one which invokes by default when object of the class is created.


 It is generally used to initialize the value of the data members.


 It is also called no argument constructor.


Remember:- If we do not create any constructor in a class, then compiler creates one  default constructor (with no arguments) for us. And if we write a constructor with arguments or no-arguments then the compiler doesn't create a default constructor.


Example:

//Default constructor
#include <iostream>
using namespace std;

class Constructor_demo
{
    public:
    Constructor_demo()  // Default constructor
    {
     cout<<"Hello from Default constructor \n";
    }
};

int main()
{
   Constructor_demo d1,d2; // d1, d2 are object
   return 0;
}

Output:-

Hello from Default constructor 
Hello from Default constructor 


The other way to write the above program would be :-

#include <iostream>
using namespace std;

class Constructor_demo
{
    public:
    Constructor_demo(); // constructor declaration only
};

Constructor_demo::Constructor_demo() // constructor definition
 {
    cout<<"Hello from constructor using scope resolution operator\n";
 }

 int main()
{
   Constructor_demo d1,d2;
   return 0;
}

Output:

Hello from constructor using scope resolution operator
Hello from constructor using scope resolution operator


2. Parameterized constructor:-

Constructors that can take arguments are called parameterized constructors.


Sometimes it is necessary to initialize the various data elements of different objects with different values when they are created.


We can achieve this objective by passing arguments to the constructor function when the objects are created.


Example:

// parameterized constructor
#include <iostream>
using namespace std;

class arg_constructor
{
    private:
    int a,b;

    public:
    arg_constructor()
    {
      a=b=0;
      cout<<"Zero argument constructor called\n";
      show();
    }

    arg_constructor(int xint y)
    {
      a=x;
      b=y;
      cout<<"Two argument constructor called\n";
      show();
    }

   void show()
   {

    cout<<"a="<<a<<"\tb="<<b<<endl;

   }
};

int main()
{

  arg_constructor d1;
  arg_constructor d2(10,20);
  return 0;

}

Output:

Zero argument constructor called
a=0 b=0
Two argument constructor called
a=10    b=20


3. Copy constructor:-

A copy constructor is used to declare and initialize an object from another object.


For example, integer(integer &i); OR integer I2(I1).


Constructor which accepts a reference to its own class as a parameter is called copy constructor.


Remember:-The compiler always provides a one copy constructor when it is not define in a class which does a member-wise copy between objects .


Example:

//copy constructor
#include <iostream>
using namespace std;

class copy_constructor
{
    private:
    int data;

    public:
   copy_constructor() // Default constructor
   {

    data =200;
    cout<<"Default constructor is called\n";

  }

   copy_constructor(int x) // parameterized constructor
   {

   data = x;
   cout<<"parameterized constructor called\n";

  }

   copy_constructor (copy_constructor &d) //copy constructor
   {
    
    datad.data;
    cout<<"copy constructor is called\n";

  }

   void show()
   {
     cout<<"data="<<data<<endl;
   }
};

int main()
{

  copy_constructor d1(10);
  copy_constructor d2=d1;
  copy_constructor d3;
  d3=d2;
  copy_constructor d4(d3);
  d1.show();
  d2.show();
  d3.show();
  d4.show();
  return 0;

}

Output:

parameterized constructor called
copy constructor is called
Default constructor is called
copy constructor is called
data=10
data=10
data=10
data=10


All types of constructor in one example:-

#include <iostream>
using namespace std;

class rectangle
{
    int lengthwidth;

    public:
    rectangle() // Default constructor
   {
    length=0
    width=0;
    cout<<"Default constructor is called\n";

   }

    rectangle(int lenint wid) // Parameterized constructor
   {
     length=len
     width=wid;
     cout<<"parameterized constructor is called\n";
   }

   rectangle(rectangle &r) // Copy constructor
   {
    lengthr.length;
     widthr.width;
     cout<<"copy constructor is called\n";
   }

   void show()
   {
     cout<<"Length="<<length<<endl;
     cout<<"Width="<<width<<endl;
   }

};

int main()
{
  rectangle r1; // Invokes default constructor
  rectangle r2(10,10); // Invokes parameterized constructor rectangle
  rectangle r3(r2); // Invokes copy constructor
  r1.show();
  r2.show();
  r3.show();
  return  0;
}

Output:-

Default constructor is called
parameterized constructor is called
copy constructor is called
Length=0
Width=0
Length=10
Width=10
Length=10
Width=10



I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.

Thank you so much for reading. And take care about yourself and your family.




Comments

Popular posts from this blog

Function with Default arguments in c++

Hello friends, i n this post today, we learn about   Function with Default arguments in c++ .  so lets start... Function with default arguments  Function with default arguments:- In C ++ it is possible for a function not to specify all its arguments. Some of the arguments  may be specified their default values at the time of declaring the function.   Default values are specified when the function is declared. We must add default arguments from right to left. We cannot provide a default value to a particular argument in the middle of an argument list. Default arguments are useful in situations where some arguments always have the same value. For Example, passing marks. In a function with default argument, if one argument is default, all successive arguments  must be default. We cannot provide default values in the middle of the arguments or towards  l eft side. We provide few examples: 1. void fun(int x, int y = 20, int z=35); (valid) 2. void fun(int x,...

function overriding in c++

Hello friends, i n this post today, we learn about w hat is function overriding  in c++ .  so lets start... Function overriding in c++ Function overriding:- In function overriding we write the same function which is defined in base class, we write the same function in derived class with same parameter is known as function overriding.  If base class and derived class have member functions with same name and arguments.  If you create an object of derived class and write code to access that member function then, the member function in derived class is only invokes. Means the member function of derived class overrides the member function of base class. This is called function overriding or method overriding in C++. Example: //Function overriding #include <iostream>   using   namespace   std ; class   A {    public:    void   display ()   {     cout << "This is parent ...