हिंदी माध्यम नोट्स
Categories: c++ language in hindi
destructor in c++ language , what is destructor in c++ explain in hindi with example program
what is destructor in c++ explain in hindi with example program , destructor in c++ language :-
इससे पहले के article मे , c++ language के सबसे महतवपूर्ण concept constructor को discuss किया है | अब इस article मे c++ language के दुसरे महतवपूर्ण concept destructor को discuss किया था |
C++ destructor
जब किसि constructor को किसिस object को create करने के लिए किया जाता है तब किसी प्रोग्राम से किसी object की responsibility को ट्रैक किया जाता है | अतः प्रोग्राम किसी प्रोग्राम को आटोमेटिक call करता है जिसे destructor कहते है | destructor का use किसी object को clear करने के लिए use किया जाता है | अतः इस destructor को useful application होता है |
जब किसी constructor new का use किसी object के लिए memory को allocate करने के लिए use किया जाता है तब destructor , delete command को किसी object को डिलीट करने के लिएर किया जाता है | destructor का use किसी memory को free करने के लिए किया जाता है |
stock construtor का use किसी new क्कोम्मंद को use करना complusy नहीं होता है | इसलिए किसि stock destructor मे भी कोई भी task को perform नहीं किया जाता है |अतः complier destructor को implicit declare नहीं किया जाता है | लेकिन destructor को explicit declare किया जाता है तब इसका syntax निन्म होता है :
constructor की तरह destructor का sepcial name होता है | इसे tilde operator (~) को use किया जाता है | जैसे stock destructor को ~stock से call किया जता है | constructor की तरह descructor की तरह कोई भी value return नहीं होती है | और इसका कोई भी declare type नहीं होता है | लेकिन constructor की तरह destructor मे कोई भी argument pass नहीं होता है | अतः इसका syntax निन्म होता है :
~stock ();
stock()destructor का केवल एक ही ड्यूटी होता है | इसका syntax निन्म होता है :-
stock :: ~stock ()
{
}
जब किसी destructor को call किया जाता है तब इसका execution को निन्म code से पहचान जा सकता है :-
stock :: ~stock ()
{
cout<<“object delete !”;
}
जब किसी static object को create किया जाता है तब प्रोग्राम इस code मे allocate memory को आटोमेटिक terminate कर देता है |
जब किसी आटोमेटिक class object को create किया जाता है तब प्रोग्राम इस destructor से allocate memory को आटोमेटिक terminate कर देता है | जब class मे perform किये गये task को execute करने के बाद destructor allocate memory को डिलीट किया जाता है |
जब किसि object को new से create किया जाता है तब ये object heap memory और free store मे located होता है | और जब डिलीट command को use किया जाता ही तब destructor से allocate memory से डिलीट किया जाता है |
प्रोग्राम से temporary object को create किया जाता है | जो की कुछ sepecfic task को perform किया जाता है |
जाब class object expire हो जाता है तब destructor को आटोमेटिक call किया जाता है |
Stock management project का modify form :
इससे पहले के article मे discuss Stock management project मे केवल construtor को उससे किया जाता है लेकिन अब इस article मे construtor और destructor को use किया गया है | c++ language मे बहुत लम्बे code को store करने के लिए file format को useकिया जाता है | इसमें code को अलग लग file मे store किया जाता है |
हेडर file stock.h मे हेडर code को include किया जाता है | इसमें class की declartion और source code file जो की method की definition को hold करता है को hold करता है | इसके लिए सभीfile को ट्रैक easily किया जाता है |
stock.cpp मे class की definition को hold किया जाता है |
इसका code निन्म है :
stock.h
class stock
{
char company [50];
int share ;
double sharevalue ;
double total ;
void set_total (){total = share value * share ;}
public :
stick ();
void buy();
void sell();
void show();
void update ();
}
stock.cpp mai
#include<iostream>
#include<conio.h>
stock :: stock (const char * compnany , int n =0 , double price = 0.0 )
{
std :: strcpy ( name ,company,50 );
company[50]=’\0′;
if(n<0)
{
std::cerr<<“share can not be negetive “;
<< we set share equal to ‘0’ “<<endl;
share = 0 ;
}
else
share = n ;
share_price = price ;
total ();
}
stock :: ~stock ()
{
std :: cout<<“Stock Management Syatem Close “<<Bye ;
}
stock :: buy()
{
if(n<0)
{
std::cerr<<“share purchase can not be negetive “;
<< Please enter share more than ‘0’ “<<endl;
share = 0 ;
}
else
share += n ;
share_price = price ;
total ();
}
stock :: sell()
{
if(n<0)
{
std::cerr<<“share sell can not be negetive “;
<< Please enter share more than ‘0’ “<<endl;
share = 0 ;
}
else
share -= n ;
share_price = price ;
total ();
}
stock :: show()
{
cout<<“company Name :<<company ;
cout<<“share value :”<<total ;
cout<<“share number :”<<sharevalue
}
Input File मे ,
इस file मे यूजर द्वारा input किये data को console screen पर display किया जाता है और data पर operation perform किया जाता है |इसका code main() function होता है | इसका code निन्म है :
main()
{
cout<<“using constructor for create object “;
stock company1 (“parth “,30 , 200);
company1.show();
stock company2(“meet “,50 , 100);
company2.show();
cout<<“assignment operator”;
company2= company1;
company2.show();
company1.show();
cou<<“using construtor to reset object”;
company2=stock (“Om”,200,200);
cout<<“revised stock”;
company2.show();
cout<<“Done !”;
}
}
इस article मे destructor को discuss किया है अब आगे के article मे destructor के advance method को discuss करेगे |
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
4 weeks ago
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
4 weeks ago
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
4 weeks ago
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
4 weeks ago
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
1 month ago
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…
1 month ago