- Introduction to C++ pointers in Hindi
- Declaring & initializing C++ pointers in Hindi
- C++ Pointer to pointer in Hindi
Introduction to C++ Pointers
Pointers के बारे में आप C language में पहले पढ़ चुके है। C++ में भी pointers को उसी प्रकार यूज़ किया जाता है। Pointers variables में आप किसी normal variable का address store करते है।
इस address के द्वारा pointer variables उस original variable की value को point करते है। Pointers कभी भी value को store नहीं करते है बल्कि pointer variables address के माध्यम से उस value को point करते है।
आप सड़क पर खड़े थे किसी ने आप से आकर पूछा भाई साहब रमेश कँहा रहता है। आपने रमेश के घर की तरफ इशारा कर दिया। इसी प्रकार pointer variables भी सिर्फ इशारा करते है की particular value किस location पर stored है।
इस address के द्वारा pointer variables उस original variable की value को point करते है। Pointers कभी भी value को store नहीं करते है बल्कि pointer variables address के माध्यम से उस value को point करते है।
आप सड़क पर खड़े थे किसी ने आप से आकर पूछा भाई साहब रमेश कँहा रहता है। आपने रमेश के घर की तरफ इशारा कर दिया। इसी प्रकार pointer variables भी सिर्फ इशारा करते है की particular value किस location पर stored है।
Pointers के द्वारा आप memory में store की गयी value को program में कई जगह से point कर सकते है। Pointers के द्वारा variables की values के साथ operations भी perform किये जा सकते है जैसे की variables को print करना आदि।
हालाँकि आप values को variable names से access कर सकते है, फिर भी आपको pointers के रूप में एक alternate approach provide की गयी है। आइये इसे एक उदाहरण के माध्यम से समझने का प्रयास करते है।
मान लीजिये आपने एक int type का Age variable create किया है।
जब आप इस variable को किसी value के साथ initialize करेंगे तो compiler memory में variable के नाम से एक memory location allot करेगा।
ऊपर दिए गए उदाहरण में Age उस memory location का नाम है और 60 उस memory location में store की गयी value है। इस memory location का एक address भी होगा जो की hexadecimal form में होता है। Pointers इसी address को store करते है और इस address के माध्यम से ही वे original variable की value को point करते है।
हालाँकि आप values को variable names से access कर सकते है, फिर भी आपको pointers के रूप में एक alternate approach provide की गयी है। आइये इसे एक उदाहरण के माध्यम से समझने का प्रयास करते है।
मान लीजिये आपने एक int type का Age variable create किया है।
जब आप इस variable को किसी value के साथ initialize करेंगे तो compiler memory में variable के नाम से एक memory location allot करेगा।
ऊपर दिए गए उदाहरण में Age उस memory location का नाम है और 60 उस memory location में store की गयी value है। इस memory location का एक address भी होगा जो की hexadecimal form में होता है। Pointers इसी address को store करते है और इस address के माध्यम से ही वे original variable की value को point करते है।
एक pointer से आप किसी दूसरे pointer variable को भी point कर सकते है। C++ में arrays के भी pointers create कर सकते है। Objects के लिए pointers create करने की ability भी C++ आपको provide करती है। Pointers को functions के साथ भी यूज़ किया जाता है।
आइये अब देखते है की C++ में आप किस प्रकार pointers create और use कर सकते है।
Pointer variables को आप address of (&) operator के साथ initialize करते है।
आइये अब देखते है की C++ में आप किस प्रकार pointers create और use कर सकते है।
Declaring & Initializing C++ Pointers
Pointer variables create करने के लिए आप asterisk (*) operator को use करते है। जिस type का original variable होता है उसी type का pointer variable create किया जाता है। C++ में आप primitive types से लेकर user defined type तक के pointer variables create कर सकते है।
जैसा की आप ऊपर दिए गए syntax में देख सकते है, सबसे पहले data type define किया गया। इसके बाद asterisk (*) variable के साथ pointer variable का नाम declare किया गया है।
Pointer variables को आप address of (&) operator के साथ initialize करते है।
ऊपर दिए गए उदाहरण में age variable का address ptr pointer variable को assign किया गया है।
Example
#include <iostream>
using namespace std;
int main()
{
// Regular variable
int age = 50;
// Pointer variable
int *ptr;
// Assigning normal variable to pointer
ptr = &age;
cout<<"Value of age variable using normal syntax : "<<age<<endl;
cout<<"Address of age variable : "<<ptr<<endl;
cout<<"Value of age variable using pointer variable : "<<*ptr<<endl;
}
|
ऊपर दिए गए उदाहरण में value at (*) operator के साथ pointer variable को यूज़ किया गया है। ये operator किसी address पर store की गयी value को access करने के लिए यूज़ किया जाता है। ऊपर दिया गया program निचे दिया गया output generate करता है।
Value of age variable using pointer variable : 50
|
Pointer to Pointer
जैसा की आपको पहले बताया गया एक pointer से दूसरे pointer variable को भी point किया जा सकता है। इसके लिए double asterisk (**) operator को यूज़ किया जाता है। इसका syntax नीचे दिया जा रहा है।
data type **pointer-name;
|
मान लीजिये आप पहले ऊपर create किये गए ptr variable के लिए एक pointer create करना चाहते है तो ऐसा आप इस प्रकार कर सकते है।
इस pointer variable को आप इस प्रकार initialize करेंगे।
Pointer to Array
Pointer variables के द्वारा आप array को भी point कर सकते है। मान लीजिये आपने एक num नाम से integer array create किया है। इसमें आपने values भी store की है।
इस array के लिए आप pointer इस प्रकार create कर सकते है।
इस pointer को इस प्रकार initialize किया जायेगा।
इस array के लिए आप pointer इस प्रकार create कर सकते है।
इस pointer को इस प्रकार initialize किया जायेगा।
arrpntr इस समय num array के first element का address hold कर रहा है। आप चाहे तो arrpntr variable को loop के द्वारा increment करते हुए value at (*) operator के द्वारा आप array के सभी elements को print भी कर सकते है।
Pointer to Objects
जैसा की आपको पहले बताया गया आप user defined types (class objects) के लिए भी pointers create कर सकते है। किसी भी class type का pointer create करके आप उससे किसी normal object को point कर सकते है। इस pointer के द्वारा आप class के members को access कर सकते है। आइये इसे एक उदाहरण के माध्यम से समझने का प्रयास करते है।
ऊपर दिए गए उदाहरण में item नाम से एक class create की गयी है। Main function में इस class का एक object और एक pointer variable create किया गया है। इसके बाद object के address को address of (&) operator द्वारा pointer variable को assign किया गया है।
item obj;
// Pointer to object
item *obpntr;
// Pointing to object using object pointer
obpntr = &obj;
// Calling function with object pointer
obpntr->display();
|
इसके बाद pointer variable के माध्यम से class के display() function को execute करवाया गया है। ये program निचे दिया गया output generate करता है।
Pointer to Function
आप functions के लिए भी pointers create कर सकते है और उन्हें execute कर सकते है। इसका general syntax नीचे दिया जा रहा है।
मान लीजिये आपने एक addition का function create किया हुआ है।
ऊपर दिए function के लिए pointer इस प्रकार create किया जायेगा।
इस statement के द्वारा एक function pointer type create किया गया है। इस type का pointer variable create करके आप उसे add function का address assign करते है। इसका उदाहरण नीचे दिया जा रहा है।
इसे एक complete उदाहरण के रूप में निचे दिया जा रहा है।
ऊपर दिया गया program निचे दिया गया output generate करता है।
int main()
{
}
|
इसे एक complete उदाहरण के रूप में निचे दिया जा रहा है।
#include <iostream>
}
|
ऊपर दिया गया program निचे दिया गया output generate करता है।
This pointer
This एक pointer होता है जो हर class के साथ automatically attached होता है। इस pointer को current class के सन्दर्भ में यूज़ किया जाता है। इसे सिर्फ class के अंदर ही यूज़ किया जा सकता है।
मान लीजिये आप किसी reason से class के किसी function को class में ही execute करवाना चाहते है। लेकिन object के बिना class के किसी भी function को execute करना possible नहीं है। ऐसी situation में आप this pointer यूज़ कर सकते है और function को execute करवा सकते है।
मान लीजिये आप किसी reason से class के किसी function को class में ही execute करवाना चाहते है। लेकिन object के बिना class के किसी भी function को execute करना possible नहीं है। ऐसी situation में आप this pointer यूज़ कर सकते है और function को execute करवा सकते है।
{
cout<<"Bye Bye! Take care!";
}
void welcome(void)
{
cout<<"Welcome into this program. How are you feeling in here..."<<endl;
// Calling byebye() function using this pointer
this->byebye();
}
|
ऊपर दिए गए उदाहरण में जैसा की आप देख सकते है thisdemo class के object से सिर्फ welcome() function को call किया गया है। Welcome function में this pointer द्वारा byebye() function को call किया गया है। जब आप welcome() function को call करते है तो byebye() function भी call हो जाता है।
जब भी आपको बिना object के class में members को access करने की आवश्यकता हो तो आप this pointer को यूज़ करेंगे।
जब भी आपको बिना object के class में members को access करने की आवश्यकता हो तो आप this pointer को यूज़ करेंगे।
0 Comments