C++ Operators

C++ Operators

C++ Operators perform operations by taking one or more value, to give another value. These operations are performed on variables and values.

For example:  a + b, a – b, etc.

Scope Resolution Operator ::

Use this operator, if you want to access the global version of a variable. The global version remains the same throughout the program and the value remains the same even accessed from inner block. Here’s an example to easily understand the usage of scope resolution operator,

You can see on using scope resolution operator, the global version of the variable “a” can be accessed i.e. a = 10

Here’s the output,

Note: Learn how to create a program and run in Tubo C++

Memory Management Operators

Operators for managing memory in C++ includes new() and delete(). These operators allocate and free the memory. The new() operator can also create an object. If you want to destroy an object, you can easily do it with delete().

We will discuss both the operators with examples in the next chapter.

Type Cast Operator

Explicit type conversion is possible with the type cast operator.

Here’s the syntax,

Here’s an example,

Memory Dereferencing Operators

The major difference between C and C++ is that C++ has classes. C++ allows defining a class that consists of data and members. So, in C++ you can also access the members through pointers, using the member dereferencing operators.

::*   Declare a pointer to a member of a class

*    Accessing a member using an object name and a pointer to that member.

->*  Accessing a member using a pointer to the object and a pointer to that member.

new operator

Use the new operator to create objects of any type in C++. An object declared using new is alive till it is destroyed using delete operator. The new operator can be overloaded.

Here’s how to declare,

Here’s how you can initialize memory,

The new operator can be used with array to create a memory space. Let’s see how,

delete operator

The delete operator is used to destroyed objects created with the new operator,

Here’s how to declare,

The delete operator can be overloaded like the new operator.

C++ Data Types
C++ Decision Making Statements
Studyopedia Editorial Staff
Studyopedia Editorial Staff

We work to create free tutorials for all.

No Comments

Post A Comment