• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Operator » What is Operators? Explain Scope Resolution Operator and Operators Precedence.
Next →
← Prev

What is Operators? Explain Scope Resolution Operator and Operators Precedence.

By Dinesh Thakur

In addition to the operators like arithmetic operators, relational operators, logical operators, the conditional operator and assignment operators that most of the languages support, C++ provides various new operators that are given in Table.New Operator

We’ll be covering the following topics in this tutorial:

  • Scope Resolution Operator (::)
  • Operators Precedence

Scope Resolution Operator (::)

In C++, variables in different blocks or functions can be declared with the same name. That is, the variables in different scope can have the same name. However, a local variable overrides the variables having the same name in the outer block or the variables with global scope. Hence, a global variable or variable in the outer block cannot be accessed inside the inner block. This problem is solved by introducing new operator scope resolution operator :: introduced in C++.

The scope resolution operator : : is a special operator that allows access to a global variable that is hidden by a local variable with the same name. To understand the concept of scope resolution operator, consider this example.

A program to demonstrate the use of scope resolution operator

#include
using namespace std;
int x = 5;
int main () {
  int x = 3;
  cout<<”The local variable of outer block is: “<<X;
  cout<<”\nThe global variable is: “<<::X;
  int x = 10;
  cout <<”The local variable of inner block is: “<<X;
  cout <<”\n The global variable is: “<<::X;
 }
 return 0;
}

The output of the program is

The local variable of outer block is: 3

The global variable is: 5

The local variable of inner block is: 10

The global variable is: 5

In this example, three variables with same name x are declared. x declared outside main () has global scope, which is hidden by both the variables x declared in the outer block and inner block inside main ( ). The variable x inside the inner block overrides both variables (global variable and the variable declared in outer block).Thus, the global version of variable x is accessed using : : x in the inner and outer block.

Note that the main application of scope resolution operator is in the classes that will be discussed when the classes are introduced.

new and delete: C++ provides two dynamic allocation operators new and delete to allocate and deallocates memory at run-time respectively. The new operator is a unary operator that allocates memory and returns a pointer to the starting of the allocated space. The syntax of allocating memory using the new operator is

p_var = new data_type;

 where,

p_ var = the name of a pointer variable
new = C++ keyword
data_type = "Valid data type of C++"

For example, to dynamically allocate memory to a variable of type int at run-time, a pointer to type int is defined first and then the memory is allocated at run-time using the new operator as shown here.

int *iptr;&amp;amp;amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;amp;amp;nbsp; //pointer declaration
ptr = new int;  //allocating memory to an int variable

 In these statements, the new operator returns the address of the memory allocated for an int variable from the free store and this address is stored in the pointer iptr. The pointer declaration and allocation of memory can also be performed using a single statement as given here.

int *iptr = new inti;

Moreover, allocated memory using new operator can also be initialized. The syntax to initialize the memory using new operator is

p_var = new datatype (value) ;
where, value = initial_value.

For example, consider this statement.

float *fptr = new float (20.45) ; // initializing the newly variable 
and created variable

Like built-in data types, memory can also be allocated dynamically to derived and user-defined data types like arrays, structures and classes. These examples illustrate this concept.

 • To allocate memory to a one-dimensional array, consider these statements.

int *arr_iptr = new int [20] ; //allocating memory space to an array of
// 20 integers
float *arr_fptr = new float [10]; //allocating memory space to an array of
//10 float numbers

• To allocate memory to a multi-dimensional array, consider these statements.

int (*arr_iptr) [3] = new int [3][3]; //allocating memory to a 2-D array
int (*arr_iptr) [5][5] = new int [3][5][5]; //allocating memory to a 3-D array

Note that all the dimensions must be specified while creating multi-dimensional arrays with new operator. However, the first dimension can be a variable whose value is provided at run_time. For example, consider these statements.

int (*arr_iptr) [3] [5] new int [] [5] [5]; //invalid
int (*arr_iptr) [3] [5] new int [x] [5] [5]; //invalid
int (*arr_iptr) [3] [5] new int [3] [5] []; //invalid

The life-time of a variable created at run-time using the new operator is not restricted till the execution of the program. Rather, it remains in the memory until it is explicitly deleted using the delete operator. When a dynamically allocated variable is no longer required, it must be destroyed using the delete operator to ensure safe and efficient use of memory.

The syntax for using the delete operator is

delete p_var;

For example, to delete the pointers iptr and fptr of types int and float respectively, these statements are used.

delete iptr;
delete fptr;

Similarly, to delete the array pointers arr_iptr and arr_fptr of types int and float respectively, these statements are used.

delete [] arr_iptr;
delete [] arr_fptr;

The function performed by the new and delete operators is similar to the malloc () and free () library functions used in C. However, new and delete operators have several advantages over malloc ()and free ()which are listed here.

• new automatically returns a pointer to the appropriate data type. There is no need to explicitly typecast the pointer as required in malloc() .

• new automatically allocates enough memory to accommodate the object without using the sizeof operator.

• An object can be initialized while allocating space using the new operator.

• Both new and delete operators can be overloaded.

endl and setw: c++ provides various operators that can be used with an output statement to display the formatted output. These operators are known as manipulators. The manipulators which are commonly used are end1 and setw.

The endl manipulator causes a linefeed to be inserted when used with an output statement. The effect of this manipulator is the same as that of the newline character ‘\ n’. To understand the concept of endl, consider this statement.

Cout<<"Name: "<<name<<endl<<"Roll No: "<<rollno<<endl<<"Address: "<<address<<endl;

This statement would display the output in three lines, one for each variable. setw manipulator causes a value to be displayed right-justified within a specified field width. The field width is passed as an argument to this manipulator. To understand the concept of setw, consider these examples.

Example : A program to display the output without using setw

#include
using namespace std;
int main () {
  int Refrigerator = 15000, TV=6000, Almirah=800;
  cout<<”Refrigerator: "<<Refrigerator<<endl;
  cout<<”TV : "<<Tv<<endl;
  cout<<”Almirah: "<<Almirah<<endl;
  return 0;
}

The output of this program is

Refrigerator: 15000

TV : 6000

Almirah: 800

In this example, the output is unformatted, which makes it difficult to compare the values. However, this output can be formatted by using setw operator.

Example : A program to display formatted output using setw

#include
#include
using namespace std;
int main () {
  int Refrigerator = 15000, TV=6000 Almirah=800;
  cout<<setw(15)<<”Refrigerator:”<<setw(6)<<Refrigerator<<endl;
  cout<<setw(15)<<”Tv:"<<setw(6)<<TV<<endl;
  cout<<setw(15)<<”Almirah:”<<setw(6)<<Almirah<<endl;
  return 0;
}

The output of the program is

Refrigerator:15000

TV:6000

Almirah:800

In this example, setw(15) specifies the field width 15 for displaying the strings and setw(6) specifies the-field width 6 for displaying the value of variables. The values are right -justified within the specified field width.

Operators Precedence

Generally, an expression consists of more than one operator; hence, the compiler needs to know which operator is to be evaluated first. For this, it is important to determine the precedence and associatively of operators. The order in which different operators in an expression are evaluated is determined by the precedence of operators. The operators with a higher precedence are evaluated before the operators with a lower precedence. However, the order in which operators of the same precedence are evaluated is determined by the associatively of operators. The associatively of an operator can be either from the left to the right or from the right to the left. The operators with the left to right associatively are evaluated from the left -hand side while the operators with the right to left associatively are evaluated from the right -hand side.

The precedence and associatively of the C++ operators are listed in Table. Note that the precedence of operators decreases from top to bottom.

Precedence and Associativity of C++ Operators

You’ll also like:

  1. Scope Resolution Operator
  2. Explain Order of Precedence of Operators for Calculations
  3. Explain purpose of relational operators and logical operator
  4. What is Precedence of Operators
  5. What is Precedence and Associativity of Operators?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW