Namespaces and Organization
Namespace Basics
A namespace groups related names so they do not crowd the global scope.
namespace
A namespace wraps functions, types, or variables inside a named group.
Namespace Basics
namespace_basics.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
namespace math_tools {
int doubleValue(int number) {
return number * 2;
}
}
int main() {
int value = 4;
int result = math_tools::doubleValue(value);
std::cout << "value=" << value << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
namespace math_tools {
int doubleValue(int number) {
return number * 2;
}
}
int main() {
int value = 7;
int result = math_tools::doubleValue(value);
std::cout << "value=" << value << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
namespace math_tools {
int doubleValue(int number) {
return number * 2;
}
}
int main() {
int value = 10;
int result = math_tools::doubleValue(value);
std::cout << "value=" << value << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
value ← 4
9int main() {10 int value→ 4 = 4; //@value=7, 101112 int result = math_tools::doubleValue(value4);int doubleValue(int number)
3namespace math_tools {4int doubleValue(int number4) {5 return number4 * 2;6}result ← 8
12 int result→ 8 = math_tools::doubleValue(value4);1314 std::cout << "value=" << value4 << std::endl;15 std::cout << "result=" << result8 << std::endl;16 return 0;17}outputvalue=4 result=8
value ← 7
9int main() {10 int value→ 7 = 7;1112 int result = math_tools::doubleValue(value7);int doubleValue(int number)
3namespace math_tools {4int doubleValue(int number7) {5 return number7 * 2;6}result ← 14
12 int result→ 14 = math_tools::doubleValue(value7);1314 std::cout << "value=" << value7 << std::endl;15 std::cout << "result=" << result14 << std::endl;16 return 0;17}outputvalue=7 result=14
value ← 10
9int main() {10 int value→ 10 = 10;1112 int result = math_tools::doubleValue(value10);int doubleValue(int number)
3namespace math_tools {4int doubleValue(int number10) {5 return number10 * 2;6}result ← 20
12 int result→ 20 = math_tools::doubleValue(value10);1314 std::cout << "value=" << value10 << std::endl;15 std::cout << "result=" << result20 << std::endl;16 return 0;17}outputvalue=10 result=20