C++ Name visibility | Scopes | Namespaces | Using | Storage Classes

Опубликовано: 09 Октябрь 2024
на канале: Sriramasubramaniam Nagarajan
50
4

C++ Name visibility | Scopes | Namespaces | Using | Storage Classes | C++ Programming in Tamil

Scopes

Named entities, such as variables, functions, and compound types need to be declared before being used in C++. The point in the program where this declaration happens influences its visibility:

An entity declared outside any block has global scope, meaning that
its name is valid anywhere in the code. While an entity declared
within a block, such as a function or a selective statement, has 
block scope, and is only visible within the specific block in which
it is declared, but not outside it.

Variables with block scope are known as local variables.

Namespaces

Only one entity can exist with a particular name in a particular scope. This is seldom a problem for local names, since blocks tend to be relatively short, and names have particular purposes within them, such as naming a counter variable, an argument, etc...

But non-local names bring more possibilities for name collision,
especially considering that libraries may declare many functions,
types, and variables, neither of them local in nature, and
some of them very generic.

Namespaces allow us to group named entities that otherwise
would have global scope into narrower scopes, giving them 
namespace scope. This allows organizing the elements of programs
into different logical scopes referred to by names.

Namespaces are particularly useful to avoid name collisions.

Using

The keyword using introduces a name into the current declarative region (such as a block), thus avoiding the need to qualify the name

Namespace aliasing

Existing namespaces can be aliased with new names
namespace new_name = current_name

The std namespace

All the entities (variable, types, constants and functions) of the standard C++ library are declared within the std namespace
using namespace std;

Storage Classes

The storage for variables with global or namespace scope is allocated for the entire duration of the program. This is known as static storage, and it contrasts with the storage for local variables (those declared within a block). These use what is known as automatic storage. 

The storage for local variables is only available during the block in which they are declared; after that, that same storage may be used for a local variable of some other function, or used otherwise.

But there is another substantial difference between variables with static storage and variables with automatic storage:- Variables with static storage (such as global variables) that are not explicitly initialized are automatically initialized to zeroes.- Variables with automatic storage (such as local variables) that are not explicitly initialized are left uninitialized, and thus have an undetermined value.

The actual output may vary, but only the value of x is guaranteed to be zero. y can actually contain
just about any value (including zero).

****************************************************************************

YouTube -    / sriramasubramaniamnagarajan  

Connect me @
instagram:   / sriramasubramaniam  
sololearn: https://www.sololearn.com/Profile/104...

PlayList:

MovieBuff -    • Moviebuff  

Hawkeye -    • Hawkeye  

PHP Tutorial :    • PHP Tutorial  

JavaScript Tutorial -    • JavaScript Tutorial  

Python Classes and Objects -    • Python Classes and Objects  

Python CSV -    • Python CSV  

C programming -    • C Programming Language  

Python Programming -    • Python Programming  

Python Programs -    • Python programs  

DataBase Concepts -    • DataBase Concepts  

canva - https://www.canva.com/join/simplified...

#C++Programming #Namespace