---
jupytext:
formats: md:myst
text_representation:
extension: .md
format_name: myst
kernelspec:
display_name: Python 3
language: python
name: python3
---Variables, Scopes and Namespaces: Namespaces¶
Adapted from: “Learn Modern C++” by cpptutor: Learn Modern C++: Variables, Scopes and Namespaces
Program that Demonstrates using Namespaces¶
// 02-height.cpp : define the same variable name in two different namespaces
#include <print>
using namespace std;
namespace Wonderland {
auto alice_height_m{ 0.15 };
}
namespace VictorianEngland {
auto alice_height_m{ 0.9 };
}
int main() {
println("Alice\'s height varies between {}m and {}m",
Wonderland::alice_height_m,
VictorianEngland::alice_height_m);
}Explanation of the Above Code¶
This C++ code demonstrates the use of namespaces to define variables with the same name in different contexts, avoiding naming conflicts.
Code Breakdown:¶
#include <print>
using namespace std;The
<print>header is used for formatted output (similar tostd::formatin C++20). It allows the use ofprintlnfor printing formatted strings.using namespace std;allows direct access to standard library functions without prefixing them withstd::.
namespace Wonderland {
auto alice_height_m{ 0.15 };
}A namespace
Wonderlandis defined.Inside this namespace, a variable
alice_height_mis declared and initialized with the value0.15(meters).
namespace VictorianEngland {
auto alice_height_m{ 0.9 };
}Another namespace
VictorianEnglandis defined.Inside this namespace, a variable
alice_height_mis declared and initialized with the value0.9(meters).
Purpose of Namespaces:¶
Namespaces allow variables with the same name (
alice_height_m) to coexist without conflict because they are scoped to their respective namespaces (WonderlandandVictorianEngland).
int main() {
println("Alice's height varies between {}m and {}m",
Wonderland::alice_height_m,
VictorianEngland::alice_height_m);
}The
mainfunction prints a formatted string using theprintlnfunction.The variables
Wonderland::alice_height_mandVictorianEngland::alice_height_mare accessed using the namespace resolution operator (::).The output will display the values of
alice_height_mfrom both namespaces.
Output:¶
The program will produce the following output:
Alice's height varies between 0.15m and 0.9mKey Concepts:¶
Namespaces: Namespaces are used to organize code and prevent naming conflicts by grouping related variables, functions, or classes.
Namespace Resolution Operator (
::): This operator is used to access members of a specific namespace.Variable Scoping: The same variable name can exist in different namespaces without conflict.
This program demonstrates how namespaces can be used to manage naming conflicts in C++ effectively.
Compile and Run Code¶
Use Python to Change to Working Directory¶
import os
root_dir = os.getcwd()code_dir = root_dir + "/" + "Cpp_Code/02_Variables_Scopes_and_Namespaces"os.chdir(code_dir)Use Docker to Compile the Code in a C++23 Environment¶
!docker run --rm -v $(pwd):/app cpp23-clang18:latest clang++-18 -std=c++23 -stdlib=libc++ /app/02-height.cpp -o /app/02-heightUse Docker to Run Executable in a C++23 Environment¶
!docker run --rm -v $(pwd):/app cpp23-clang18:latest ./02-heightAlice's height varies between 0.15m and 0.9m