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,
- Default constructor
- Parameterized constructor
- Copy constructor
- 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
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 CallingIn 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
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
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