In this article types of constructor we give the information about the class of which the constructor is made, if when the object of the same class is created, then it is automatically called.

Types of constructor:

Constructor:

This is a member function of a special type, which is similar to the name of its class.
The class of which the constructor is made, if when the object of the same class is created, then it is automatically called.

Constructor Definition: 

“A Constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. ”

The constructor is invoked whenever an object of its associated class is created. The constructor has no return type. void also does not return. If a value has to be initialized, then it has to be accessed with the object by making it a member function separately. The constructor does this work only after creating the object. The virtual keyword is not used with the constructor.

The constructor can be easily understood on the basis of the following points:-

  • Constructors are used to initialize the values of the data members of the class.
  • The constructor which is there is a member function of the class.
  • The name of the class is also name of the constructors that is class name and constructor name is same.
  • Constructors do not have a return type also no void return type.
  • When the object of the class is created, the constructor is called automatically.
  • It is never virtual.
  • These declared always in the public section.
  • It is not inherited by the derived class.
  • There are three types of constructors – default, parameterized and copy constructors.
  • if we do not provide any constructors in the program then the compiler generates the default constructor for us.

Example for Constructor inside of class

class Prog

{

            private:

                      int i, j;

           public :

            Prog()              // Default Constructor

            {

                 i=102;

                 j=103;

            }

};

Constructor can also be defined outside the class.

Example for Constructor outside of class

class Prog

{

            private:

                      int i,j;

            public :

            Prog();   //Constructor declaration

};

Prog::Prog()   //Constructor Definition

{

           i=102;    //Constructor body;

           j=103;

}

Example for Constructor:-

// Source Code :

#include<iostream.h>

#include<conio.h>

using namespace std;

class Example

{

public :

Example()        //Constructor

{

cout<<“Constructor call automatically… “;

}

};

int main()

{

Example e;

return 0;

}

Output :

Constructor call automatically….

Types of constructor:

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Constructor Overloading

Default Constructor:

The Default constructor does not take any parameter or argument. The above program is of Default Constructor.

There is a class named ‘Prog’ in the program and its constructor has been created. When the object of Prog class is created then the constructor will be called automatically. As many times as the object of Prog class is created, then the constructor is called.

Source Code :

#include <iostream.h>

using namespace std;

class Prog

{

           public :

                       Prog()           // Default Constructor

                      {

                                 cout<<“Default Constructor.”<<endl;

                      }

};

int main()

{

             Prog s1, s2, s3;

             return 0;

}

Output :

1.Default Constructor.

2.Default Constructor.

3.Default Constructor.

  1. Parameterized Constructor: 

In Parameterized Constructor, parameters are passed to the constructor.

In Parameterized Constructor, different arguments are given to the constructor. There is no limit to arguments in this.

In Parameterized Constructor, the values ​​of the parameters have to be given in the object of the class.

For the program addition given below, two values ​​have been initialized and their addition has been done.

 Source Code :

#include <iostream.h>

using namespace std;

class Prog

{

private:

    int a, b, c;

public :

Prog(int x, int y)          // Parameterized Constructor

    {

    a = x;

    b = y;

    c = a + b;

   }

void display()

{

    cout<<“Addition of “<<a<<” and “<<b<<” is “<<c;

}

};

int main()

{

Prog a(15, 16);

a.display();

return 0;

}

Output :

Addition of 15 and 16 is 31

Types of constructor:

3. Copy Constructor:-

Object is created with Copy Constructor. In Copy Constructor, the first Constructor Object is copied to another Object. The data which is in the first object, the same data is copied to the second object.

Object is copied in two ways in Copy Constructor.

Prog a2(a1); //or

Prog a2 = a1;

Source Code :

#include <iostream.h>

using namespace std;

// declare a class

class Rect1

{

private:

double length;

double height;

public:

// initialize variables with parameterized constructor

Rect1(double l, double h)

{

length = l;

height = h;

}

// copy constructor with a Rect1 object as parameter

// copies data of the obj parameter

Rect1(Rect1 &obj)

{

length = obj.length;

height = obj.height;

}

double calculateArea()

{

return length * height;

}

};

void main()

{

// create an object of Rect1 class

Rect1  p1(10, 8);

// copy contents of p1 to p2

Rect1 p2 = p1;

// print areas of p1 and p2

cout << “Area of First Rectangle : ” << p1.calculateArea() << endl;

cout << “Area of Second Rectangle : ” << p2.calculateArea();

return 0;

}

OUTPUT:

 Destructor: 

Destructor This is a special type member function, which destroys the object. When the object goes out of scope, then the Destructor is automatically called.

Destructor; Similar to Constructor, but Destructor does not have parameters. Used with the ~(tilde) sign on the Destructor prefix.

Syntax for Destructor

~class_name()

{

            //statement(s);

}

Source Code :

#include <iostream.h>

using namespace std;

class Prog

{

    int a;

public:

    Prog(int x)                 // Constructor

{

    a = x;

    cout<<“Constructor is created.”<<endl;

}

    ~Prog()          //Destructor

{

    cout<<“Constructor is deleted.”<<endl;

}

void show()

{

    cout<<“Value of a : “<<a<<endl;

}

};

int main()

{

Prog a(5);

a.show();

return 0;

}

Output:

Constructor is cretaed.

Value of a : 5

Constructor is deleted.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *