Type Of Constructor

There are different type of constructors in C++

1 Overloaded Constructors
Besides performing the role of member data initialization, constructors are no different from other functions. This included overloading also. In fact, it is very common to find overloaded constructors. For example, consider the following program with overloaded constructors for the figure class

//Illustration of overloaded constructors
 //construct a class for storage of dimensions of circles.
 //triangle and rectangle and calculate their area
 #include <iostream.h>
 #include <conio.h>
 #include <circle.h>
 #include <rectangle.h>
 #include <triangle.h>

 Class figure
 {
 Private:
 Float radius, side1,side2,side3; //data members
 Char shape[10];
 Public:
 figure(float r) //constructor for circle
 {
 radius=r;
 strcpy (shape, “circle”);
 }
 figure (float s1,float s2) //constructor for rectangle
 strcpy
 { 
 Side1=s1;
 Side2=s2;
 Side3=radius=0.0; //has no significance in rectangle
 strcpy(shape,”rectangle”);
 }
 Figure (float s1, floats2, float s3) //constructor for triangle
 {
 side1=s1;
 side2=s2;
 side3=s3;
 radius=0.0;
 strcpy(shape,”triangle”);
 }
 void area() //calculate area
 { 
 float ar,s;
 if(radius==0.0)
 {
 if (side3==0.0)
 ar=side1*side2;
 else
 ar=3.14*radius*radius;
 cout<<”\n\nArea of the “<<shape<<”is :”<<ar<<”sq.units\n”;
 }
 };
 Void main() 
 {
 Clrscr();
 Figure circle(10.0); //objrct initialized using constructor
 Figure rectangle(15.0,20.6);//objrct initialized using onstructor
 Figure Triangle(3.0, 4.0, 5.0); //objrct initialized using constructor
 Rectangle.area();
 Triangle.area();
 Getch();//freeze the monitror
 }

2 Copy Constructor;
It is of the form classname (classname &amp;) and used for the initialization of an
object form another object of same type. For example,

 Class fun
 {
 Float x,y;
 Public:
 Fun (floata,float b)//constructor
 { 
 x = a;
 y = b;
 }
 
Fun (fun &amp;amp;f) //copy constructor
 {
cout<<”\ncopy constructor at work\n”;
 X = f.x;
 Y = f.y;
 }
 Void display (void)
 {
 {
 Cout<<””<<y<<end1;
 }
};

Here we have two constructors, one copy constructor for copying data value of a fun
object to another and other one a parameterized constructor for assignment of initial values
given.

3 Dynamic Initialization of Objects

In C++, the class objects can be initialized at run time (dynamically). We have the flexibility of providing initial values at execution time. The following program illustrates this concept:

//Illustration of dynamic initialization of objects
 #include <iostream.h>
 #include <conio.h>
 Class employee
 {
 Int empl_no;
 Float salary; 
Public:
 Employee() //default constructor
 {}
 Employee(int empno,float s)//constructor with arguments
 {
 Empl_no=empno;
 Salary=s;
 }
 Employee (employee &amp;emp)//copy constructor
 {
 Cout&lt;&lt;”\ncopy constructor working\n”;
 Empl_no=emp.empl_no;
 Salary=emp.salary;
 }
 Void display (void)
 {
 Cout&lt;&lt;”\nEmp.No:”&lt;&lt;empl_no&lt;&lt;”salary:”&lt;&lt;salary&lt;&lt;end1;
 }
 };
 Void main()
 {
 int eno;
 float sal;
 clrscr();
 cout&lt;&lt;”Enter the employee number and salary\n”; cin&gt;&gt;eno&gt;&gt;sal;
 employee obj1(eno,sal);//dynamic initialization of object
 cout&lt;&lt;”\nEnter the employee number and salary\n”; cin&gt;eno&gt;&gt;sal; 
 employee obj2(eno,sal); //dynamic initialization of object
 obj1.display(); //function called
 employee obj3=obj2; //copy constructor called
 obj3.display();
 getch();
 }

4 Constructors and Primitive Types
In C++, like derived type, i.e. class, primitive types (fundamental types) also have their constructors. Default constructor is used when no values are given but when we given initial values, the initialization take place for newly created instance.

For example,

float x,y; //default constructor used
int a(10), b(20); //a,b initialized with values 10 and 20
float i(2.5), j(7.8); //I,j, initialized with valurs 2.5 and 7.8

5 Constructor with Default Arguments
In C++, we can define constructor s with default arguments. For example, The following code segment shows a constructor with default arguments:

Class add
 {
 Private:
 Int num1, num2,num3;
 Public:
 Add(int=0,int=0); //Default argument constructor
 //to reduce the number of constructors
 Void enter (int,int);
 Void sum();
 Void display();
 }; 
//Default constructor definition
add::add(int n1, int n2)
 {
 num1=n1;
 num2=n2;
 num3=n0;
}
Void add ::sum()
{
Num3=num1+num2;
}
Void add::display ()
{
Cout<<”\nThe sum of two numbers is “--num3--end1;
}

Now using the above code objects of type add can be created with no initial values, one initial values or two initial values. For Example,

Add obj1, obj2(5), obj3(10,20);
Here, obj1 will have values of data members num1=0, num2=0 and
num3=0
Obj2 will have values of data members num1=5, num2=0 and num3=0
Obj3 will have values of data members num1=10, num2=20 and num3=0
If two constructors for the above class add are
Add::add() {} //default constructor
and add::add(int=0);//default argument constructor

Then the default argument constructor can be invoked with either two or one or no parameter(s).
Without argument, it is treated as a default constructor-using these two forms together causes ambiguity. For example, The declaration add obj; is ambiguous i.e., which one constructor to invoke i.e.,
add :: add() or add :: add(int=0,int=0) so be careful in such cases and avoid such mistakes.

SPECIAL CHARACTERISTICS OF CONSTRUCTORS
These have some special characteristics. These are given below:

  • (i) These are called automatically when the objects are created.
  • (ii) All objects of the class having a constructor are initialized before some use.
  • (iii) These should be declared in the public section for availability to all the
    functions.
  • (iv) Return type (not even void) cannot be specified for constructors.
  • (v) These cannot be inherited, but a derived class can call the base class
    constructor.
  • (vi) These cannot be static.
  • (vii) Default and copy constructors are generated by the compiler wherever
    required. Generated constructors are public.
  • (viii) These can have default arguments as other C++ functions.
  • (ix) A constructor can call member functions of its class.
  • (x) An object of a class with a constructor cannot be used as a member of a
    union.
  • (xi) A constructor can call member functions of its class.
  • (xii) We can use a constructor to create new objects of its class type by using the
    syntax.
  • (xiii) The make implicit calls to the memory allocation and deallocation operators
    new and delete.
  • (xiv) These cannot be virtual.

Try Now – PROGRAMMING IN C++ MCQs