Scope Resolution Operator, Dot Operator, Operator Precedence, Reference Variable, and Type Casting
Scope Resolution Operator, Dot Operator, Operator Precedence, Reference Variable, and Type Casting
P R E S E N T E D TO :
S AU M E N D U R OY ( S A U M O ) P R E PA R E D BY:
LECTURER,DEPT OF C.S.E ID:11190320383
N U BT K
Content Layout with List
• Definition
• Functionality
• Example
• Why it is used
Scope resolution operator
• Scope resolution operator (::) in C++ is used to define a function outside a class or
when we want to use a global variable but also has a local variable with the same name.
•#include <iostream>
•using namespace std;
• int a = 100; // global variable
• int main() {
• int a = 200; // local variable
• cout << "Local variable: " << a << "\n";
• cout << "Global variable: " << ::a << "\n";
• return 0;
•}
Use of scope resolution operator
Highest ()
++ -- (prefix) + - (unary) ! ~
*/%
+-
<< >>
Lower ^
Example :
#include <iostream>
using namespace std;
int main()
{
int num1 = 10, num2 = 30;
Int result;
result = num1 * 2 + num2;
Cout<<” result value is ”<<
result <<endl;
return 0;
}
Use of operator precedence
‘Comma’ operator has lowest precedence.
‘Unary Operators’ are operators having highest precedence.
Operators sharing common block in the above table have equal priority or
precedence.
While solving expression, equal priority operators are handled on the basis of FIFO
[First in First Out]
Type Casting
Type Casting is used to convert the type of a variable, function, object,
expression or return value to another type. It is the process of converting one
type into another. In other words converting an expression of a given type into
another is called type casting.
Example:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
Double p;
P = i ; // Implicit Conversion
Cout<<” Implicit value is ”<< p <<endl;
return 0; }
Explicit Conversion
Explicit conversion can be done using type cast operator. In other words,
the value that gets converted to the specific type by using type cast
operator is known as explicit type conversion.
Example:
#include <iostream>
using namespace std;
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
cout << "Sum = " << sum;
return 0; }
Reference variable
A reference variable is an alias, that is, another name for an already existing variable.
Once a reference is initialized with a variable, either the variable name or the
reference name may be used to refer to the variable.