0% found this document useful (0 votes)
31 views19 pages

Scope Resolution Operator, Dot Operator, Operator Precedence, Reference Variable, and Type Casting

The document provides definitions and examples of scope resolution operator, dot operator, operator precedence, reference variable, and type casting in C++. The scope resolution operator (::) is used to access global variables or functions outside of a class. The dot operator (.) is used to access members of a class object. Operator precedence determines the order of operations in an expression. Reference variables create aliases to existing variables. Type casting converts a variable from one type to another, either implicitly without an operator or explicitly with a cast operator like (int).

Uploaded by

Nijhum Choudhury
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
31 views19 pages

Scope Resolution Operator, Dot Operator, Operator Precedence, Reference Variable, and Type Casting

The document provides definitions and examples of scope resolution operator, dot operator, operator precedence, reference variable, and type casting in C++. The scope resolution operator (::) is used to access global variables or functions outside of a class. The dot operator (.) is used to access members of a class object. Operator precedence determines the order of operations in an expression. Reference variables create aliases to existing variables. Type casting converts a variable from one type to another, either implicitly without an operator or explicitly with a cast operator like (int).

Uploaded by

Nijhum Choudhury
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

Presentation On

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.

Functionality of Scope resolution operator 


• This operator is used to access data member function outside of the class.
• Used to access global variable inside the main function.
• Used to access hidden data.
• If the local and global variables are same name then the local variable gets first priority.
Example:

•#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

• The scope resolution operator :: is used to


identify and disambiguate identifiers used
in different scopes.
• Lets keep it simple. The scope resolution
operator (::) basically does two things:
• Access the global variable when we have a
local variable with same name
• Define a function outside the class
Dot operator
 Dot operator(.) is used to access the entrance variabls and methodses of class objects.

Functionality of Dot[.] operator


Dot[.] operator is used for access the member function
outside of the class.
It is used for invoke the member functions.
Syntax:
classname.Member function(arguments)
Example:
• #include<iostream> • };
• using namespace std; • int main()
• class population • {
• { • population x1;
• public: • string name ;
• int print(string name,int id) • int id;
• • cin>>name>>id;
{ cout<<“Name:”<<name<<endl<<ID:<<i
d<<endl; • x1.print(name,id);//using dot(.)
operator
}
• return 0;}
Use of dot(.) operator
Dot(.) operator used to access public members of a class
About member variable:
Object_name.public_member_variable_name=value
About member function:
Object_name.public_member_function_name=(argument_list)
Operator Precedence
Operator precedence determines the grouping of terms in an expression
and decides how an expression is evaluated. Certain operators have
higher precedence than others; for example, the multiplication operator has
a higher precedence than the addition operator.

Functionality of Operator precedence 


Precedence rules decides the order in which different operators are applied.
Associativity rule decides the order in which multiple occurrences of the
same level operator are applied.
C Operators with their Precedence & Associativity are listed in the following
table.
Precedence Operators

Highest ()

  ++ -- (postfix) new typeof

  ++ -- (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.

Types of Type Casting


There are two ways of achieving the Type Casting. These are:
• Implicit Conversion
• Explicit Conversion
Implicit Conversion
Implicit type conversion is not done by any conversions or operators. In other
words, the value that gets automatically converted to the specific type to
which it is assigned, is known as implicit type conversion.

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.

Functionality of reference variable


A reference variable is an alias for another variable.
Defined with an ampersand(&)
Void getDimensions(int&,int&);
Changes to a reference variable are made to the variable it refers to.
Use reference variable to implement passing parameters by reference.
Example :
#include <iostream>
using namespace std;
int a = 100;
int main() {
int a = 20, b = 40;
int & c = a;
cout << " a " << a << endl;
cout << " c " << c << endl;
c = b;
cout << " a " << a << endl;
cout << " c " << c << endl;
return 0;
}
use of reference variable in c++

A Reference Variable can also be used in function parameter, this is known


as call by reference.
1. It permits the manipulation of objects by reference.
2. It eliminates the copying of object parameters.
3. Reference variables can be created for user defined data types such as
structures and classes.
Thank you

You might also like