Q14. Explain about the scope of class.
Answer:
Class Scope
Class scope defines the accessibility or visibility of class variables or functions. The term scope is defined as a place where in variable name, function name and typedef are used inside a program. The different types of scopes are as follows,
- Local scope
- Function scope
- File scope
- Class scope and
- Prototype scope.
1. Local Scope
The variables that are declared in a function block are called local variable which have local scope. These variables can only be accessed by the function/block in which they are declared. The same variable can be used in other functions but it need to define their respective new scope. It is possible to insert a block within another block. The variable name given to a particular variable is local for the block and sub-block present inside it.
The formal parameter posses local scope and acts as it belongs to the outermost block.
Example
void func
{
int i;
}
In the above function, since ‘i’ is declared inside the block, the integer ‘i’ has local scope. This integer can not be accessed because there is no code written for accessing it.
2. Function Scope
The variables defined inside the function will have function scope. These variables can only be accessed inside the function.
Example
void a()
{
//statements
int label;
{
goto label; //label in scope even
though declared later
}
goto label; //label ignores block
scope
}
void b( )
{
goto label;
}
3. File Scope
The identifiers which are present outside the definition of function or class will have file scope. This scope consist of local scope as well as class scope and it will be a in the outermost part of program. The variables defined with file scope are known as global variables.
These variables can be accessed by all the functions in the program. Default value for global variable is zero.
Example
int d; // Global variable
declaration
void display( ); //Global function
declaration
void main( )
{
d= 8; // Assignment of global
variable 'd'
void display( ) // Global function
called by main
{
cout<<"The value of d
is" <<d<<"\n";
}
void display( )
{
cout<<d; //Global variable is
accessible here also
}
}
4. Class Scope
The data members and member functions defined in a class will have class scope. They are local only to that class. Each member possess unique scope.
Example
class A
{
int a(int x = n)
{
//statements
return x * n;
}
int b( );
int i=n* 2;
static const int n = 1;
int A[n];
};
int A: : b()
{
return n;
}
5. Prototype Scope
The variables that are declared inside a function prototype have prototype scope, They are local only up to that prototype.
Example
int fun(int p, int q);
main ()
{
int a= 5, b = 10;
fun(a, b);
}
int fun(int p, int q)
{
int sum = p + q;
return sum;
}
Program
#include<iostream.h>
#include<conio.h>
int a,b; // global variables
int fun(int);
void main()
{
clrscr();
int num1, num2; // local variables
of main()
cout<<"Enter any two
numbers: ";
cin>>num1>>num2;
if(num1==num2)
{
int temp; // block scope (local
variable of this if block)
temp = num1 + num2;
cout<<"\nValue :
"<<fun(temp);
}
else
{
cout<<fun(num1)<<"\t"<<fun(num2);
}
getch();
}
int fun(int x) // function scope
for fun()
{
int res; // local variable of
fun()
res = x*2;
return res;
}