हिंदी माध्यम नोट्स
Categories: c++ language in hindi
Constructor in c++ language , types of constructor in c++ ,Need ,Declaring
types of constructor in c++ ,Need ,Declaring ,Constructor in c++ language :-
इससे पहले के article मे , stock maintenance प्रोजेक्ट को discuss किया जाता है | stock प्रोजेक्ट code मे कई सारे default function को use किया जा सकता है | इसे constructors और destructors कहते है | अब इस article मे c++ constructors को discuss करेगे |
Need of C++ constructors
c++ language मे class object को नार्मल standard types की तरह use किया जाता है | अभी तक हमने c++ object को initial नहीं किया था अब इस article मे c++ object को int और struct की तरह initial भी किया जा सकता है | stock type को नार्मल variable की तरह initial नहीं किया जा सकता है | इसका उदाहरन निन्म है :-
int year = 2003;
struct google{
char * item ;
int m ;
};
google g1 = {‘abc’,2005};
stock g2 = {‘parth,meet’,2008 ,200};
इस stock g2 = {‘parth,meet’,2008 ,200}; statement से struct को initial नहीं किया जा सकता है | क्योकि stock object मे कई data item private होता है | इसे डायरेक्ट access नहीं किया जासकता है |इस data item को access करने के लिए member function को use किया जाता है |अगर यूजर द्वारा सही member function को use किया जाता है तभी ही object stock को initial किया जाता है | इस method से object को initail तभी किया जा सकता है जब इसके data item को public access mode मे declare किया जाता है |
genral method मे , जब object को initail किया जाता है तब ही इसे initail किया जाना चाहिए | उदाहरन के लिए
stock gift;
gift.buy(10,230);
जब इस उदाहरन को consider किया जाता है तब stock class के object को initail नहीं किया गया है | इसलिए object मे compnay का name भी initail नहीं हुआ | जब इस प्रकार के code को run किया जाता है इसे error generate होता है | जब c++ object को declare किया जाता ही इसे आटोमेटिक initail किया जाता है |c++ मे एक sepeciaल function होता है जिसे c++ constructor कहते है | c++ constructor का use किसी नए object को declare करने के लिए और इसमें values को assign करने के लिए किया जाता है | इसका name class name के सामान रहता है |
उदाहरन के लिए stock class के लिए constructor का नाम stock ही रहता है | constructor prototype और header का एक अलग से properties होती है | constructors मे कोई return value नहीं होती है इसका return type भी void नहीं होता है |
Declaring of C++ constructors
जब किसी constructors को declare किया जाता है | तब ये अलग अलग ही concept को use करता है | उदाहरन की तरह stock क्लास के लिए constructors को declare करने के लिएconstructorsमे तीन argument को pass किया जाता है | stock constructors को declare करने के लिए तीन argument की जरुरत इसलिए पड़ी क्योकि stock class मे तीन external value को ACCEPT किया जाता है | और fourth value total को constructors के declare मे use नहीं किया जाता है क्योकि इसे share_value * number से find किया जाता है | अतः इसके केवल COMPNANY के लिए values को पास किया जाता है | इसका constructors निन्म होता है :-
stock (const char * compnany , int n =0 , double price = 0.0 )
इस constructors मे
पहला argument एक string का pointer है जिसमे compnany array member से initial किया जाता है |
n : ये integer type variable है जिसमे share के name को initail किया जाता है |
price : ये integer type variable है जिसमे share की price से initial किया जाता है |
इसका उदाहरन निन्म होता है :-
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 ();
}
ये code पहले वाले code के सामान है लेकिन इस case मे difference निन्म होता है की इसमें प्रोग्राम एक automatic function को invoke करता है जिससे constructors की value को आटोमेटिक initail किया जाता है |
c++ constructors मे constructors का data item मे constructors के name को initail को नहीं किया जाता है | बल्कि constructors की value को initial किया जाता है |
उदाहरन 2 मे book class को use किया जाता है | book मे name , price और year को इन्तिअल किया जाता है | इसके लिए generate constructors के लिए तीन argument को pass किया जाता है | इसनकी value को आटोमेटिक को initial किया जाता है | और इसमें कोई fourth variable नहीं है इसलिए इसमें कोई confusion नहीं होता है | लेकिन अगर यूजर book से regarding दूसरी information को store करना चाहता है तब इस constructors मे pass argument की सख्या 4 हो जाती है |
book :: book (const char * name , int year =0 , double price = 0.0 )
{
std :: strcpy ( book _ name ,name ,50 );
book _ name[50]=’\0′;
cout<<“Book name :”<<book _ name ;
cout<<“price : “<<price;
cout<<“Publish year :”<<year ;
}
इस article मे c++ constructors को discuss किया जाता है | अब आगे के article मे c++ constructors के advance method को discuss करेगे |
Recent Posts
Question Tag Definition in english with examples upsc ssc ias state pcs exames important topic
Question Tag Definition • A question tag is a small question at the end of a…
2 weeks ago
Translation in english grammer in hindi examples Step of Translation (अनुवाद के चरण)
Translation 1. Step of Translation (अनुवाद के चरण) • मूल वाक्य का पता करना और उसकी…
2 weeks ago
Report Writing examples in english grammer How to Write Reports explain Exercise
Report Writing • How to Write Reports • Just as no definite rules can be laid down…
2 weeks ago
Letter writing ,types and their examples in english grammer upsc state pcs class 12 10th
Letter writing • Introduction • Letter writing is an intricate task as it demands meticulous attention, still…
2 weeks ago
विश्व के महाद्वीप की भौगोलिक विशेषताएँ continents of the world and their countries in hindi features
continents of the world and their countries in hindi features विश्व के महाद्वीप की भौगोलिक…
2 weeks ago
भारत के वन्य जीव राष्ट्रीय उद्यान list in hin hindi IAS UPSC
भारत के वन्य जीव भारत में जलवायु की दृष्टि से काफी विविधता पाई जाती है,…
2 weeks ago