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

Inheritance in c++

Hello friends, in this post today, we learn about Inheritance in c++ concept and types of inheritance in c++ . Which  is very important. so lets start... Inheritance  Inheritance   Class can acquire the properties and methods of another class. Inheritance is the process of deriving a new class from an already existing class. In other words" Inherit all the properties and methods of class into another class, is called Inheritance." Inheritance  provides the idea of reusability i.e., code once written can be used again and again in number  of new classes. The new class is called derived class and old class is called base class. The new class can use all or some of the features of the already existing  class and the programmer can define his own members to the new class. Types of Inheritance In c++, there are 5 types of inheritance. Types of Inheritance Syntax of Inheritance:- class derived_classname : access_spacifier base_classname Where class is keyword used to create a clas

What is scope resolution operator in c++

Hello friends, in this post today, we will learn about What is scope resolution operator in c++ and how to use in our program. so let's start.... Scope resolution operator  Scope resolution operator :- Scope resolution operator which is also known as ( membership lable ), represnted as (::) . The scope resolution operator is employed to resolve or extend the scope of variable or functions. C++ is block structured language. We know that the same variable or function name can be used to have different meaning in different block. The scope resolution operator will refer value of worldwide variable or function from anywhere (also from inner block). Without scope resolution operator all variable will refer local value. Example for variabe: #include   <iostream>   using   namespace   std ; int  m= 10 ; int   main () {       int  m= 20 ;      {       int  k=m;        int  m= 30 ;     cout<< "we are in inner block \n " ;      cout<< "k=" <<k<