Member Functions in C++ : Defined inside Class
हमने अभी तक जितनी भी Class Develop की है उन सभी में Class के अन्दर ही Member Functions को Define किया है। जब हम किसी Class के अन्दर ही Member Functions को Define कर देते हैं तो वे Member Functions By Defaults inline होते हैं, चाहे हम inline Keyword का प्रयोग करें या ना करें।
जबकि किसी Class के बाहर Define किए गए सभी Functions तब तक inline नहीं होते जब तक कि हम inline Keyword का प्रयोग करके Function को inline Declare ना करें। एक बात हमेंशा ध्यान रखें कि main() Function कभी भी inline नहीं हो सकता।
Member Functions in C++ ” Defined outside Class
हम जानते है कि किसी Class के अन्दर Define होने वाले सभी Member Functions By Default inline होते हैं। अब यदि कोई Member Function काफी बडा हो, तो वह Memory में काफी अधिक Space ले लेगा जिससे Speed ब<ने के बजाय Program की Speed काफी कम भी हो सकती है। इस स्थिति में हम चाहते हैं कि Member Function Inline ना हो कर सामान्य Function बन जाए।
ऐसा तभी हो सकता है जबकि Member Function को Class के बाहर Define किया जाए। लेकिन हम किसी भी Function को सामान्य तरीके से किसी Class से बाहर Define नहीं कर सकते। हमें इस काम के लिए एक विशेष तरीके का पालन करना पडता है। निम्न Syntax को देखिए:
class alpha //class Specification { … void member_function() // member function { void AnyFunction(void); // Prototype declaration of Member Function … … } }; void alpha::AnyFunction(void) // definition outside class { // this function is not an inline function // statements }
किसी Member Function को हम इस तरीके से किसी Class से बाहर Define कर सकते हैं। यहां AnyFunction() का Prototype Declaration हमने alpha Class में किया है लेकिन इस Function को हमने Class के बाहर Define किया है।
जब हम किसी Member Function को किसी Class से बाहर Define करना चाहते हैं तो हमें बताना होता है कि हम जिस Function को Define कर रहे हैं वह किस Class का Member Function है। ये बताने के लिए हमें Class के नाम व Member Function के नाम दोनों के नामों के बीच Scope Resolution Operator का प्रयोग करना पडता है। यानी हमें Class का पूरा नाम लिख कर ये बताना पडता है कि जो Member Function हम Declare कर रहे हैं वह किस Class से Related है या किस Class में उसे Declare किया गया है।
यदि हम इस तरह से ना लिखें तो Create होने वाला Function एक सामान्य सा User Defined Function होगा ना कि किसी Class का Member Function, इसलिए हमें हमेंशा इस बात का ध्यान रखना होता है कि Create होने वाला Function किस Class का Member Function है।
हमेंशा ऐसा नहीं होता है कि हम जो Function किसी Class में Member Function के रूप में लिखें वह हमेंशा Inline हो। उदाहरण के लिए यदि हम किसी Member Function में Loop का प्रयोग करते हैं, तो Compiler हमें Warning Message देता है और हमारे Member Function को inline बनाने के बजाय एक Simple Function बना देता है।
वास्तव में हम जब किसी Function को inline Declare कर रहे होते हैं तो हम केवल हमारे Compiler को एक Request कर रहे होते हैं कि वह हमारे Function को inline कर दे। लेकिन यदि Function काफी जटिल हो तो हमारा Compiler उस Function को inline नहीं बनाता, बल्कि एक सामान्य Function बना कर उपयोग में लेता है।
चलिए, इन Concepts को अब हम weekdays Class पर लागू करते हैं और समझने की कोशिश करते हैं कि किस प्रकार से इन Concepts को किसी Program में Use किया जा सकता है। हमने weekdays Class में देखा था कि जब हम उस Program को Compile करते हैं तो हमें Compiler “Functions containing switch statements are not expanded inline” Warning Message प्राप्त होता है। Compiler हमें ये Warning ना दे इसके लिए जरूरी है कि हम Function को Class से बाहर Define करें। निम्न Program में inday() Function को Class से बाहर Define किया गया है:
// Program #include <iostream.h> #include <string.h> // for stricmp() const int MAX = 10; // maximum length of day name, +1 const int DPW = 7; // days per week const char day_name[DPW][MAX] = // array of day names { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday"}; class weekday // class of day names { private: int day_number; // Sunday=1, etc. public: void inday(); // NOTE: declaration void outday() // display the day name { cout << day_name[day_number]; } void outnumber() // display the day number { cout << (day_number + 1); } void add(int days) // add days to { // this weekday day_number += days; // add days day_number %= DPW; // ensure not > 7 } }; // end weekdays class // NOTE: definition void weekday::inday() // user inputs day name { int j; char tempday[MAX]; // holds user input bool gotit = false; // match found? (0=false) while( !gotit ) // cycle until user enters { // a correct name cout << "Enter day of week (e.g., Friday): "; cin >> tempday; for(j=0; j<DPW; j++) // compare user input { // with list of names if( stricmp(tempday, day_name[j])==0 ) { // if there's a match, gotit = true; // set flag, break; // break out of for loop } } // end for day_number = j; // record what day it was } // end while } // end inday() #include <conio.h> void main() { weekday wd; // make a weekday object cout << "What day is it?" << endl; wd.inday(); // user gives it a value cout << "You entered "; wd.outday(); // display its day name cout << "\nThat's day number "; wd.outnumber(); // display its day number wd.add(10); // add 10 days to it cout << "\nTen days later is "; wd.outday(); // display its day name getch(); }
ये Program उसी तरह से काम करता है जिस तरह पिछली बार किया था लेकिन इस बार Compiler हमें किसी प्रकार की कोई Warning नहीं देता क्योंकि हमने inday() Member Function का Declaration Class के अन्दर किया है लेकिन Function को Class के बाहर Define किया है।
जिन Member Functions में दो&चार Statements से अधिक Statements हों, सामान्यतया उन Member Functions को Class से बाहर Define करना चाहिए। यदि हम सभी Member Functions को Class से बाहर Define करते हैं, तो Class को समझना काफी सरल होता है, क्योंकि Class काफी छोटी दिखाई देती है। जब हम Functions की Library Create कर रहे होते हैं, तब ये एक अच्छा तरीका होता है। क्योंकि इस स्थिति में Class काफी सरल व आसानी से समझने योग्य होती है। (Member Functions in C++ – Wiki)
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook C++ Programming Language in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
C++ Programming Language in Hindi | Page: 666 | Format: PDF