हिंदी माध्यम नोट्स
C++ : Reference Variable , Address variable in hindi what is Address variable within structure in c++ language
c++ language मे , address variable ये variable होता है जिसमे किसी variable के address को hold करता है | address variable किसी variable और दुसरे variable ]के बीच मे लिंक बनाते है | उदहारण के लिए variable value और v दोनों address variable है | जब किसी reference को argument की तरह कार्य करता है तब function orginal data की copy को pass किया जाता है | किसी function मे pass by reference को use किया जाता है reference variable को pointer variable और नार्मल variable की जगह use किया जाता है |
int value = 120;
int &v = value ;
इस उदहारण मे value एक नार्मल variable है लेकिन ‘v’ एक reference variable है इसमें variable value की value को variable ‘a’ मे assign किया जाता है |
उदहारण के लिए :
#include<iostream.h>
#include<conio.h>
void main()
{
using namespace std;
int v=101;
int &value = v;
cout<<“value of v : “<<<<endl;
cout<<“value of value : “<<value<<endl;
v++;
cout<<“value of v : “<<v<<endl;
cout<<“value of value : “<<value<<endl;
cout<<“Address of v :”<<&v ;
cout<<“Address of value :”<<&value;
getch();
}
इस उदाहराण मे , निन्म आउटपुट होगा :
value of v : 101
value of value : 101
value of v : 102
value of value : 102
Address of v : 4400
Address of value :4400
Address variable within structure
reference variable को class और struture मे use किया जा सकता है | reference variable को यूजर define type data होता है | किसी struture मे reference variable को use करना किसी basic variable के तरह ही होता है | reference variable को use करने के लिए address operator ‘&’ को use किया जाता है | लेकिन function मे किसी struture के address को return किया जाता है | reference variable को function मे return करने के लिए एक अलग method को use किया जाता है | अतः किसी function से struture reference variable को return करने के लिए const किओ use किया जाता है |
इसका उदाहरन निन्म है :
#include<iostream.h>
#include<conio.h>
struct student
{
char name [20];
int grade ;
int class;
};
void main()
{
student s= {“parth”,60,”2″};
next(s);
student s1;
s1= next(s);
cout<<“Student ‘s’ class : “<<s.class<<endl;
cout<<“Student ‘s1’ class : “<<s1.class<<endl;
getch();
}
const student &next ( student &s)
{
cout<<” Name :”<<s.name<<endl;
cout<<” Grade :”<<s.grade<<endl;
s.class++;
cout<<“Class :”<<s.class<<endl;
return s;
}
इस उदहारण मे student एक struture है जिसमे तीन memebers है |
name[] ; ये character array है जिसमे student का नाम को assign किया जाता है |
class : ये class variable है जिसमे student के class को assign किया जाता है |
grade : ये integer type variable है जिसमे student के grade को assign किया जाता है |
main() function मे , student type s variable को use किया जाता है | जिसकी values को parth , 60 और 2 से initail किया जाता है | इसके बाद next () function मे struture variable ‘s’ को pass किया जाता है |
next() function मे struture की memeber class को increment किया जाता है | और struture की values को display किया जाता है | इसके अलावा एक और variable को declare किया जाता है | इसमें ‘s’ की value को next() की value को assign किया जाता है |
Temporary variable
जब c++ language मे , actual parameter किसी reference variable से match नहीं होता है तब Temporary variable को use किया जाता है | लेकिन c++ Temporary variable को तभी generate किया जाता है तब const reference को use किया जाता है | इसके अलावा Temporary variable को निन्म condition मे भी generate होता है :-
1.जब किसिस actual parameter का type सही है लेकिन इसकी value नहीं तब Temporary variable generate होता है |
2.जब किसी actual parameter को type गलत है लेकिन इस type को सही type मे convert किया जाता है तब Temporary variable generate होता है |
इसके उदहारण के लिए :
#include<iostream.h>
#include<conio.h>
void swapreference(int & a , int & b );
void main()
{
int value1 = 100;
int value2 = 200;
cout<<“Value1 :”<<value1;
cout<<“value 2 : “<< value2 ;
cout<<“Swap with reference :”<<endl;
swapreference(value1 , value2 );
cout<<“Value1 :”<<value1;
cout<<“value 2 : “<< value2 ;
getch();
}
void swapreference(int & a , int & b )
{
int temp;
temp = a;
a=b;
b=temp ;
}
इस उदहारण मे swaping function को call by reference को use किया जाता है |void swapreference(int & a , int & b ) ; इस function मे reference variable को use किया जाता है | इस उदहारण मे , c++ complier, दो Temporary variable को generate किया जाता है | इसका आउटपुट होगा :
value1 : 100
value2: 200
Swap with reference :
value1 : 200
value2: 100
उदहारण -2
#include<iostream.h>
#include<conio.h>
int cube(int & a );
void main()
{
int number ;
cout<<“Enter Number : “<<endl;
cin>>number ;
int output = cube(number);
cout<<“Cube value : “<<output;
getch();
}
int cube(int & a )
{
return a*a*a;
}
इस उदहारण मे cube function को call by reference को use किया जाता है |void cube(int & a ) ; इस function मे reference variable को use किया जाता है | इस उदहारण मे , c++ complier, एक Temporary variable को generate किया जाता है | इसका आउटपुट होगा :
Enter Number : 3
Cube value : 27
इस article मे structure के साथ reference variable को discuss किया है | और इसके अलावा Temporary variable को discuss किया जाता है |
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 भारत पर पहला तुर्क आक्रमण किसने किया…