Scope Resolution Operator
Scope Resolution Operator
- C++ is block structured language.
- Same variable name is used in different block for different tasks.
- The scope of the variable is starts form declaration till the end of that block.
- In other word you can use variable in the block where we declare it.
- Scope Resolution operator denotes with sign colon[:]
Example:
................
................
block A
{
.................
int a=10;
................
}
.................
.................
block B
{
.................
.................
int a=12;
................
................
}
..............
- In this example there are 2 blocks .
- And in both the block variable a is declared.
- But the values are different in both the block.
- If we print variable's value inside of the block A it will print 10.
- Same as if we print the value inside the block B it will print 12.
W.A.P. to find largest number
from two numbers using of scope resolution operator.
CODE:
#include<iostream>
using
namespace std;
class
set
{
int
a,b;
public:
void
input(void);
void
display(void);
int
largest(void);
};
int
set :: largest(void)
{
if(a>=b)
return
(a);
else
return(b);
}
void
set :: input(void)
{
cout
<< "W.A.P. to find largest number from two numbers using of nesting
member function.\n\n\n";
cout<<"Input
values of a and b:" << "\n";
cin
>> a >>b;
}
void
set :: display(void)
{
cout<<"Largest
value ="<< largest( ) << "\n";
}
int
main( )
{
set
A;
A.input(
);
A.display(
);
return
0;
}
OUTPUT:
0 Comments