हिंदी माध्यम नोट्स
Categories: c++ language in hindi
This Pointer in c++ language , what is type of this pointer in c++ in hindi , example source code program
example source code program , what is type of this pointer in c++ in hindi , This Pointer in c++ language :-
इससे पहले के article मे , constructor और destructor को discuss किया है अब इस article मे this pointer को discuss करेगे जो की c++ मे constructor और destructor की तरह important होता है |
this pointer
stock management code मे और भी एडवांसमेंट हो सकता है और वो एडवांसमेंट this pointer से होता है | प्रत्येक class member function किसी एक object से associate होता है | लेकिन किसी एक method को दो अलग अलग object के साथ use किया जाता है तब c++ this pointer को उस किया जाता है |
Need of this pointer
stock class declaration को class data को display किया जाता है | show() function मे total को use किया जाता है | और इस variable को use करने के लिए inline function को use किक्या जाता है | या इसके method से stored data को डायरेक्ट access किया जा सकता है इसका कपड़े निन्म है :
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 मे , total variable से allocate memory को function total डायरेक्ट access कर सकता है | और इसलिए variable total की value को access करने के लिए total method function को use किया जाता है | लेकिन class मे define member function total () से total variable को reset करने की facility नहीं होती है |
जब इस class मे एक और function greator() को add किया जाता है जो की ये पता लगता है की कोनसा शेयर की value highest है | इस function से this pointer को use किया जा सकता है | इस member function को define करने के लिए निन्म तरीके आपनाया जाता अहि की
दो object मे से reference को return किया जाता है और इसमें से greater value को return किया जाता है | इस approach मे निन्म प्रॉब्लम निकल कर आती है :-
1.किसी सदो object के एक member function के value को किस तरह से compare किया जाता है | उदाहरन के लिए greator() को use किया जाता ही | stock1.greator() से stock1 के data को access किया जाता है | और stock2.greator () मे stock2 के data को use किया जाता है | जब in दोनों object को compare किया जता है तब दुसरे object को एक argument की त्राग pass किया जाता है | और इसके लोए reference method को use किया जाता है | इसलिए greator() मे const stock2 & को pass किया जाता है |
इस method मे एक object को implicit और explicit use किया जाता है | इस method मे से इन दोनों object मे से एक object का reference return होता है | const का use ये define करने के लिए किया जाता है की explicit object के memory address को constant हो और इसे change नहीं किया जा सकता है | और दोनों कही object का return type const होता है इसलिए इस method function का return type भी const होता है |
अतः अगर stock class के object stock1 और stock 2 को compare करना हो तो तब stock1 के function method greator() मे stock2 को pass किया जाता है इसक syntax निन्म होता है :
top = stock1.greator(stock2);
top = stock2.greator(stock1);
पहले syntax मे , stock1 को implicit और stock2 object को explicit access किया जाता है | और दुसरे syntax मे , stock2 को implicit और stock1 object को explicit access किया जाता है |
और method function greator() का के निन्म होगा :
const stock & stock1 :: greator(const stock & stock2 )
{
if(total > stock2.total)
{
return total;
}
return stock2.total ;
}
इस code मे total object stock1 का data है और stock2.total का use stock2 के data को access किया जाता है | और code के execute किया जाता है तब इसमें से जिस भी object के variable total की value greater होगी वो return होगी | और नहीं ये member function जिस भी object से associate होता है उसकी value को return करता है | जब top = stock1.greator(stock2); को call किया जाता है तब इसका stock2 , stock का reference है लेकिन stock1 से alias नहीं है |
इस प्रॉब्लम को solve करने के लिए this pointer को use किया जाता है | this pointerउस object को point करता है जो की member function को invoke करता है | top = stock1.greator(stock2); मे this pointer stock1 object को point करता है क्योकि stock1 मे से member function को call किया जाता है |
top = stock2.greator(stock1); this pointer stock2 object को point करता है क्योकि stock2 मे से member function को call किया जाता है | this pointer को shorthand operator के साथ भी use किया जा सकता है | इसका syntax निन्म होता है :-
this->total ;
और उपर वाले code मे मॉडिफिकेशन निन्म होता है :-
const stock & stock1 :: greator(const stock & stock2 )
{
if(total > stock2.total)
{
return total;
}
return *this ;
}
this pointer object के address को होड़ करता है और *this से इस pointer variable की value को use किया जाता है |इसका उदाहरन निन्म है :-
stock.h
class stock
{
char company [50];
int share ;
double sharevalue ;
double total ;
void set_total (){total = share value * share ;}
public :
stick ();
void buy();
void sell();
void show();
void update ();
const stock & stock :: greator(const stock & stock )const ;
}
stock.cpp mai
#include<iostream>
#include<conio.h>
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 ();
}
stock :: ~stock ()
{
std :: cout<<“Stock Management Syatem Close “<<Bye ;
}
stock :: buy()
{
if(n<0)
{
std::cerr<<“share purchase can not be negetive “;
<< Please enter share more than ‘0’ “<<endl;
share = 0 ;
}
else
share += n ;
share_price = price ;
total ();
}
stock :: sell()
{
if(n<0)
{
std::cerr<<“share sell can not be negetive “;
<< Please enter share more than ‘0’ “<<endl;
share = 0 ;
}
else
share -= n ;
share_price = price ;
total ();
}
stock :: show()
{
cout<<“company Name :<<company ;
cout<<“share value :”<<total ;
cout<<“share number :”<<sharevalue
}
const stock & stock1 :: greator(const stock & stock2 )
{
if(total > stock2.total)
{
return total;
}
return *this ;
}
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…
1 week ago
Translation in english grammer in hindi examples Step of Translation (अनुवाद के चरण)
Translation 1. Step of Translation (अनुवाद के चरण) • मूल वाक्य का पता करना और उसकी…
1 week 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…
1 week 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…
1 week ago
विश्व के महाद्वीप की भौगोलिक विशेषताएँ continents of the world and their countries in hindi features
continents of the world and their countries in hindi features विश्व के महाद्वीप की भौगोलिक…
1 week ago
भारत के वन्य जीव राष्ट्रीय उद्यान list in hin hindi IAS UPSC
भारत के वन्य जीव भारत में जलवायु की दृष्टि से काफी विविधता पाई जाती है,…
1 week ago