• ntroduction to C++ constructors in Hindi
  • Different types of C++ constructor in Hindi
  • C++ destructors in Hindi 

    Introduction to Constructors 

    Constructor एक तरह का member function होता है जो class के objects को initialize करता है। इस function में आप class के data members (variables) को initial value assign करते है।

    जब भी किसी class का एक नया object create किया जाता है तो उस class का constructor call होता है और उसमें दिए गए सभी statements execute होते है।

    Constructors आपको किसी object को use करने से पहले जरुरी tasks perform करने की capability provide करते है। जैसे की आप class member variables को initialize कर सकते है, database/server से connection establish कर सकते है आदि।

    उदाहरण के लिए मान लीजिये आपने एक class create की है, जिसका नाम Product है। इस class में आपने 2 variables price और batchId declare किये है। जैसा की नीचे दिया गया है।

    class product
    {
        private:
                  int price;
                  int batchId;
    }; 

    इस class का जब आप object create करते है तो C++ के द्वारा default constructor call किया जाता है और price और batchId variables को 0 values assign होती है। Integers के लिए zero initial value होती है।

    यदि आप चाहे तो खुद भी constructor का यूज़ करते हुए इन variables को अपनी मन चाही value से या फिर उन values से assign कर सकते है जो user object create करते समय argument के रूप में pass करेगा।

    नीचे constructor के माध्यम से इन variables को different value assign करने का example दिया जा रहा है।

    class product
    {
       private:
                int price;
                int batchId;
       public:
                  product() // Constructor
                  {
                        price=300;
                        batchId=1005;
                   }
    };  
     
    ऊपर दिए गए उदाहरण में constructor के अंदर price और batchId variables को क्रमशः 300 और 1005 values assign की गयी है। जब भी इस class का object create किया जाएगा इन variables को ये values automatically assign हो जाएगी।

    जैसा की मैने आपको बताया आप चाहे तो user के द्वारा pass कि गयी values से भी class variables को initialize कर सकते है। ऐसे constructor को parameterized constructor कहते है। इनके बारे आपको आगे बताया जायेगा। लेकिन उससे पहले आइये देखते है की constructors की क्या विशेषताएँ होती है।  

    Characteristics of Constructors 

    निचे constructors की कुछ विशेषताएँ दी जा रही है जिनके बारे में जानकार आप constructors को और भी अच्छे तरीके से समझ सकते है। 
    1. Constructors को public access modifier section में declare किया जाना चाहिए। 
    2. Constructors को आप class के अंदर भी define कर सकते है और class के बाहर भी define कर सकते है। 
    3. Constructor का नाम class के नाम जैसा ही होता है।   
    4. जब objects create किये जाते है तो constructors automatically call हो जाते है। 
    5. Constructor के साथ कोई return type नहीं define किया जाता है और ये कोई values भी return नहीं करते है। 
    6. Constructors को कोई भी class inherit नहीं कर सकती है। हालाँकि एक derived class base class के constructor को call कर सकती है। 
    7. Constructors virtual declare नहीं किये जा सकते है। 
    आइये अब C++ के द्वारा provide किये गए different constructors के बारे में जानने का प्रयास करते है।

    Types of Constructors 

    नीचे आपको C++ में available अलग अलग तरह के constructors के बारे में बताया जा रहा है। आइये इनके बारे में जानने का प्रयास करते है।

    Default Constructor 

    Default constructor को आपको define करने की आवश्यकता नहीं होती है। जब आप कोई दूसरा constructor define नहीं करते है तो C++ के द्वारा default constructor automatically create करके call किया जाता है।    

    Normal Constructor 

    Normal constructor वे होते है जिनमें आप arguments नहीं pass कर सकते है। ऐसे constructors में आप variables को manually initialize करते है। Normal constructors का general syntax नीचे दिया जा रहा है। 

    class-name() 
    {
        // Statements to be executed
    }
            
    आइये अब इसे उदाहरण के माध्यम से समझने का प्रयास करते है। Introduction section में आपको normal constructor class के अंदर create करके बताया गया था। आइये अब देखते है की normal constructor को class के बाहर कैसे define करते है।      

    #include <iostream>
    using namespace std;

    class student
    {
       private:
                int rollno;
                int standard;
       public:  
               // Constructor declaration
               student(); 
               void displayStu();
    };

    // Constructor definition
    student:: student(void)
    {
       rollno=1;
       standard=10;
    }

    void student :: displayStu(void)
    {
        cout<<"Roll no is : "<<rollno<<endl;
        cout<<"Standard is :"<<standard<<"th"<<endl;
    }

    int main()
    {
         student obj1;
         obj1.displayStu();
         return 0;
    }

    ऊपर दिया गया program निचे दिया गया output generate करता है।

    Roll no is : 1
    Standard is : 10th  

    Parameterized Constructors

    Parameterized constructors में आप parameters define कर सकते है। Object create करते समय आप parameterized constructors को arguments pass कर सकते है जो की class variables को assign की जा सकती है। Parameterized constructors का general syntax नीचे दिया जा रहा है। 

    class-name(parameters list)
    {
        // Statements to be executed
    }  
       
    Parameterized constructors को नीचे उदाहरण के माध्यम से समझाया जा रहा है।

    class square
    {
         private:
                     int num;
         public:
                     // Declaring parameterized constructor
                     square(int n);

                     int calsquare();
    };

    // Definition of parameterized constructor
    square :: square(int n)
    {
       num=n;
    }           

    int square :: calsquare()
    {
        return num*num;
    }

    int main()
    {
       int result;

       // Passing argument to constructor 
       square obj1(5);

       result=obj1.calsquare();
       cout<<"Square of 5 is : "<<result;
       return 0;
    }

    ऊपर दिया गया program निचे दिया गया output generate करता है।

    Square of 5 is : 25 

    Copy Constructors

    यदि आप चाहे तो एक object की values से किसी दूसरे object को भी initialize कर सकते है। इसके लिए आप copy constructor यूज़ करते है। Copy constructor में argument के रूप में object pass किया जाता है।

    जब आप कोई नया object create करते है तो argument के रूप में जिस object को देते है उसी object की सभी values नए object में copy हो जाती है। इसीलिए इस तरह के constructor को copy constructor कहा जाता है।

    Copy constructor का general syntax नीचे दिया जा रहा है।

    class-name (class-name & obj)
    {
        // Statements to be executed

    आइये अब इसे एक उदाहरण के माध्यम से समझने का प्रयास करते है।

    class furniture
    {
        private:
                int price;
        public:
               
                // Parameterized constructor
                furniture(int n) 
                {
                     price=n;
                 }

                // Copy constructor
                furniture(furniture & obj)  
                {
                    price=obj.price;      
                }

                void showprice(void);
    };

    void furniture :: showprice(void)
    {
        cout<<"Price of this object is:"<<price;
    }

    int main()
    {
        furniture obj1(5);
        obj1.showprice();        

        // Passing obj1 as argument for copy constructor
        furniture obj2(obj1); 

        obj2.showprice();
    }

    ऊपर दिया गया program निचे दिया गया output generate करता है।

    Price of this object is : 5
    Price of this objects is 5 

    Destructors

    Destructors class के member function होते है जो execute होने पर object को destroy करते है। जैसे ही कोई object scope से बाहर जाता है तो destructor call हो जाता है और object destroy हो जाता है। Destructor automatically call होता है।    

    Constructor की तरह ही destructor भी class के नाम से ही define किया जाता है। लेकिन destructor में आप class के नाम से पहले tilde (~) symbol लगाते है। Destructors में कभी भी parameters define नहीं होते है। Destructors का general syntax नीचे दिया जा रहा है। 

    ~class-name()
    {
       // Statements to be executed
        
    आइये अब destructors के उपयोग को उदाहरण के माध्यम से समझने का प्रयास करते है। 

    #include <iostream>
    using namespace std;

    class MyClass
    {

      public:
             // Constructor 
             MyClass()
            {
                 cout<<"Object is created"<<endl; 
             }

             // Destructor
            ~MyClass()
             {
                    cout<<"Object is destroyed";
              } 
    };

    int main()
    {
          MyClass obj1;
          
          if(3>5)
          {
                MyClass obj2;
           }
    }      

    ऊपर दिए गए उदाहरण में जैसे ही compiler if statement के बाहर आता है तो obj2 destroy हो जाता है। ये program निचे दिया गया output generate करता है।

    Object is created 
    Object is destroyed