Image

C++ - Constructor - Constructor

Constructor

A constructor is a special member function of a class which automatically invoked whenever object is created.

C++ allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor function. A constructor function is a special function that is a member of a class and has the same name as that class.

• Purpose : It is used to initialize member variable of invoking object.

• Application : Whenever we install any system software so at the time first of all the drivers of that software is install so as we know that the behavior of contructor.

Rules for constructing constructor
1. Constructor name and class mane must be same
2. It is always define within public section
3. Constructor overloading is allowed
4. It does not contain any return type not even void
5. It does not contain any return statement
6. It is automatically invoked whenever object is created
Types of constructor

C++ supports three types of constructor

1. Default constructor
2. Parameterized constructor
3. Copy constructor
1. Default constructor

A constructor having no arguments then such type of constructor is called default constructor.

A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is Default Constructible

Syntax:
	Class <class_name>
	{
		public:
			<class_name>()  //  default constructor
			{
			}
	};
e.g: WAP for default constructor
#include<iostream.h>
#include<conio.h>
class Area{
  		int len,bre;
  public:
	 Area(){
		  len=5;
	  	  bre=6;
	 }
	 void getlength(){
	  cout<<"\nEnter length and breadth ";
	  cin>>len>>bre;
	 }
	 int areacal()
	 {
	  return(len*bre);
	 }
	 void display(int temp)
	 {
	  cout<<"\nArea = "<<temp;
	 }
};

void main(){
  Area a1,a2;
  clrscr();
  int temp;
  cout<<"\nFor Default ";
  temp=a1.areacal();
  a1.display(temp);
  cout<<"\nFor Paramerised ";
  a2.getlength();
  temp=a2.areacal();
  a2.display(temp);
  getch();
}
O/P
For Default
Area =30
For Paramerised
Enter length and breadth
4 
5
Area=20

2. Parameterized constructor

A constructor having one or more than one arguments then such type of constructor is called parameterized constructor.

It may be necessary to initialize the various data elements of different objects with different values when they are created. This is achieved by passing arguments to the constructor function when the objects are created.

The constructors that can take arguments are called parameterized constructors.

Syntax:
	Class <class_name>
	{
		public:
			<class_name>(parameters)  //  parameterized constructor
			{
			}
	};
e.g: WAP for parameterized constructor
#include<iostream.h>
#include<conio.h>
class Cube{
  public:
	  int side;
  public:
	 Cube(int x){
	  side=x;
	 }
};

void main(){
  Cube c1(10),c2(20),c3(30);
  clrscr();
  cout<<c1.side<<"\t";
  cout<<c2.side<<"\t";
  cout<<c3.side<<"\t";
  getch();
}
O/P
10
20
30

3. Copy constructor

A constructor which replicates or duplicates an existing object then such type of constructor is called copy constructor. The copy constructor is used to: Initialize one object from another of the same type. Copy an object to pass it as an argument to a function.

e.g:
#include<iostream.h>
#include<conio.h>

class Code{

		int id;

	public:
		Code(){
		 id=10;
		}
		Code(int x){
		 id=x;
		}
		Code(Code &x){
		 id=x.id;
		}
		int show()
		{
		 return id;
		}
};
void main()
{
 Code c1;
 Code c2(3);
 Code c3(c2);
 Code c4=c3;
 Code c5;
 clrscr();
 cout<<"c1 = "<<c1.show();
 cout<<"c2 = "<<c2.show();
 cout<<"c3 = "<<c3.show();
 cout<<"c4 = "<<c4.show();
 cout<<"c5 = "<<c5.show();
getch();
}
O/P
C1=10
C2=3 
C3=3
C4=3
C5=10