on Leave a Comment

C++ Constructors | Types of Constructors

Constructor is a member function of class that invoked automatically when object is created. Constructor task is to initialize the objects of its class. The name of constructor is similar to the name of class. Constructor has no return type, so return keyword is not used. Constructor must be instance member function it can never be static.

Declaration and definition of constructor in class:

Example,

class number
{
                int a,b;
                public:
                                number()                            //constructor
                                {
                                                a=0;
                                                b=0;
}
};

A constructor can defined either inside or outside class. Constructor is defined outside using scope resolution operator.

A simple program to understand constructor

#include<iostream>
using namespace std;
class number
{
    int a,b;
public:
    number()
    {
        cout<<"Constructor";
    }
};
int main()
{
    number a;
    return 0;
}

OUTPUT

Constructor

Constructor is automatically invoked when object is created. When object a is created, constructor automatically called and cout<<"Constructor"; is executing.

TYPES OF CONSTRUCTOR

(i)   Default constructor
(ii)  Parameterized constructor
(iii) Copy constructor

Default constructor

Default constructor does not contain any argument. We can use default constructor to initialize default value to an object. If we do not create any constructor in class, then compiler automatically create default constructor for class.

Example of default constructor

#include<iostream>
using namespace std;
class number
{
    int a,b;
public:
    number()
    {
       a=0;
       b=0;
    }
    void putdata()
    {
        cout<<"a="<<a<<"  b="<<b;
    }
};
int main()
{
    number a;
    a.putdata();
    return 0;
}

OUTPUT

a=0  b=0

Another example of default constructor is

#include<iostream>

using namespace std;

class number

{
    int a,b;
public:
    number()
    {    }
    void putdata()
    {
        cout<<"a="<<a<<"  b="<<b;
    }
};
int main()
{
    number a;
    a.putdata();
    return 0;
}

Output will show garbage value of a and b

Parameterized constructor

Parameterized Constructor contains parameters. There may be more than one parameterized constructor. 

An example of parameterized constructor is

#include<iostream>
using namespace std;
class number
{
    int a,b;
public:
    number(int x, int y)
    { a=0; b=0;   }
    void putdata()
    {
        cout<<"a="<<a<<"  b="<<b;
    }
};
int main()
{
    number a(1,2);
    a.putdata();
    return 0;

}

OUTPUT

a=1  b=2

Here we define a parameterized constructor in class. This parameterized constructor automatically invoked when object is created. It is important to pass parameters to this constructor. The line number a(1,2); passes values 1,2 to the x and y in the constructor. 

Copy Constructor

Copy constructor takes reference of object as parameter. Copy constructor copies the content of one object to other object. 

Example of copy constructor
#include<iostream>
using namespace std;
class number
{
    int a,b;
public:
    number(number &c)
    {
        a=c.a;
        b=c.b;
    }
    number()
    {

    }
    void setdata(int x,int y)
    {
        a=x;
        b=y;
    }
    void putdata()
    {
        cout<<"a="<<a<<"  b="<<b;
    }
};
int main()
{
    number a,b;
    b.setdata(1,2);
    a=b;
    a.putdata();
    return 0;
}

OUTPUT

a=1  b=2

It is important to note that if we don't make any constructor in class then compiler automatically makes two constructors namely default constructor and copy constructor. If we make default constructor in class, then compiler don't make default constructor but also makes copy constructor. Also if we make copy constructor in class, then compiler don't make copy constructor but also make default constructor. But if we make both copy constructor and default constructor in class, then compiler don't make any constructor.   

0 comments:

Post a Comment