Constructor and Destructor in C++

Constructor and Destructor in C++ are very important, without the help of Constructor we cannot invoke and it also helps us to do a lot of things


Constructor and Destructor in C++



What is a Constructor ?

[A Constructor is a "special type of member function" which has the same name as of the "Class".]
[Constructor initialize the Objects in C++ and can be automatically called when an Object is created i.e, it's main purpose is to initialize all data-members of a Class, in other words to construct "Objects". ]
[A Constructor has no return-type although it is an Member-Function ( Functions that are written inside the "Class" is known as "Member-Function" ) ]

Example for Member-Function:-
class ABC {
     public:  //Access Specifier
     void Fun( ) //function having return-type➝ "void" 
    {
       .......
       .......
               }
}; 


Example for Constructor:-
 class ABC {
     public:  //Access Specifier
     ABC( ) //Constructor having "no return-type"
    {
      .......
      .......
              }
};
There are three types of Constructor :-

  1. Default Constructor
    A Default Constructor is a constructor which has no argument, and if it has parameters then all the parameters has Default values. If their is not User-defined constructor inside the Class then the compiler automatically creates a Default Constructor.
    Example:-
     class ABC {
         public:  //Access Specifier
         int a,b; //Data-Members
         ABC( ) 
        {
         a=5; 
         b=10;
               }
    };
    
    int main()
    {
    ABC const; //here "const" is an Object
    cout<<"a:"<<const.a<<" b:"<<const.b; //to print 'a'&'b'
    }

       
    Output
    a:5 b:10


  2. Parameterized Constructor
    Sometimes we need to pass arguments in a Constructor and it is possible. A Constructor which has arguments in it which is used to initialize different Data-Members and Member-Function inside a Class  is known as Parameterised Constructor.
    Example:-
     class ABC {
         public:  //Access Specifier
         int a,b; //Data-Members
         ABC(int x,int y) 
        {
           a=x;
           b=y;      
                }
    };
    
    int main()
    {
    ABC const; //here "const" is an Object
    const(10,20); 
    cout<<"a:"<<const.a<<" b:"<<const.b; //to print 'a'&'b'
    }

       
    Output
    a:10 b:20


  3. Copy Constructor
    A Constructor which initialize an Object using another Object of the same Class
    In Simple ➝ If we have more than one Constructor in a single Class then one of the constructor is said to be a copy of another one.
    [ Note:- The parameters should be different between two Constructor which are of the same Class ]
    Example:-
     class ABC {
         public: 
         int a,b; //Data-Members of "int" type
         float a,b; //Data-Members of "float" type
    ABC(int x,int y) 
        {            //Parameter are "int" type
           a=x;
           b=y;      
                }
    ABC(float x,float y) 
        {                //Parameter are "float" type
           a=x;
           b=y;      
                }
    };
    
    int main()
    {
    int n,m;
    ABC const; //here "const" is an Object
    cin>>n>>m;
    const(n,m); 
    cout<<const.a<<const.b; //to print 'a'&'b'
    }
If 'n' & 'm' are of "int" type then the Constructor having the integer parameter will be executed but if the type of 'n' & 'm' is "float" then the Constructor having the float parameter will be executed.

What is a Destructor ?


[ Destructor are used to de-allocate the memory for a Class Object. ]
[ A Destructor is called automatically when an Object is destroyed or the Object passes out of the scope which has been created by the Constructor. ]
The name of the Destructor must start with the tilde character ( ). ]
[ Destructor has no return-type.  ]
[ Destructor also don't have any type of "parameters". ]
[ A Class can have only One-Destructor in it. Not more than one. ]

Example:-
class ABC{
        public: 
        ABC( )  //Constructor
        {
         cout<<"Constructor is called";
                    }
        ~ABC( )  //Destructor
        {
         cout<<"Destructor is called";
                    }
        void Mem_Fun( )  //Member-Function
        {
         cout<<"Member Function is called";
                    }
};

int main( )
{
 ABC obj;  //obj is the Object
 obj.Mem_Fun( ); //Member Function is called
 delete obj; //deleting the object
}


Output
Constructor is called
Member Function is called
Destructor is called



Introduction to Object-Oriented Programming language.

Introduction to C++.

For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time...

Post a Comment

0 Comments