constructor

In this page, we will discuss following,

 

 

need of constructor

Class is a user defined data type but its behavior is of type built int data type meaning-

Built in data type.

 int x,y,z;

User-defined data type

 class-name x,y,z;

as we know, int is the keyword whereas class-name is an identifier. But in both we can declare variables where in int, declare x, y, z in normal variables and class, declare x, y, z class-variables or objects. both are called identifier.

let’s initialization them,

int x = 5; int y = 2; int z =4;

while initialization in class variable

class-name x,y,z;

x.member(5);
y.member(2);
z.member(4);

is from the type.

you can see the difference between the two where in the built in data type, the value (5,2,4) is assigned while the variable is declared. meaning no separate statement is written, whereas in class it does not.

In class, class-variable (x, y, z) are declared first, then value is assigned using the class member.

in class, first the class-variable (x, y, z) is declared, then the value is assigned through the class member, then the class-object is initialized.

Whereas in a constructor, the object can be initialized directly, in a way that it has automatic initialization.

You can understand this from the example below-

class constructor
{
    int a,b;
   public:
    void constructor(int x, int y)
    {
       a = x;
       b = y;
    }
};
constructor obj(3,4); // direct initialization

in constructor, if no value is assigned, the data-members of the class will be initialized automatically, whose value can be zero or garbage, just like built-in-data type.

 

Constructor

in simple language, the constructor is a function member which is used to initialize a class variable.

The constructor function is similar to the name class name, which automatically calls itself when the class object is declared.

 

Rule for declaring a constructor

The constructor is declared in the public section of the class whereas definition can define both inside or outside class (using the resolution operator like other normal members).

The constructor member name is the same as the class name. Which is a member of this.

As soon as the object of that class is created, whose member it is, it automatically gets called.

They do not return any value. Although it is not a return type, it is also not a void type function.

Constructor member cannot be inherited, although they can be called from the derive class.

Because the constructor is a function, so it can be overloaded.

 

Type of Constructor

  • Default constructor
  • Parameterzed constructor
  • Copy constructor

Default constructor

A constructor which does not pass any arguments, which means without arguments, is called the default constructor.

The program of Default constructor is given below-

In the program below construct() is a default constructor. The object of the class construct will be automatically called in execution as soon as it is created.

SYNTEX:

class construct
{
  public:
   construct()
    {
       body of constructor member;
    }
};

Example:

#include<iostream.h>
#include<conio.h>

 class construct
  {
    public:
     construct(void);   // constructor declaration in public mode
  }; 

    construct::construct(void)     // constructor definition outside class
     {
       cout<<"constructor called:\n";
     }

 void main()
  {
    clrscr();

    construct x;   // object x declare

    getch();
  }
OUTPUT:-
constructor called:

every time a new object of a class is created, the same time the constructor function will automatically call it, means object will initialize automatically. For Example-

construct x, y, z; Then,

constructor called
constructor called
constructor called

a default constructor makes a class visible in the program because as soon as the class object is created, it is automatically called, no separate function member (default constructor) has to be called,

whereas after creating an object in a class without default constructor, we also have to write the calling statement of its members.

The compiler automatically provides a constructor if no default constructor member has been declared in the program. It is reported in the perametrized constructor.

 

Parameterized constructor

The name itself implies that the constructor, which passes the parameters in the function, is called the parametrized constructor.

Here is the syntex below,

function-name(parameter-list);

remember here, function-name is same as class-name and parameter-list represent the data-type which maybe int , float, char type.

Example:-

#include<iostream.h>
#include<conio.h>

 class construct
  {
     int a,b;
    public:
      // construct(){ }      default constructor invoked by compiler
      construct(int,int);         //peramitrized constructor
  };

  construct::construct(int x,int y)           // constructor defination outside class
   { 
     a = x;
     b = y;
     cout<<"Total : "<<a+b<<endl;
   }

 void main()
  {
    clrscr();

    construct x(2,3);

    getch();
  }
OUTPUT:-
Total: 5

where the default constructor is used for the same values, the parametrized constructor is used to initialize different types of values.

 

use of constructor

  • constructor used to initialize a class object.
  • Because a constructor is automatically called as soon as a function class object is created, if a task is to be performed at the starting point of execution, it can be defined in the constructor body (see mini project).
  • However, class variables can be initialized even by a normal function, but where a normal member has to call each object, which can be complicated when the number of object increases, while the newly created object is automatically initialized in the constructor will go.
  • a good example of this is to establish a connection to the server in a website, (defining the connection in the constructor body) where the constructor is automatically called when loading each webpage.

constructor in-complete without destructor


previous- Friend function | Friend Class

next-Destructor in C++