Constructors and Destructors in C++

Estudies4you
2.8 CONSTRUCTORS AND DESTRUCTORS
Q19. Define constructor. List and explain the different types of constructors in C++.
Answer:

Constructor
A constructor is a member function which automatically gets called whenever the object of the class is instantiated. It has same name as that of its class and does not return any value.
Types of Constructors
The different types of constructors are,
  1. Default constructor
  2. Parameterized constructor
  3. Copy constructor
  4. Constructor with default arguments.
1. Default Constructor
A constructor that does not take any argument is known as default constructor. If no constructor is specified then a default constructor is called automatically. It does not accept any argument.
Example
#include<iostream.h>
#include<conio.h>
class Number //class declaration
{
private:
int x;
float y; //private member declaration.
public:
number(); //constructor declaration without any arguments
void display() // member function definition
{
cout <<“\n \t x =” <<x<<“\t y ="<<y;
}
};
Number:: Number() // constructor definition with no arguments
{
cout<<“\n Constructors containing no arguments.”;
x=y=NULL;
}
int main()
{
clrscr();
class number C; // defining object C
C.display();
getch();
return 0;

}
Output
Constructors and Destructors in C++,define Constructor in c++,syntax for Constructor in c++,use of Constructor in c++,types of Constructors in c++,Default constructor in c++,Parameterized constructor in c++,copy Parameterized constructor in c++,Constructor with default arguments in c++,examples of constructors in c++,define destructor in c++,difference between constructor and destructor in c++,Explicit Constructor Calling in c++, Implicit Constructor Calling in c++,types of Parameterized Constructor in c++,use of constructors in c++,define class in c++,syntax for class,class syntax,data abstraction in c++,data hiding in c++,what is object,difference between class and object,how to declare a object to class,what is scope of class in c++,types of scopes in c++,estudies4you,pointers in c++,static members of a class in c++,Static Member Function in c++, constructor and destructors in c++,difference between constructor and destructor,define constructor,define destructor,static member functions,data abstraction definition,short notes on data abstraction,types of abstraction in c++,what is ADT in c++,abstract data type in c++,information hiding in c++,


2. Parameterized Constructor
A constructor that takes arguments as its parameters is known as parameterized constructor. It is used to initialize the elements that take different values. Whenever objects are declared then their initial values are passed as arguments to constructor function. This can be done using the following two methods.
(a) Explicit constructor calling
(b) Implicit constructor calling

(a) Explicit Constructor Calling
In this method, the constructor is called explicitly by means of writing its name and passing arguments (if any).
Syntax
class_name object_name = constructor_name(arg1, arg2);
(b) Implicit Constructor Calling
In this method, the constructor is called implicitly by means of simply et the class name followed by object name and passing parameters without using the name of the constructor.
Syntax
class_name object_name(arg1, arg2);

Program
#include<iostream.h>
#include<conio.h>
class Myclass
{
int x, y;
public:
myclass(int p, int q)
{
x=p;
y=q;
}
void show( )
{
cout<<x<<" "<<y;
};
int main( )
{
myclass mc(3, 5);
me.show( );
getch( );
return 0;

}

Output
Constructors and Destructors in C++,define Constructor in c++,syntax for Constructor in c++,use of Constructor in c++,types of Constructors in c++,Default constructor in c++,Parameterized constructor in c++,copy Parameterized constructor in c++,Constructor with default arguments in c++,examples of constructors in c++,define destructor in c++,difference between constructor and destructor in c++,Explicit Constructor Calling in c++, Implicit Constructor Calling in c++,types of Parameterized Constructor in c++,use of constructors in c++,define class in c++,syntax for class,class syntax,data abstraction in c++,data hiding in c++,what is object,difference between class and object,how to declare a object to class,what is scope of class in c++,types of scopes in c++,estudies4you,pointers in c++,static members of a class in c++,Static Member Function in c++, constructor and destructors in c++,difference between constructor and destructor,define constructor,define destructor,static member functions,data abstraction definition,short notes on data abstraction,types of abstraction in c++,what is ADT in c++,abstract data type in c++,information hiding in c++,

3. Copy Constructor
Copy constructor is a constructor function used for copying objects. It has the same name as that of a class and takes a reference to a constant parameter.
This constructor copies one object on the other sequentially during the object declaration. This process of initializing is called copy initialization.

Program
#include<iostream.h>
#include<conio.h>
class Example
{
private:
int data;
public:
Example( )
{
}
Example(int a)
{
data = a;
}
Example(Example &c) ‘
{
data = c.data;
cout<<“Copy constructor invoked”;
}
void show()
{
cout<<data;
}
int main()
{
clrscr( );
Example e(20);
e.show( );
Example el;
el =e;
el.show( );
return 0;

}

Output
Constructors and Destructors in C++,define Constructor in c++,syntax for Constructor in c++,use of Constructor in c++,types of Constructors in c++,Default constructor in c++,Parameterized constructor in c++,copy Parameterized constructor in c++,Constructor with default arguments in c++,examples of constructors in c++,define destructor in c++,difference between constructor and destructor in c++,Explicit Constructor Calling in c++, Implicit Constructor Calling in c++,types of Parameterized Constructor in c++,use of constructors in c++,define class in c++,syntax for class,class syntax,data abstraction in c++,data hiding in c++,what is object,difference between class and object,how to declare a object to class,what is scope of class in c++,types of scopes in c++,estudies4you,pointers in c++,static members of a class in c++,Static Member Function in c++, constructor and destructors in c++,difference between constructor and destructor,define constructor,define destructor,static member functions,data abstraction definition,short notes on data abstraction,types of abstraction in c++,what is ADT in c++,abstract data type in c++,information hiding in c++,

4. Constructors with Default Arguments
C++ allows the programmer to invoke a constructor without specifying all of its arguments. In such situations, the constructor assigns a default value to the argument that doesn’t have a matching value. The assignment of default value is done during function declaration. Generally, a default argument is checked for type during its declaration and evaluated at the time of its invocation.

Program
#include<conio.h>
#include<iostream.h>
class Student
{
int rollno;
float marks;
public:
student(int p = 10, float q = 40)
{
rollno = p;
marks = q;
}
void show( )
{
cout<<“Roll no:”<<rollno<<endl;
cout<<“Marks:”<<marks<<endl;
}
};
void main( )
{
student x;
student y(1, 50);
clrscr( );
cout<<“Output using default constructor arguments:\n”;
x.show( ); :
cout<<“Output without default constructor arguments:\n”;
y.show( );
getch( );
}


Output
Constructors and Destructors in C++,define Constructor in c++,syntax for Constructor in c++,use of Constructor in c++,types of Constructors in c++,Default constructor in c++,Parameterized constructor in c++,copy Parameterized constructor in c++,Constructor with default arguments in c++,examples of constructors in c++,define destructor in c++,difference between constructor and destructor in c++,Explicit Constructor Calling in c++, Implicit Constructor Calling in c++,types of Parameterized Constructor in c++,use of constructors in c++,define class in c++,syntax for class,class syntax,data abstraction in c++,data hiding in c++,what is object,difference between class and object,how to declare a object to class,what is scope of class in c++,types of scopes in c++,estudies4you,pointers in c++,static members of a class in c++,Static Member Function in c++, constructor and destructors in c++,difference between constructor and destructor,define constructor,define destructor,static member functions,data abstraction definition,short notes on data abstraction,types of abstraction in c++,what is ADT in c++,abstract data type in c++,information hiding in c++,

To Top