OOAD Functions Q/A #6


Question:What do you mean by constructor? explain the various features and types of constructors available in C++.

Answer:

A constructor can be defined as a special member function which is used to initialize the objects of the class with initial values. It is special member function as its name is the same as the class name. It enables an object to initialize itself during its creation. This is termed as automatic initialization of objects.

Features of constructors:

Constructor have following special features:

  1. A constructor name must be same as that of its class name.

  2. Constructors are called automatically when the objects are created.

  3. Constructors should be declared in the public section to be availabile to all the functions.

  4. Constructors do not have return type , not even void and therefore they can not return value.

  5. Constructors can have default arguments as other C++ functions.

  6. Constructors can not be inherited.

  7. Constructors can not be static.

  8. Constructors can not be virtual.

  9. The address of a constructor can not be referred.

  10. Constructors are member functions so they can be overloaded.

Type of constructor

C++ has the following types of Constructors.

  1. Default Constructor

  2. Parameterized Constructor

  3. Multiple Constructor

  4. Constructor with default arguments

  5. Copy Constructor

  6. Dynamic Constructor

Default Constructor

A constructor without arguments is called default constructor.

Syntax for declaration of default constructor:

class classname;
{
public :
classname ( )// default Constructor
{
}
};

Syntax to invoke default Constructor

classname objectname;

Program example to explain default Constructor

#include<iostream>
using namespace std;
class Employee
{
   int empno;
   float salary ;
   public :
      Employee ( )// default Constructor
      {
         empno =210;
         salary =14000;
      }
      void showdata ( );
};
void Employee::showdata ( )
{
   cout<<"Employee number:" <<empno<<endl;
   cout<<"Salary:"<<salary<<endl;
}
int main ( )
{
   Employee emp ;
   emp.showdata( );
   return 1;
}

Output

Employee number :210
Salary :14000

Parameterized Constructor

The constructors having arguments as parameters are known as parameterized Constructors. These type of constructors are useful when the programmer wants to initialize various data elements of different objects with different values during their creation.

Syntax for declaration of parameterized Constructor

class class_name
{
   public:
   class_name (argument _list)// parameterized Constructor
   {

   }
};

To pass initial values while creating an object, one can select any one of the two ways.

  • Call the constructor implicity. The syntax is

    class_name object_name (argument list );
    

    It is also called shorthand method and it is used frequently.

  • Call the constructor explicity. The syntax is

    class_name object_name = class_name (argument list );
    

Program example to explain parameterized constructor

#include <iostream>
using namespace std;
class Employee 
{
   int empno;
   float salary;
   public:
      Employee (int x, float y)// parameterized constructor
      {
         empno =x;
         salary =y;
      }
      void showdata( );
};
void Employee::showdata() 
{
cout <<"Employee number:"<<empno<<endl;
cout <<"Salary:" <<salary<<endl;
}
int main ( )
{
   Employee e1 (101,10000.0);  //implicit  calling
   e1.showdata( );
   Employee e2 = Employee (102,4000.0); // explicit calling
   e2.showdata( );
   return 1;
}

Output

 
Employee number : 101
Salary : 10000
Employee number : 102
Salary : 4000

Multiple constructor

When more than one constructor functions are defined in the same class it is called overloaded constructor. Invoking of constructor depends upon following.

  • Number of arguments

  • Data type of arguments

Program to implement multiple constructor

#include <iostream>
using namespace std;
class Sum
{
   int x,y;
   public :
      Sum (int a)// constructor1
      {
         x =a;
         y =a;
      }
      Sum (int a,int b)// constructor 2
      {
         x =a;
         y =b;
      }
      void display ( )
      {
         cout <<"sum ="<<(x+y) <<endl;
      }
};
int main ( )
{
   Sum s1(20); // constructor 1 called
   s1.display( );
   Sum s2 (20,30); // constructor 2 called
   s2.display ( );
   return 1;
}

Output

 
Sum =40
Sum =50

Constructor with default arguments

A constructor with default argument can be defined . In this type of constructor default arguments are specified in the constructor declaration itself.

Program example to explain constructor with default arguments

#include <iostream>
using namespace std;

class Interest 
{
   float principle , time ,rate, amount ;
   public:
   Interest (float p, float t, float r =6.5)// default argument 
   {
      principle = p;
      time = t;
      rate =r;
   }
   void display ( );
};
void Interest ::display ()
{
   cout <<"principle ="<<principle<<endl;
   cout <<"rate ="<<rate<<endl;
   cout <<"time ="<<time<<endl;
   amount =(principle*time*rate)/100;
   cout <<"amount ="<<amount<<endl;
}
int main ( )
{
   Interest obj1(5000,4);// default constructor
   obj1.display( );
   Interest obj2(5000, 4,7.5);
   obj2.display( );
}

Output

principle =5000
rate = 6.5
time =4
amount = 1300
principle = 5000
rate = 7.5 
time = 4
amount = 1500

In the above example , the data members principle and time of object obj1 are initialized to 5000, 4 respectively at the time off creation and the data member rate takes the default value 6.5. Data members principle, time and rate of object obj2 are initialized to 5000 , 4 and 7.5 respectively at the time of creation.

Copy constructor

Copy constructor is used to initialize an object from another object. In this way declaration of constructor involves object of same class in its argument list. For example , consider the following declaration:

Item obj1;
Item obj2(obj1);

Here the first statement creates an object obj1 of class Item. The second statement creates another object obj2 of class item and initializes it with the concepts of existing object obj1. The second statement can also be written as

Item obj2 = obj1;

Syntax for declaration of copy constructor

class class_name
{
public :
class_name (class_name &object1)
{

}
};

Syntax to invoke copy constructor

class_name object2 = object1;

Or

class_name object2 (object1);

Program example to explain copy constructor

#include <iostream>
using namespace std;
class Item 
{
   int x,y ;
   public:
      Item (int a, int b)
      {
         x= a;
         y= b;
      }
      Item (Item &k)// copy constructor defined 
      {
         x= k.x;
         y= k.y;
      }
      void display ( )
      {
         cout <<"x ="<<x<<endl;
         cout <<"y ="<<y<<endl;
      }
};
int main ( )
{
   Item obj1 (20, 30);
   Item obj2(obj1);// copy constructor called
   cout <<" First object" <<endl;
   obj1.display( );
   cout <<"Second object"<<endl;
   obj2.display ( );
}

Output

First object 
X=20
Y =30
Second object
X =20
Y = 30

Dynamic constructor

Dynamic constructor allocates memory to objects at the time of their creation. The memory is allocated with the use of new operator.

Syntax to invoke dynamic constructor

class_name* object2 = new class_name();
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements