हिंदी माध्यम नोट्स
CLASS IMPLEMENTATION in c++ language in hindi , Class implementation file
array name [30];
इस उदाहरन मे declare name मे 30 element की जगह 29 element ही store होते है | ठीक उसी प्रकार object की array के साथ भी होता है | उदाहरन के लिए
stock firm_detail{“ABC,POR,CDA”,30,353};
इस उदाहरन मे अगर firm_detial की size 10 है तब इसमें ABC,POR,CD store होगा | A truncating हो जाता है |
इस तरह के size restriction की प्रॉब्लम के solution को इस article मे discuss करेगे | length restriction को class implement से solve किया जा सकता है | इस प्रॉब्लम को solve करने के लिए निन्म methods को use किक्या जाता है :-
1.पहले method मे array की size को बढाया जाता है | जो की मेमोरी wasted की प्रॉब्लम को generate करता है |
2.दुसरे method मे array के स्थान पर string को use किया जाना चाहिए | और string की size automatic होनी चाहिए | इस method को implement करने के लिए class मे तीन मुख्य changes करने होते है |
change1 : string class को add किया जाता है | string class को अड़ करने एक लिए header position पर string.h header file को इन्क्लुए किया जाता है |
change2 : दुसरे changes मे , class के private सेक्शन को change किया जाता है | private सेक्शन मे company का नाम के लिए array को define किया गया था लेकिन अब string को define करेगे |
change 3 : तीरसे change मे constructor को change किया गया था | जिसमे कंपनी name को कंपनी array मे copy किया गया था और इस array के last block मे null pointer को assign किया गया था | लेकिन string के साथ केवल string को एक वर्तिअब्ले मे से दुसरे variable मे assign किया जाता जाता है |
इन तीनो changes से क्लास के implement मे किया जाता है | class के private method मे ही change करना है | उसके public method मे कोई change नहीं होगा |
कई बार प्रोग्रामर class implement मे कोई भी change नहीं कर सकता है | लेकिन इसके जगा एक नए constructor’
को add कर सकते है | जिसमे इस कंपनी name को assign किया जा सकता है |
नार्मल code :
class stock
{
private :
char company [50];
int share ;
double sharevalue ;
double total ;
double total ;
public :
void total (){ return total = share value * share;}
stick ();
void buy();
void sell();
void show();
void update ();
};
इस code मे आप देख सकते है की कम्पनी के नाम को store करने के लिए company [] array को use किया गया है जिसकी size 50 है इसमें 50 charecter store हो सकते है | इस array की size काफी बड़ी है | इसलिए इसमें सभी कंपनी के नाम store हो सकता है | लेकिन कभी कभी कंपनी का नाम बहुत छोटा होता है तब memory wasteage बहुत ज्यादा होता है | इस memory wastage को कम करने के लिए string को use किया जाता है |
Modified code 2 :
class stock
{
private :
string company [50];
int share ;
double sharevalue ;
double total ;
double total ;
public :
void total (){ return total = share value * share;}
stick ();
void buy();
void sell();
void show();
void update ();
};
इस code मे array की जगह string class के string data type को use किया गया है | string data type को use करने एक लिए header section मे string class को include किया जाता है | जिसका स्यन्त्याक्स निन्म होता है :
#include<string>
या
#include<string.h>
इस दोनों syntax से string class को code मे include किया जा सकता है |
Constructer code :
stock :: stock (const char * compnany , int n =0 , double price = 0.0 )
{
std :: strcpy ( name ,company,50 );
name[50]=’\0′;
}
इस code मे constructor को define किया गया है जो की array के साथ use किया जाता है | इसमें company variable के data को name array मे assign किया जाता है | और array के last block मे null pointer को assign किया जाता है |
Modified code :
stock :: stock (const char * compnany , int n =0 , double price = 0.0 )
{
name = company ;
}
इस code मे केवल , string variable name मे compnany के data को assign किया जाता है | जिसके लिए simple string assignment operator को use किया जाता है |
और अगर यूजर द्वारा class के code को modify नहीं किया जा सकता है तब class constructor को modify किया जाता है | इसका code निन्म होता है :-
string (const :: string & company , int n , int price);
इस constructor को use करने पर interface भी change हो जाता है | इसके लिए main() code मे निन्म तरह से object को initial किया जाता है |
इस constructor style से private सेक्शन मे कोई भी change नहीं होता है बल्कि public सेक्शन मे change होता है |
इसके लिए main() function मे निन्म code होता है :
string name ;
getline (cout , name );
stock min (name , 12 , 200);
इस तरह से class को modify करके memory और time complxity को कम किया जा सकता है |
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…