constructor in c++

Constructor In C++ -: In the c++ programming language A constructors is a special member function whose task is to initialize the object of its class. It is a special because its name is the same as the class name. The constructors is involved whenever an object of its associated class is created. It is called constructors because it construct the values of data member of the class.

How the constructor declared and defined

//class with a constructor

class integer

{

   int a, b;

   public:

    integer (void);    // constructor declared
    ...........
    ...........
};

integer :: integer (void)  //constructors defined

{

   a=0;
   b=0;

} 

Characteristic for define the constructors -: There are some special character for defining the constructors function these are ..

  • Constructor should be declared in public section.
  • It can not inherited through a derived class can call the base class constructor.
  • These are invoked automatically when the object are created.
  • we can not refers to their address.
  • constructor do not have any return type not even void and therefore and they cannot return values.
  •  Like any other c++ function they can have default arguments.
  • Constructor can not be used as a member of a union.
  • constructor make ‘implicit value’ to the operator new and delete when memory allocation is require.
  • constructor can not be virtual.

 

Types of Constructors in c++ programming language-:  In the  C++ programming language constructor has many types these are

  1. Parameterized  Constructors
  2.  Multiple Constructor in class
  3. Default constructors
  4. Dynamic Initialization constructors
  5.  Copy Constructors
  6. Dynamic Construction
  7. Constructing Two dimension array.

 

Parameterized Constructors

The constructor integer(), defined above , initializes the data members of all the objects to zero. However, in practice it may be necessary to initialize
the various data elements of different objects with different values when they are created. C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are created. The constructors that can take arguments are called Parameterized.

 

 #include <iostream>

 using namespace std;

 class integer
 {
       int  m,  n;
    public:
        integer(int, int);           // constructor declared
        
        void display(void)
        {

             cout << " m = " << m << "\n";
             cout << " n = " << n << "\n";
         }

 };

 integer :: integer(int x, int y)     // constructor defined
 {

        m = x;   n=y;
 }

 int main()
 {

      integer  int1(10,60);   
                            // constructor called implicitly

      integer int2 = integer(30, 90);
                            // constructor called explicitly

      cout << "\nOBJECT1 << "\n";
      int1.display();

      cout << "\nOBJECT2" << "\n";
      int2.display();

      return 0;
 
 }

Output of this program

OBJECT1
M=10
N=60
OBJECT2
M=30
N=90

 

Multiple Constructor in a class-:  We are already using two type constructor. It is called the overloading constructors. these are the

  1. integer();  // there are no arguments
  2. integer(int,int);   // there are two arguments

In the first case the constructor itself supplies the data value and no value are passed by the calling program . in the second case the function call passes the appropriate value from main() c++  permits us to use both these constructors in the same class.

overloading constructors program-:

 #include <iostream>

 using namespace std;

  class complex

{

    float x, y;

       public:

complex (){}        //constructors has no arguments

complex ( float a ) { x=y=a;}  //constructor has one arguments 

complex (float real, float good){ x= real; y=good;}

friend complex  sum (complex, complex);

friend void show(complex);

};

complex sum( complex c1, complex c2)  //friend

{
   complex c3;

   c3.x= c1.x +c2.x;
   c3.y= c1.y +c2.y;
   return(c3);

}
void show(complex c)     //friend

{

   cout << c.x << " +j" << c.y <<"\n";

}

int main()

{

 complex A(2.7, 3.5);    
 complex B(1.6);
 complex C;

 c= sum(A,B);
 cout << "A = "; 
 show (A);
 cout << "B = ";
 show (B);
 cout << "C = ";
 show (C);

return 0;
}


Output of this program .

A = 2.7 + j3.5
B = 1.6 + j1.6
C = 4.3 + j5.1

 

 

Read more