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

What is reference variable in c++

Hello friends, in this post today, we learn about What is reference variable in c++. and what is its major use  in c++ . so lets start... Reference variable in c++ Reference variable:- Reference variable is a new concept in C++. A reference variable is a variable which provides an alias (alternative name) for a previously defined variable or any constant. Reference variable is an internal pointer. Declaration of reference variable is preceded with & symbol ( but do not read it as ‘address of ‘). Reference variable must be initialized during declaration. It can be initialized with already declared variables only. Reference variables can not be updated. This mechanism is useful in object oriented programming because it permits the manipulation of objects by reference, and eliminates the copying of object parameters back and forth. It is also important to note that references can be created not only for built-in data types but also for user- defined data types. Syntax: Data_...

Access modifiers in c++

Hello friends, in this post today, we will learn about Access modifiers in c++ and how to use it in data hiding. so let's start.... Access specifiers (or visibility labels)  that control the flow of data member and function outside the class. There are 3 types of access levels which are:- Access specifiers public: - Public is keyword . The class members ( functions or data) , which are declared as public can be accessed from outside the class. private:- Private members  of the class can only accessed by   the members functions with in that class. Private is a keyword which is used in Data hiding and Data abstraction. It is also used in Encapsulation. protected :- Protected is  keyword. This access specifier play a very important role in Inheritance. The class member s, which are declared as Protected   can be   accessed within the class   and   from derived class but cannot be accessed from any other class. NOTE :- If we do not declare any ...