हिंदी माध्यम नोट्स
Categories: c++ language in hindi
write a program of calculator using member function , stock market ,MEMBER FUNCTION ( EXAMPLE )
stock market ,MEMBER FUNCTION ( EXAMPLE ) , write a program of calculator using member function :-
इससे पहले के article मे class के function को discuss किया था जिसे केवल class के scope मे ही declare किया जा सकता है | अब इस article मे class के member function को discuss करेगे | जो की class के सबसे efficient characteristic है |
उदाहरन 1
write a program of calculator using member function .
Explanation
सबसे पहले class calculator को declare किया जाता है | जिसमे class के लिए variable और member function के prototype को ही declare किया जता है |
add() से addition perform होता है | इसे add(int a , int b ) से declare किया जाता है जिसमे a और b class calculator का private variable है |
sub() से minus perform होता है इसे sub (int a , int b ) से declare किया जाता है जिसमे a और b class calculator का private variables है |
mul() से multiplication perform होता है |इसे mul (int a , int b ) से declare किया जाता है जिसमे a और b class calculator का private variable है |
div() से division perform होता है |इसे div (int a , int b ) से declare किया जाता है जिसमे a और b class calculator का private variable है |
class scope को क्लोज करने के बाद scope resolution से इन सभी function की definition को define किया जाता है |
इसके लिए निन्म syntax होता है :
calculator :: add(int a , int b ) {
function _ body ;
}
calculator :: sub (int a , int b ) {
function _ body ;
}
calculator :: mul(int a , int b ) {
function _ body ;
}
calculator :: div(int a , int b ) {
function _ body ;
}
main() मे ,
सबसे पहले यूजर से दो value को input किया जाता है | इसके बाद class calculator के object को define किया जाता है इस object से class के सभी function को define किया जाता है |
source code
#inclue<iostrem>
#include<conio>
using namespace std;
class calculator
{
private :
int a,b;
private :
void add(int a , int b );
void sub(int a , int b );
void mul(int a , int b );
void div(int a , int b );
void display( int a , int b )
{
cout<<“Addition is “<<add(a,b)<<endl;
cout<<“Subtraction is “<<sub(a,b)<<endl;
cout<<“Multipliaction is “<<mul(a,b)<<endl;
cout<<“Division is “<<div(a,b)<<endl;
} ; // close of class
calculator :: add(int a , int b ) {
return a+b;
}
calculator :: sub (int a , int b ) {
return a-b ;
}
calculator :: mul(int a , int b ) {
return a*b ;
}
calculator :: div(int a , int b ) {
return a/b ;
}
void main()
{
int num1 , num2 ;
cout<<“enter number 1 :”<<endl;
cin>>num1 ;
cout<<“enter number 2 :”<<endl;
cin>>num2 ;
intca.display(num1 , num2) ;
getch();
}
इसका आउटपुट होगा :
enter number 1 :12
enter number 2 :3
Addition is 15
Subtraction is 09
Multipliaction is 36
Division is 04
उदाहरन 2 :
इससे पहले के article मे discuss किये गये उदाहरन मे main() function को define नहीं किया गया है | अब इस article मे stock market के लिए प्रोजेक्ट को पूरा करगे |
#include<iostream>
#inclue<cstring>
class stock
{
private :
char company[50];
int share ;
double share_worth ;
double total;
void total ()
{
total = share * share_worth ;
}
public :
first_biding(const * char name , int number , double value );
buy( int number , double value );
sell( int number , double value );
update (double value );
display ( ) ;
}
vopid stock ::first_biding(const * char name , int number , double value )
{
std :: strcpy (company,company_name,50 );
company[50]=’\0′;
if(number <0)
{
std::cerr<<“share can not be negetive “;
<< we set share equal to ‘0’ “<<endl;
share = 0 ;
}
else
share = number ;
share_worth= value ;
total ();
}
vopid stock ::buy( int number , double value )
{
if(number <0)
{
std::cerr<<“share ourchase can not be less than ‘0’ “;
<< Transtion denied and try again “<<endl;
}
else
share + = number ;
share_worth= value ;
total ();
}
}
vopid stock ::sell( int number , double value )
{
if(number <0)
{
std::cerr<<“share sell can not be less than ‘0’”;
<< Transtion denied and try again “<<endl;
}
else if(number > share)
{
std::cerr<<“share sell can not higehr than share with compnay “;
<< Transtion denied and try again “<<endl;
}
else
share – = number ;
share_worth= value ;
total ();
}
}
void stock :: update (double value )
{
share_worth= value ;
total ();
}
void stock :: display ( )
{
void std :: cout;
void std :: endl;
cout<<“Company Name : << company;
cout<<“share :”<<share ;
cout<<“”Total Worth :”<<total ;
cout<<“Share Worth :”<<share_worth ;
}
void main()
{
void std :: cout;
void std :: endl;
stock stock1 ;
stock1.first_biding(“ABC Ltd”,100,200);
stock1.display();
stock1.buy(4,180);
stock1.display;
stock1.sell(6,230);
stock1.display();
getch();
}
इस उदाहरन मे किसी कंपनी के शेयर को manage करने के लिए छोटा सा प्रोजेक्ट है जिसमे 5 function है :
1.update () : इस function मे कंपनी के द्वारा की गयी sell – purchase को maintain किया जाता है | और इसके अलावा कम्पनी के शेयर के price को update किया जाता है |
2.display () ; इस function मे कंपनी के द्वारा की basic detail जैसे compnay _ name , share number और share value को display किया जाता है |
3.buy() : इस function मे compnay के द्वारा purchase किये गये share को update किया जाता है | इसके लिए यूजर द्वारा input किये शेयर value को कंपनी के data base से check किया जाता है और इसके बाद इसे update किया जाता है |
4.sell() : इस function मे कम्पनी के द्वारा sell किया गये share को update किया जाता है |
इस उदाहरन मे cerr आउटपुट stream command को use किया गया है जो को c++ प्रोग्राम मे आये हुए error message को display करने क एलिए use किया जाता है | इसके अलावा इस उदाहरन मे scope resolution operator से function member को डेफिन किया गया है | जिसे in सभी function मे class stock के private variable और function को access किक्या जा सकता है |
इसका आउटपुट निन्म होगा :
Company Name : ABC Ltd
share : 100
Total Worth :20000
Share Worth :200
Company Name : ABC Ltd
share : 104
Total Worth :18720
Share Worth :180
Company Name : ABC Ltd
share : 98
Total Worth :22540
Share Worth :230
इस article मे , c++ language से दो छोटे से प्रोजेक्ट calculator और stock maintenance प्रोजेक्ट को discuss किया है | अब आगे के article मे c++ class के member function के कुछ basic उदाहरनो को discuss करगे |
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
4 weeks ago
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
4 weeks ago
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
4 weeks ago
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
4 weeks ago
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
1 month ago
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…
1 month ago