Cpp Tutorial: C++ Special operators

In this tutorial we will learn about below topics:
1. Referencing operator
2. Dereferencing operator
3. Scope resolution operators
4. Memory management operators
5. Namespace
6. Comma operator
7. Arrow operator
8. This pointer

1. Referencing Operator:

Referencing operator (&): This operator is used to define a referencing variable. A reference variable is used to provide an alternative (alias) name for already defined variable.
Syntax:
Data-type &new-name = existing-name;
Example:
int amount = 100;
int &rupees = amount;
In the above example, there we have created a variable with the name “amount”. Then we have created a reference variable “&rupees” and assigned it to amount.
Changing the value of “rupees” will change the value of “amount”.
Below are some of the pointers to be noted for reference variables:
1. Once a variable is assigned to a reference variable, then it should not be reassigned to other variable.
2. Single variable can be assigned to multiple reference variables. Changing the value of one reference variable, will change the value of all the variables.
3. When “&” is used in declaration then it will behave as a reference operator, when used in the expression then it is an address operator.
Example of referencing operator:
#include<iostream>
using namespace std;

int main()
{
	int price = 100;
	
	int &value = price;	// reference variable
	int	&rupees = price; // one variable can have multiple reference variable
	cout<< "Initial value" << " price = "<< price <<" value = "<< value<< " rupees = "<<rupees<<endl;
	value = 200;
	cout<< "After changing value" << " price = "<<price <<" value ="<< value<< " rupees = "<<rupees<<endl;
return 0;
}
Output:
Initial value price = 100 value = 100 rupees = 100
After changing value price = 200 value =200 rupees = 200

2. Dereferencing operator (*)

This operator when used in an expression, it is used to get the value that is stored in the address that the variable is holding.
We shall study about dereferencing operator in pointers chapter.

3. Scope resolution operator.

Scope resolution operator “::” can be used as a unary or binary operator. Below we discuss about both of the options.
Scope resolution as a unary operator:
1. We know that if there a local variable name same as a global variable inside a class, then the preference is given to local variable.
2. We use scope resolution operator to access the global variable.
Example:
#include<iostream>
using namespace std;

int i = 10;

int main()
{
 int i = 20;
 
 cout<<"The value of local variable i is " <<i<<endl; 
 cout<<"The value of global variable i is " <<::i<<endl; 

return 0;
}

Output:

The value of local variable i is 20
The value of global variable i is 10

Scope resolution as a binary operator:

1. This is used to define member function prototype inside a class and the actual function definition outside a class.
2. Multiple classes can have same member function name. This will help compiler to distinguish which function belongs to which class.
3. “::” is the symbol of scope resolution operator.

Syntax:

function_return_type class_name :: member_function_name
Example:
#include<iostream>
using namespace std;

class Sum 
{
	int num1 = 10;
	int num2 = 20;
	public:
	
	void sum();
};

void Sum::sum()
{
	cout<<"The sum of num1 + num2 is "<<(num1 + num2)<<endl;
}

int main()
{
	Sum s1;
	s1.sum();
	
return 0;
}
Output:
The sum of num1 + num2 is 30

4. Memory management operators

C++ provides two new methods to allocate and destroy memory. They are “new” and “delete”.
“new” is used to allocate memory.
The new operator will internally calculate the size of the object and allocate appropriate memory.
It returns a pointer pointing to that memory location, of the same type. Type casting is done internally.
It also initializes the memory.
Syntax:
Pointer_variable = new <data-type>;
“delete” is used to delete the allocated memory.
It is used when the memory is no longer needed.
Syntax:
delete <variable_name>;
Example for new and delete:
#include<iostream>
using namespace std;

int main()
{
	int *variable = new int;
	int *array = new int[10];
	delete(variable);
	delete []array;
return 0;
}

5. Namespace:

Namespace is used to define the scope of the variables.
There can be more than one namespace is a C++ application.
Usually we use “std” namespace while writing C++ programs. It has standard objects like cout, cin etc. Hence it is sometimes called as global namespace.
Below is the syntax for defining a namespace.
namespace <namespace_name>
{	
	//statements
}

Note:

1. We use namespace keyword for defining namespace.
2. Namespace do not end with a semicolon.

Example:

#include<iostream>
using namespace std;
namespace test_namespace
{
	int iNum = 123;
	float fNum = 123.456; 
}

int main()
{
	cout<< test_namespace::iNum <<endl;
	cout<< test_namespace::fNum <<endl;
	return 0;
}
Output:
123
123.456

6. Comma operator:

In C++ comma operator “,” can be used to define a block instead of curly braces “{}”. Comma operator cannot be used to define function blocks. It is only used to define block for “if..else”, looping blocks.
Example of comma operator:
#include<iostream>
using namespace std;

int main()
{
	int num = 10;
	if (num == 10)
		cout<<"inside if block"<<endl,
		cout<<"we are using comma operator"<<endl,
		cout<<"end of block"<<endl; // end of the block should be ended with semi-colon
	else
		cout<<"the value of num is not 10",
		cout<<"end of else block";
return 0;
}
Output:
inside if block
we are using comma operator
end of block

7. Arrow operator

Arrow operator is used to access the object through a pointer.
Example:
#include<iostream>
using namespace std;

class Diff
{
	int a;
	int b;
	public:
	void initialize()
	{
	   a = 30;
	   b = 10;
	}
	void printDiff()
	{
	   cout<<"The difference is "<<(a - b)<<endl;
		
	}
};

int main()
{

	Diff d1;
	Diff *diffPtr;
	
	diffPtr= &d1;

	diffPtr->initialize(); //same as calling a.initialize()
	diffPtr->printDiff(); // same as calling a.printDiff()

return 0;
}
Output:
The difference is 20

8. This pointer

1. This pointer is a constant pointer that holds the address of the current object.
2. When we create an object and call a member function, then automatically “this” pointer will be passed.
3. “this” pointer is not available to static member functions, because static member functions can be called without creating an object.
Example 1:
“this” pointer is used when the local variable name is same as data member name.
#include<iostream>
using namespace std;

class Example
{
	int a;
	int b;
	public:
	
	void setValues(int a , int x)
	{
		this->a = a;
		this->b = x;
	}
	
	void getValues()
	{

		cout<<"The value of a = "<<this->a<<" The value of b = "<<this->b;
	}
};

int main()
{
	Example e1;
	
	e1.setValues(10, 20);
	e1.getValues();
}

Output:

The value of a = 10 The value of b = 20
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *