Small Project Organization
Namespace Helpers
A namespace groups helper names so a small project can avoid accidental collisions.
namespace
Namespaces give related functions a shared prefix without putting everything into one global name pool.
qualification
Calling `billing::addFee` makes the helper's ownership visible at the call site.
Namespace Helpers
namespace_helpers.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
namespace billing {
int addFee(int amount, int fee) {
return amount + fee;
}
}
int main() {
int subtotal = 35;
int fee = 4;
int total = billing::addFee(subtotal, fee);
std::cout << "subtotal=" << subtotal << std::endl;
std::cout << "fee=" << fee << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
namespace billing {
int addFee(int amount, int fee) {
return amount + fee;
}
}
int main() {
int subtotal = 10;
int fee = 4;
int total = billing::addFee(subtotal, fee);
std::cout << "subtotal=" << subtotal << std::endl;
std::cout << "fee=" << fee << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
namespace billing {
int addFee(int amount, int fee) {
return amount + fee;
}
}
int main() {
int subtotal = 80;
int fee = 4;
int total = billing::addFee(subtotal, fee);
std::cout << "subtotal=" << subtotal << std::endl;
std::cout << "fee=" << fee << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
subtotal ← 35, fee ← 4
9int main() {10 int subtotal→ 35 = 35; //@subtotal=10, 801112 int fee→ 4 = 4;13 int total = billing::addFee(subtotal35, fee4);int addFee(int amount, int fee)
3namespace billing {4int addFee(int amount35, int fee4) {5 return amount35 + fee4;6}total ← 39
12 int fee = 4;13 int total→ 39 = billing::addFee(subtotal35, fee4);1415 std::cout << "subtotal=" << subtotal35 << std::endl;16 std::cout << "fee=" << fee4 << std::endl;17 std::cout << "total=" << total39 << std::endl;18 return 0;19}outputsubtotal=35 fee=4 total=39
subtotal ← 10, fee ← 4
9int main() {10 int subtotal→ 10 = 10;1112 int fee→ 4 = 4;13 int total = billing::addFee(subtotal10, fee4);int addFee(int amount, int fee)
3namespace billing {4int addFee(int amount10, int fee4) {5 return amount10 + fee4;6}total ← 14
12 int fee = 4;13 int total→ 14 = billing::addFee(subtotal10, fee4);1415 std::cout << "subtotal=" << subtotal10 << std::endl;16 std::cout << "fee=" << fee4 << std::endl;17 std::cout << "total=" << total14 << std::endl;18 return 0;19}outputsubtotal=10 fee=4 total=14
subtotal ← 80, fee ← 4
9int main() {10 int subtotal→ 80 = 80;1112 int fee→ 4 = 4;13 int total = billing::addFee(subtotal80, fee4);int addFee(int amount, int fee)
3namespace billing {4int addFee(int amount80, int fee4) {5 return amount80 + fee4;6}total ← 84
12 int fee = 4;13 int total→ 84 = billing::addFee(subtotal80, fee4);1415 std::cout << "subtotal=" << subtotal80 << std::endl;16 std::cout << "fee=" << fee4 << std::endl;17 std::cout << "total=" << total84 << std::endl;18 return 0;19}outputsubtotal=80 fee=4 total=84