C++ operator, एक symbol की तरह, जैसे हम real world में mathematical task के लिए operator का उपयोग करते हैं, वैसे ही एक program के माध्यम से same task को करने के लिए operator का उसे करते हैं।
उदाहरण के लिए,
A + B
जहां “+” एक operator है और a और b operand या variable भी कह सकते हैं।
programming world में, इन operators को विभिन्न वर्गों में विभाजित किया गया है, C++ इन operator निम्न प्रकार से बांटा गया हैं।
arithmetic operator in C++
ये basic operators या default operator हैं। ये operator किसी task को करने के लिए दो variable का use करते हैं। आम तौर पर ये सभी प्रोग्रामिंग भाषाओं में मौजूद होते हैं।
इनका वर्णन नीचे दी गई टेबल में किया गया है-
Operator | Name | Example |
= | addition | a+b |
– | subtraction | a-b |
* | multiplication | a*b |
/ | division | a/b |
% | modulus | a%b |
जैसे –
int a= 3,b=4;
cout<<a+b; // print addition 7
cout<<a-b; // print addition -1
cout<<a*b; // print addition 12
Relational Operator in C++
C ++ में, एक दो अलग-अलग या एक जैसे प्रकार के operator को जोड़कर , किसी तीसरे प्रकार का operator बनाया जाता है। जिसे relational operator कहा जाता है। C ++ में इन operators का प्रयोग एक सिंगल statement में दो variable को compare करने के लिए किया जाता है।
Operator | Name | Example |
< | Less than | a<b |
<= | Less than equal to | a<=b |
> | Greater than | a>b |
>= | Greater than and equal to | a>=b |
== | Equal to | a==b |
!= | Not equal to | a!=b |
relational operators का इस्तेमाल control statement के साथ किया जाएगा। जैसे कि,
जैसे , a = 3, b = 4,
less than operator
if(a<b) // if 3 is greater than 4 than body of if will be execute.
{
// body of if
}
“Not Equal To” Operator
if(a!= b) if a and b value is not equal than body of if will be execute.
{
// body of if
}
“Equal To” operator
if(a== b) if a and b value is equal than body of if will be execute. { // body of if }
यहाँ इसका program दिया गया है –
#include<conio.h>
#include<iostream.h>
/
void main()
{
clrscr();
int a = 3, b = 4 , c =4;
if(a>b) {
cout<<b<<" is greater than "<<a;
}
if(a!=b) {
cout<<a<<" is Not Equal to "<<b;
}
if(b==c) {
cout<<b<<" is Equal to "<<c;
}
getch();
}
OUTPUT
4 is greater than 3
4 is Not Equal to 3
4 is Equal to 4
इस प्रकार के operator का प्रयोग दो या दो से अधिक condition को compare करने के लिए किया जाता है।
Operator | Name | Example |
&& | AND | a > b && a != b |
|| | OR | a > b && a != ba >b || a != b |
! | NOT | !A |
जैसे किसी program में ,
यदि variable “a” का मान variable “b” से छोटा और variable “a” 0 से बड़ा हो (दोनों condition true होनी चाहिए) तो ही body of loop execute होगा।
int a = 5, b = 10 c = 20;
if(a < b && a > 0) {
//body of if
}
यदि variable “a” का मान variable “b” से बड़ा हो या variable “b ” 0 का मान variable “b ” के बराबर हो (कोई भी एक conditon true होनी चाहिए_ तो body of loop execute होगा।
if(a < b || a != b) {
//body of if
}
NOT operator can be used with a single variable,
“NOT-operator” का उपयोग एक single variable के साथ किया जाता है,
int c = 0;
if(!c) // if c value 0, than execute body of if
{
//body of if
}
“!” ऑपरेटर एक “ON” और “OFF” button की तरह व्यवहार करता है जहां ON 0 है और अन्य value OFF को represent करता है। इसलिए इसका उपयोग program में किसी statement या function के execution को check करने के लिए किया जाता है।
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int a = 5, b = 10 ;
if(a < b && a > 0) {
cout<<a<<" is smaller than "<<b<<" but greater than 0;
}
if(a < b || a != b) {
cout<<a<<" and "<<b<<"are not Equal";
}
getch();
}
OUTPUT
5 is smaller than 10 but greater than 0
5 and 10 are Not Equal
assignment operator in C++
assignment operator का प्रयोग program में एक variable/operand intialize / assign करने के लिए किया जाता है, जैसे,
int a = 5;
float b = 0.5;
यहाँ C++ में कुछ assignment operator दिए गए हैं-
Operator | Name | description |
= | assignment | a = 5 assign values to operands |
+= | addition and assign | a += b // a = a + b add Left Side variable to the Right Side variable then assign the final result to the Left side variable. |
-= | a -= b // a = a – b same as above with different operation |
|
/= | a /= b // a = a / b same as above with different operation |
|
%= | a %= b // a = a % b same as above with different operation |
यहाँ नीचे इनका उदाहरण दिया गया है –
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int count,sum=0,mul=1; //variable initialization using assignment operator
for(count = 1; count<=5;count++)
{
sum += count; // store result left side
mul *= count; //
}
cout<<"Adition : "<<sum; // print variable sum value
cout<<"\nMultiply: "<<mul<<endl; // print variable mul value
getch();
}
OUTPUT
Addition: 15
Multiply: 120
C++ special operator
ये operator C ++ की library में pre -defined होते हैं। इस प्रकार के operators का प्रयोग C ++ में advanced task के लिए use किया जाता है ।
Operator | Name | Description |
# | Pound sign | program में header file include करने में , |
?: | Conditional or ternary Operator | conditional statement में , |
++ | increment | increase value one by one |
— | decrement | decrease value one by one |
& | reference operator | reference variable में , |
* | de-reference operator | To store the address of a variables |
sizeof() | sizeof operator | किसी data -type का size ज्ञात करने के लिए , |
, | comma operator | multiple variable, declaration या initialization में , |
:: | scope resolution operator | variable का scope change karne और class तथा structure में, outline definition के लिए |
. | dot operator | structure, union and class में member access करने के लिए |
-> | arrow operator | pointer से किसी class और structure के member को access करने में , |
new | new operator | dynamic memory allocation में |
delete | delete operator | dynamic memory को de-locate करने में , |
endl | new line feed operator | new line insert करने के लिए जैसे एक keyboard में “enter ” बटन। |
To change the scope of a variable and for the outline-definition of their function members in class and structure. |
यहाँ कुछ special operator के उदाहरण दिए गए हैं –
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int a =4; // assignment operator
cout<<"Address of count variable : "<<&a<<"\n"; // reference operator
cout<<"size of int data type(in byte): "<<sizeof(a); // sizeof operator
getch();
OUTPUT
Address of count variable : 0x8f87fff4
Size of int data type(int byte): 2
thanks sir