#3 Namespaces, Identifiers, Stream
Namespaces
The use of namespaces is a way for programmers to ensure that functions, variables, modules, etc., don’t clash with other parts of the program that may share the same name.
For example, if you have a function named calculate
that determines the center of gravity position on an airplane, and another programmer has one for calculating the thrust needed for aerodynamic neutrality, and both were named calculate
, the program may get confused.
Namespaces are incredibly helpful in avoiding conflicts in codebases, especially when working on large projects or collaborating with other developers. By using namespaces, you can create distinct scopes for your functions, variables, and modules. This prevents the program from getting confused by two entities that share the same name but serve different purposes.
To use namespaces you define it as follows:
namespace my_space {
// your code here!
}
Identifiers and Keywords
Every programming language has conventions for labeling values, which can include variables, constants, functions, prototypes, and more. One thing all these conventions have in common is that they follow specific rules.
In C++23, the rules are as follows:
-
An identifier can be any sequence of uppercase or lowercase letters, digits, or underscores.
-
An identifier must begin with a letter or an underscore.
-
Identifiers are case-sensitive.
Since programmers can create their own identifiers, it’s essential to be aware that the language itself already has special identifiers called keywords. Examples of these keywords include class
, int
, and throw
.
Streams
If we really think about what a computer is we can simplify it as a tool that takes input and generates output. With this concept c++ has a concept called streams. I use the word concept because it is an abstract that is use to generalize the process of input and output.
The <<
operator, known as the insertion operator, is used to send data to the output stream, usually std::cout
. On the other hand, the >>
operator, known as the extraction operator, is used to receive data from the input stream, typically std::cin
.
-
std::cout << "Enter your name: ";
sends the prompt to the console. -
std::cin >> name;
receives the user’s input and stores it in thename
variable.
Streams abstract the details of input and output, making the code cleaner and easier to understand. They help us focus on the high-level logic without worrying too much about the underlying details of how data is being read or written.