Constructor-in-C++ .

In this session i have discussed constructor in C++ .


Constructor is a special type of function which is used to initialized the value of object . Constructor has no any return type . Constructor name same as the class name so it is called special function .

Types of Constructor :

1-Default constructor .

2-Parameterized constructor .

3-Copy construcotr .

Constructor syntax :

    class A
    {
        int x,y;
        public :
                A();//Constructor
    };
    A :: A() // Constructor defined
    {
        x=10;
        y=20;
    }

    

There are two ways by which you can call the constructor

1-Implicitly

    A obj(10,20);//Implicitly called
    

2-Explicitly

    A obj= A(10,20);//Explicitly called
    

Default Constructor :

A constructor that do not take any argument is called the default constructor . A default constructor is invoked when ever the object of the class is created .

Example :Write a program that demonstrate the use of default constructor in C++ .

    #include<iostream>
    using namespace std;  
    class A
    {
        int x,y;
        public :
            A(); // Constructor declared
            void show()
            {
                cout<<"\nX : "<<x;
                cout<<"\nY : "<<y;
            }
             
    };
    A :: A()
    {
            x=10;
            y=20;
            cout<<"Default constructor is called ";
    }
    int main()
    {
        A obj;
        obj.show();// function show called here
    }
    
   OUTPUT : Default constructor is invoked 
            X : 10
            Y : 20
   

Paramerterized Constructor :

A constructor that take the at least one argument that is called the parameterized constructor . You can initialized the different-diefferent value to object with the help of parameterized constructor .

Example :Write a program that demonstrate the use of parameterized constructor in C++ .

    #include<iostream>
    using namespace std;  
    class A
    {
        int x,y;
        public :
            A(int a,int b); // Constructor declared
            void show()
            {
                cout<<"\nX : "<<x;
                cout<<"\nY : "<<y;
            }
             
    };
    A :: A(int a,int b)
    {
            x=a;
            y=b;
            cout<<"\nParameterized constructor is called ";
    }
    int main()
    {
        A obj1(20,10); //Constructor called implicitly
        obj1.show();
        A obj2=A(50,40);// Constructor called explicitly 
        obj2.show();
        return 0;
    }
     
   OUTPUT : Parameterized constructor is called
            X : 20 
            Y : 10
            Parameterized constructor is called
            X : 50 
            Y : 40 

Constructor Overloading :

We can create multiple constructor in a class with different parameter list . It is known as constructor overloading . It is very similar to function overloading .

Example :Write a program that demonstrate the use of constructor overloading in C++ .

    #include<iostream>
    using namespace std;  
    class A
    {
        int x,y;
        public :
            A();//Default constructor
            A(int a); // Constructor with one argument 
            A(int a, int b);//Constructor with two argument
            
            void show()
            {
                cout<<"\nX : "<<x;
                cout<<"\nY : "<<y;
            }
             
    };
    A :: A()
    {
        x=0;
        y=0;
        cout<<"\nDefault constructor : ";
    }
    A :: A(int a)
    {
            x=a;
            y=0;
            cout<<"\n Constructor with one argument is called ";
    }
    A :: A(int a , int b)
    {
            x=a;
            y=b;
            cout<<"\n Constructor with two argument is called ";
    }
    int main()
    {
        A obj1();//Default constructor called 
        obj1.show();
        A obj2(10);//Constructor with one args called
        obj2.show(); 
        A obj3(10,20);//Constructor with two args called
        obj3.show();
        return 0;
    }
     
   OUTPUT : Default constructor is called
            X : 0
            Y : 0
            Constructor with one argument is called
            X : 10 
            Y : 0
            Constructor with two argument is called
            X : 10 
            Y : 20 

Copy Constructor :

A copy constructor is used to create an object by copy the values of variable from other object . The copy constructor take the instance of class object as argument .

Example :Write a program that demonstrate the use of copy constructor in C++ .

    #include<iostream>
    using namespace std;  
    class A
    {
       
       int x;
       public :
            A()
            {
                x=0;
                cout<<"\nDefault constructor ";
            }
            A(int a)
            {
                x=a;
            }
            A(A & obj) // copy constructor
            {
                x=obj.x;
            }
            void show()
            {
                cout<<"\nX : "<<x;
            }
    };
    int main()
    {
        A obj1;
        A obj2(10);
        A obj3(obj2);//Copy Constructor called
        obj1.show();
        obj2.show();
        obj3.show();
        return 0;
    }
    
   OUTPUT : Default constructor
            X : 20 
            Y : 10
               

Welcome to AndroidATPoint.

back image                     next image