Operator in c++
Operators are the foundation of any programming language. C++ programming language is incomplete without the use of operators. Operators are used to performing specific mathematical and logical computations on operands. Operator in C++ is given below:-
C++ provide following types of operator:-
- Arithmetic operator :- + – * / % ++ —
- Relational operator :- == != > < >= <=
- Logical Operator :- && || !
- Bitwise operator :- & | ^ << >> ~
- Assignment operator :- = += -= *= /= %=
Scope resolution Operator:- ( : : )
It is use for following purpose –
- To access global variable when there is local variable with some name.
- To define a function outside a class.
- To access a class static variable.
- In the case of multiple inheritances, we can use this operator.
Example:- #include<iostream> using namespace std; int x; // Global variable x int main() { int x = 10; // Local variable x cout << "global value is" << ::x; cout << "\nlocal value is " << x; return 0; } Output:- global value is 0 local value is 10
Member deferring operator:- ( * )
In c++ a deferring operator is also known as an indirection operator.
Operator on pointer variable on a pointer variable returns the location value that is point to in memory.
Memory management operator:-
It is of two type:-
- New
- Delete
- New is the memory allocation operator.
- Delete is memory deallocating operator.
Example #include <iostream> using namespace std; int main () { double* p = NULL; // Pointer initialized with null p = new double; // Request memory for the variable *p = 29494.99; // Store value at allocated address cout << "Value of p : " << *p << endl; delete p; // free up the memory. return 0; } Output :- 29495
Manipulators :-
Manipulator are operator used in c++ for formatting output.
Example:- endl
Typecast operator:-
A cast is a special operator that forces one data type to be continued into another.
These are 4 types:-
- Static cast
- Dynamic cast
- Const_cast
- Reinterpreted cast
Special assignment operator:-
The special assignment operator is equal to ( = )
Example:-
x=y=20
operator overloading:-
If operator working for the different purpose that is known as operator overloading.
Example:- #include <iostream> using namespace std; class Test { private: int count; public: Test(): count(10) { } void oper ++() { count = count+1; } void Display() { cout<<"Count: "<<count; } }; int main() { Test t; ++t; t.Display(); return 0; } Output:- Count: 11