Types of Constructors In C ++

 Types of Constructors In C ++



Constructors are special member functions that are automatically invoked whenever objects are created.

Constructors are of three types,

1. Default Constructors

2. Parametrized Constructor 

3. Copy constructors


1. Default Constructors:

Default constructor is a constructor which do not take any argument. This constructor do not have any parameter.

If we do not define any constructors in a class explicitly, compilers will automatically provide the default constructor implicitly.

Following example demonstrate the default constructor,


// C++ program to demonstrate default constructors

#include<iostream.h>

#include<conio.h>

class defaultconstructor

{

public:

defaultconstructor() //default constructors

{

a=30;

b=30;

}

void display()

{

cout<<” a+b=”<<endl;

cout<<a+b;

}

};

void main()

{

defaultconstructor obj;//object of class

obj.display();

}


2. Parametrized Constructor 

We can pass arguments to the constructors. When constructors are declared with arguments are called parametrized constructor. 

Parametrized constructors are used to initialized data members of different objects with different values.  

Following example demonstrate the parametrized constructor,


//program to demonstrate parametrized constructor

#include<iostream.h>

#include<conio.h>

class parameter

{

private:

int a, b ;

public:

parameter(int x, int y)

{

a=x;

b=y;

}

void display()

{

cout<<”a+b=”<<endl;

cout<<a+b;

}

void main()

{

parameter obj( 20,20); //two parameters are passed 

obj.display();

}


3. Copy constructors

Copy compilers are used when compilers has to create a temporary object of a class. Copy constructors are used when the initialization of an object by another object of the same class, when return of an object as a function value, stating the object as by value parameter of a function.

General syntax of copy constructors

class_name:: class_name(class_name &ptr)


for example,

class copy

{

public:

copy(copy &ptr);

};

In this example copy is the name of class. Ptr is pointer to class object copy.


The following segment of code demonstrate the copy constructor out side of class definition.


Fibonacci ::fibonacci( )// constructor

{

f0=0;

f1=1;

f2=f0+f1;

}

fibonacci::fibonacci(fobonacci &ptr) //copy constructor

{

f0=ptr.f0;

f1=ptr.f1;

f2=ptr.f2;

}


A Program to demonstrate copy constructor to display Fibonacci series in C ++ 


//Fibonacci series using copy constructor in C++ inside the class definition

#include<iostream.h>

#include<conio.h>

class copy

{

private:

int f0,f1,f2;

public:

copy() //constructor

{

f0=0;

f1=1;

f2=f0+f1;

}

copy( copy &ptr) //copy constructor

{

f0=ptr.f0;

f1=ptr.f1;

f2=ptr.f2;

}

void display()

{

f0=f1;

f1=f2;

f2=f0+f1;

cout<<” Fibonacci series is:”<<endl;

cout<<f2<<’\t’;

}

}; //end of class construction


void main()

{

copy obj;

for(int i=0; i<=50;i++)

{

obj.display();

}

}

टिप्पणी पोस्ट करा

0 टिप्पण्या