C++ Constructor And Destructor In Hindi

📔 : C++ 🔗

जब भी किसी class का Object Create / Destroy होता है तो उस Class के लिए दो method Automatically Run होते है जिन्हे Constructor और Destructor कहते हैं।


C++ में Constructor का principle Java से थोड़ा different है। Java में सिर्फ Constructor होता है जबकि C++ में Constructor और Destructor दोनों होते हैं।

C++ Constructor

Constructor special member function होते हैं, जब किसी class का Object बनाते हैं तो Constructor automatically run होता है। इसे run करने के लिए call करने जरूरत नहीं पड़ती है। लेकिन अगर Class का Object नहीं बना तो Constructor method run नहीं होगा।


इन्हे class name के साथ ही define किया जाता है , जो class का name होगा वही constructor का name रखना पड़ेगा।

C++ Constructor Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class Test{ 
  // define constructor as public because we going to create class object outside of the class.
  public :
  Test(){
    cout << "Test class object is being created.";
  }
};

int main() {
  // just create object.
  Test testObj;
  return 0;
}
Output
Test class object is being created.

Important
By Default ये Non Static ही होते हैं , इन्हे आप as a static member define नही कर सकते हैं। अगर ऐसा करते हैं तो Error Generate होगी ।
error: constructor cannot be static member function

Define Parameter

Normal methods की तरह ही इसमें भी parameters , define कर सकते हैं। basically इनका use define किये गए class variables में value initialize करना होता है।
See Example :

CopyFullscreenClose FullscreenRun
#include 
using namespace std;

// define class.
class Test{ 
  // define properties.
  string fname; string lname;
  public :
  Test(string f_name, string l_name){
    fname = f_name;
    lname = l_name;
  }
  
  // define a function to print name.
  void print_name(){
    cout << "Full Name : " << fname +" "+ lname;
  }
};

int main() {
  // pass arguments for constructor.
  Test testObj("Rahul",  "Rajput");
  testObj.print_name();
  return 0;
}
Output
Full Name : Rahul Rajput

C++ Desctructor

Destructor , तब automatically run होता है जब Initialize किया गया Class Object destroy होता है। हालाँकि जब program end होती है तो Class Object destroy हो जाते हैं। बाकी Constructor की तरह ही इसमें भी आप need के according parameters define कर सकते हैं। और यह भी non static हो होता है।


Desctructor भी class name के साथ ही define किये जाते हैं , जो class का name होगा वही constructor का name रखना पड़ेगा , बस name से पहले ~ sign use करना होगा।

C++ Desctructor Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class Test{ 
  // define constructor.
  public :
  Test(){
    cout << "Test class object created.";
  }
  
  // define destructor usig ~ sign.
  ~Test(){
    cout << endl << "Test class object destroyed.";
  }
};

int main() {
  // create object.
  Test testObj;
  return 0;
}
Output
Test class object created.
Test class object destroyed.

I Hope, अब आपको C++ में Constructor और Destructor के बारे में अच्छे से समझ आ गया होगा। :)

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers