:: operator allow to access global version of a variable.
C++ is a block structured language. Same variable name in different blocks have different meaning.
The scope of a variable is only limited to the block in which it is declared.
C++ is a block structured language. Same variable name in different blocks have different meaning.
The scope of a variable is only limited to the block in which it is declared.
Consider example :
Here block 2 defined in block 1. variable x defined in two blocks with different values.
The variable x holds the value 20 in block 2. if we access the value of x using scope resolution
operator it will return 10.
cout<< x will return value of x=20
cout<<::x will return value of x=10
The variable x holds the value 20 in block 2. if we access the value of x using scope resolution
operator it will return 10.
The general form is :
:: variable name.
in block 2 the statementcout<< x will return value of x=20
cout<<::x will return value of x=10
New operator is used to allocate memory dynamically for an object. It remain in existence until it is destroyed explicitly by delete.
The syntax for New operator is:
pointer variable = new data type;
int *p = new int(5);
When a object is no longer needed it is destroyed to release memory space. The general form of delete is:
delete pointer variable
delete p;
The endl operator causes linefeed to be inserted. It has the sane effect as using the newline character “\n”.
For example:
cout<<“enter vaslur<<endl;
or
cout<<“enter value”<<“\n”;
or
cout<<“enter value”<<“\n”;