Write a program to make a calculator using class , calculate cube using class , swapping two numbers

इससे पहले के article मे , class और object के basic को discuss किया था | अब इस article मे class और object उदहारण को discuss करेगे | जिससे आप सभी के class और object के concept को clear हो सके |
उदाहरण 1 :
Write a program to make a calculator using class .
इस उदाहरण मे , class से  calculator को बनाया गया है |Explanation
सबसे पहले class calculator () को declare किया गया है | इस claculator() मे दो data variable a,b को declare किया गया है | इस variable का access mode private है इसका मतलब इन variable केवल class मे use किया जाता है |
class calculator() मे निन्म char function को declare किया जाता है |add() मे ,
add() function मे दो value को addition किया जाता  है | और आउटपुट की value को display किया जाता है |
sub() मे ,
sub () function मे दो value को  subtraction किया जाता  है | और आउटपुट की value को display किया जाता है |
mul() मे ,
mul() function मे दो value को multilication किया जाता  है | और आउटपुट की value को display किया जाता है |
div() मे ,
div () function मे दो value को division किया जाता  है | और आउटपुट की value को display किया जाता है |

main() मे ,
class calculator() के लिए एक object ‘c’ को define किया जाता है |  इसके बाद  सबसे पहले यूजर द्वारा choice को input किया जाता है |
Enter ‘+’ for addition
Enter ‘-‘ for subtraction
Enter ‘*’ for multiplication
Enter ‘/’ for division
उसके बाद यूजर से choice को input करा लेते है | और इसके बाद  यूजर से values को input करा लेते है |
choice को switch statement मे pass किया जाता है |  और इसके बाद class के function को use किया जाता है |
अगर यूजर द्वारा ‘+’ को choose किया जाता है तब c.add() function को call किया जाता है |
अगर यूजर द्वारा ‘-‘ को choose किया जाता है तब c.sub() function को call किया जाता है |
अगर यूजर द्वारा ‘*’ को choose किया जाता है तब c.mul() function को call किया जाता है |
अगर यूजर द्वारा ‘/’ को choose किया जाता है तब c.div() function को call किया जाता है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
class calculator()
{
private :
int output;
public:
// Function definition
void add(int a, int b)
{
output = a + b;
cout<<“Output : “<<output;
}
public:
// Function definition
void div(int a, int b)
{
output = a / b;
cout<<“Output : “<<output;
}
public:
// Function definition
void sub(int a, int b)
{
output = a – b;
cout<<“Output : “<<output;
}
public:
// Function definition
void mul(int a, int b)
{
output = a * b;
cout<<“Output : “<<output;
}
};
void  main()
{
calculator c;
int number1 , number2 ;
char choice ;
cout<<“Enter ‘+’ for addition”<<endl<<“Enter ‘-‘ for subtraction”<<endl<<“Enter ‘*’ for multiplication”,,endl<<“Enter ‘/’ for division”<<endl;
cout<<“Enter choice : “;
cin >> choice;
cout<<“Enter number1 : “;
cin >> number1;
cout<<“Enters number2 : “;
cin >> number2;
switch(choice)
{
case ‘+’ :
c.add(number1,number2);
break;
case ‘-‘ :
c.sub(number1,number2);
break;
case ‘*’ :
c.mul(number1,number2);
break;
case ‘/’ :
c.div(number1,number2);
break;
default :
cout<<“invalid choice “;
break;
}
getch();
}

इसका आउटपुट होगा :
Enter ‘+’ for addition
Enter ‘-‘ for subtraction
Enter ‘*’ for multiplication
Enter ‘/’ for division
Enter choice : +
Enter number1 : 12
Enter number2 : 24
output : 36

उदाहरन 2 :
Write a program to calculate cube using class .
इस उदाहरण मे , user define function से cube को calculate करने के लिए class को बनाया गया है |

Expalnation
1.सबसे पहले header feild मे  class math  को define किया जाता है |इस class math मे  एक data variable output को declare किया गया है | इस variable का access mode private है इसका मतलब इन variable केवल class मे use किया जाता है | इसके अलावा function cube () को भी define किया जाता है | cube() function का access mode public है |
cube () मे ,
1.i-सबसे पहले function definition मे cube(int n ) को define किया जाता है | यह पर ‘n’ formal parameter है जिसमे actual parameter number से intial किया जाता है |
1.ii-उसके बाद output = n*n ; से cube को calculate कर लेते है |
1.iii- इसके बाद output को print किया जाता है |

2.उसके बाद main() function मे  math class के लिए एक object c को declare किये जाता है | एक variable number को declare किया गया है इसमें यूजर द्वारा input की गयी value को assign किया गया है |
3.यूजर द्वारा  value को input करा लेते है और इन value को  variable number मे assign करा देते है |
4. जिसमे class cube के function cube () को call किया जाता है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
class math
{
private :
int  output;
public :
int cube ( int n)
{
output = n*n*n;
cout<<“Cube value :”<<output ;
}
};
void  main()
{
math  c;
int  number ;
cout<<“Enter number : “;
cin >> number;
// Function call
c.cube(number);
getch();
}

इसका आउटपुट होगा :
Enters number : 10
cube of 10 is 1000

उदाहरन 3 :
Write a program to swapping two numbers  using class  .
इस उदाहरण मे , user define function से  two numbers swap करने के लिए function को बनाया गया है |

Explanation
1.सबसे पहले header field मे  class math को define किया जाता है |इस class math मे  एक data variable temp को declare किया गया है | इस variable का access mode private है इसका मतलब इन variable केवल class मे use किया जाता है | इसके अलावा function swap() को भी define किया जाता है |swap() function का access mode public है |
swap () मे ,
1.i-सबसे पहले function definition मे swap(int a, int b ) को define किया जाता है | यह पर ‘a’ और ‘b’ formal parameter है जिसमे actual parameter number से intial किया जाता है |
1.ii-उसके बाद swaping को perform किया जाता  है |
1.iii- इसके बाद output को print किया जाता है |

2.उसके बाद main() function मे  math class के लिए एक object c को declare किये जाता है | एक variable value1 और value2 को declare किया गया है इसमें यूजर द्वारा input की गयी values को assign किया गया है |
3.यूजर द्वारा  value को input करा लेते है और इन value को  variable number मे assign करा देते है |
4. जिसमे class math  के function swap() को call किया जाता है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
class math{
private :
int temp ;
public:
void swap  ( int a,int b )
{
temp = a;
a=b;
b=temp;
cout<<“Number1 :”<<a;
cout<<“Number2 :”<<b;
}
}
void  main()
{
math c;
int  number1 , number2   ;
cout<<“Enter number1  : “;
cin >> number1 ;
cout<<“Enter number2  : “;
cin >> number2 ;
c.swap (number1,number2);
getch();
}

इसका आउटपुट होगा :
Enter number1  : 120
Enter number2  : 130
Number1 : 130
Number2 : 120

उदाहरन 1 :
Write a program to calculate even-odd logic using class .
इस उदाहरण मे , even-odd logic को calculate करने के लिए class को बनाया गया है |

Expalnation
1.सबसे पहले header feild मे  इस class test declare किया गया है | इसमें  function even () को भी define किया जाता है | cube() function का access mode public है |
int even (int) मे एक  value return होती है जिसका  type integer है | और इस function मे pass argument की सख्या ‘1’ है और type integer है |
even () मे ,
1.i-सबसे पहले function definition मे  even (int n) को define किया जाता है | यह पर ‘n’ formal parameter है जिसमे actual parameter number से intial किया जाता है |
1.ii-उसके बाद even-odd के लॉजिक को check किया जाता है | अगर variable ‘n’ , 2 से divide होता है तब function से ‘1’ return होता है |
अन्यथा function से ‘0’ return होती है |
2.उसके बाद main() function मे , class test के लिए एक object t को define किया जाता है | इसके अलावा एक variable number को declare किया गया है इसमें यूजर द्वारा input की गयी value को assign किया गया है |
3.यूजर द्वारा  value को input करा लेते है और इन value को  variable number मे assign करा देते है |
4.उसके बाद एक और variable ‘ flag’ को declare करते है | जिसमे function even() के द्वार return की logic value को assign किया जाता है |
5.उसके बाद flag की value को check किया जाता ही |अगर flag की value ‘1’ होती है तब number is even का message display हो जाता है | अन्यथा number is odd का message print हो जाता है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
class test
{
public :
int check ( int n)
{
if(n%2 == 0)
{
return 1;
}
else
{
return 0;
}
}
};
void  main()
{
test c;
int  number ;
cout<<“Enter number : “;
cin >> number;
// Function call
int flag  = c.check(number);
if(flag == 1)
{
cout<<“Number is even. “;
}
else
{
cout<<“Number is odd. “;
}
getch();
}

इसका आउटपुट होगा :
Enters number : 10
Square of 10 is 100