-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path06_operators.cpp
154 lines (121 loc) · 5.27 KB
/
06_operators.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Operators in C++
-------------------------------------------------------------
Summary:
-------------------------------------------------------------
- Unary Operator : ++ -- (Increment & Decrement)
- Ternary Operator: ?: (Conditional operator)
- Binary Operator : + - * / % (Arithmetic Operator)
& | ^ ~ << >> (Bitwise Operator)
&& || ! (Logical Operator)
= == += -= *= /= %= (Assignment Operator)
< <= > >= == != (Relational Operator / Comparison Operator)
- Member Access : a[b] *a &a a->b a.b a->*b a*b
- Other : a(...) a,b
-------------------------------------------------------------
Detail Reference:
-------------------------------------------------------------
- Increment/Decrement : ++a
--a
a++
a--
- Arithmetic Operator : a+b Add
a-b Subtract
a*b Multiply
a/b Divide (return quotient)
a%b Modulus (return remainder)
- Bitwise Operator : a&b Bitwise AND
a|b Bitwise OR
a^b Bitwise XOR
~a Bitwise NOT (Binary One's Complement) (bit inversion)
a<<b Left Shift
a>>b Right Shift
- Logical Operator : !a Logical and Eg: !(x < 5 && x < 10)
a&&b Logical or Eg: x < 5 && x < 10
a||b Logical not Eg: x < 5 || x < 4
- Assignment Operator : a=b Assignment operator (=)
a==b Compound assignment
a+=b
a-=b
a*=b
a/=b
a%=b
a&=b Compound assignment
a|=b
a^=b
a<<=b
a>>=b
- Comparison Operator : < Less than
(RelationalOperator) <=
> Greater than
>=
== Equals to
!= Not Equal
<=> Three-Way Comparison operator
- Member Acceess : a[b] subscript
*a reference
&a dereference (represents memory address of the operand) Eg: # // address of num
a->b member access (used with pointers to access the class or struct variables)
a.b member access (accesses members of struct variables or class objects)
a->*b access pointer
a.*b access pointer
:: Scope resolution operator
::* Pointer-to-member declaration (Pointer to class data member "::*")
- Other : a, b Comma
? : Conditional ternary operator
sizeof returns the size of data type Eg: sizeof(int);
new Memory allocation operator
delete Memory release operator
endl Line feed operator
setw Field width operator
(type) C-style type-casting
a(...) "three dots" syntax (...) inside the catch block,
which will handle any type of exception.
*/
#include <iostream>
using namespace std;
int main(){
//Comma Operator
int a,b,c;
//Assignment Operator
a=10;
b=20;
c=30;
//Logical Operator
if(c>a and c>b){
cout<<"C is largest"<<endl;
}
//Ternary Operator
int x = c%2==0 ? 1 : 0;
cout<<x<<endl;
c%2==0 ? cout<<"True" : cout<<"False";
cout<<endl;
//Bitwise Operator
x=5;
int y=7;
cout<<"AND "<<(x&y)<<endl;
cout<<"OR "<<(x|y)<<endl;
cout<<"XOR "<<(x^y)<<endl;
//Shift Operator
x = x<<2;
cout<<x<<endl; // Expected answer: 20
cout<<(y>>1)<<endl; // Expected answer: 3
//Unary Operator
cout<<"Address of x: "<<(&x)<<endl;
//Post Increment / Decrement Operator
a=10;
int d = a++; //d=10 a=11
cout<<"Value of d: "<<d<<endl;
d = ++a; // d=12 a=12
cout<<"Value of d: "<<d<<endl;
//Compound Assignment Operator
a = 10;
a *= 3; // a = a*3
cout<<"A after multiply: "<<a<<endl;
a %=5; // a = a%5
cout<<"A after modulo: "<<a<<endl;
b=5;
b <<=1;
cout<<"B after left shift: "<<b<<endl;
return 0;
}