Chapter: Programming and Data structures : Object Oriented Programming Fundamentals

Storage class

Storage class defined for a variable determines the accessibility and longevity of the variable. The accessibility of the variable relates to the portion of the program that has access to the variable.

STORAGE CLASS:

 

Storage class defined for a variable determines the accessibility and longevity of the variable. The accessibility of the variable relates to the portion of the program that has access to the variable. The longevity of the variable refers to the length of time the variable exists within the program.

 

 

Types of Storage Class Variables in C++:

 

1.     Automatic

 

2.     External

 

3.     Static

 

 

Automatic:

 

Variables defined within the function body are called automatic variables. Auto is the keyword used to declare automatic variables. By default and without the use of a keyword, the variables defined inside a function are automatic variables.

 

 

For instance:

 

void exforsys( )

 

{

 

auto int x;

 

auto float y;

 

...

 

}

 

is the same as

 

 

 

void exforsys( )

 

{

 

int x;

 

float y;         //Automatic Variables

 

...

 

}

 

In the above function, the variable x and y are created only when the function exforsys( ) is called. An automatic variable is created only when the function is called. When the function exforsys( ) is called, the variables x and y are allocated memory automatically.

 

 

External:

 

External variables are also called global variables. External variables are defined outside any function, memory is set aside once it has been declared and remains until the end of the program. These variables are accessible by any function. This is mainly utilized when a programmer wants to make use of a variable and access the variable among different function calls.

 

 

Static:

 

The static automatic variables, as with local variables, are accessible only within the function in which it is defined. Static automatic variables exist until the program ends in the same manner as external variables. In order to maintain value between function calls, the static variable takes its presence.

 

 

For example:

 

#include <iostream.h> using namespace std; int exforsys(int);

 

int exforsys2(int); void main( )

{

 

int in; int out; while(1)

{

 

 

cout << "nEnter input value:"; cin>>in;

 

if (in == 0) break;

 

out=exforsys(in);

 

cout << "nResult : " << out; out=exforsys2(in);

 

cout << "nResult2: " << out;

 

}

 

cout << "n End of Program " ;

 

}

 

int exforsys(int x)

 

{

 

static int a=0; a++; return(a);

 

}

 

int exforsys2(int x)

 

{

 

int b=0; b++; return(b);

 

}

 

In the above program, the static variables a is initialized only once in the beginning of the program. Then the value of the variable is maintained between function calls.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : Object Oriented Programming Fundamentals : Storage class |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.