Namespaces and Organization
Qualified Names
The :: operator chooses a name from a specific namespace.
qualified name
A qualified name includes the namespace, `::`, and the name to use.
Qualified Names
qualified_names.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <string>
namespace checkout {
std::string status(int cents) {
if (cents >= 1000) {
return "large";
}
return "small";
}
}
int main() {
int totalCents = 750;
std::string size = checkout::status(totalCents);
std::cout << "totalCents=" << totalCents << std::endl;
std::cout << "status=" << size << std::endl;
return 0;
}
#include <iostream>
#include <string>
namespace checkout {
std::string status(int cents) {
if (cents >= 1000) {
return "large";
}
return "small";
}
}
int main() {
int totalCents = 250;
std::string size = checkout::status(totalCents);
std::cout << "totalCents=" << totalCents << std::endl;
std::cout << "status=" << size << std::endl;
return 0;
}
#include <iostream>
#include <string>
namespace checkout {
std::string status(int cents) {
if (cents >= 1000) {
return "large";
}
return "small";
}
}
int main() {
int totalCents = 1200;
std::string size = checkout::status(totalCents);
std::cout << "totalCents=" << totalCents << std::endl;
std::cout << "status=" << size << std::endl;
return 0;
}
totalCents ← 750
13int main() {14 int totalCents→ 750 = 750; //@totalCents=1200, 2501516 std::string size = checkout::status(totalCents750);std::string status(int cents)
4namespace checkout {5std::string status(int cents750) {6 if (cents >= 1000) {7 return "large";8 }9 return "small";10}size ← small
16 std::string size→ small = checkout::status(totalCents750);1718 std::cout << "totalCents=" << totalCents750 << std::endl;19 std::cout << "status=" << sizesmall << std::endl;20 return 0;21}outputtotalCents=750 status=small
totalCents ← 250
13int main() {14 int totalCents→ 250 = 250;1516 std::string size = checkout::status(totalCents250);std::string status(int cents)
4namespace checkout {5std::string status(int cents250) {6 if (cents >= 1000) {7 return "large";8 }9 return "small";10}size ← small
16 std::string size→ small = checkout::status(totalCents250);1718 std::cout << "totalCents=" << totalCents250 << std::endl;19 std::cout << "status=" << sizesmall << std::endl;20 return 0;21}outputtotalCents=250 status=small
totalCents ← 1200
13int main() {14 int totalCents→ 1200 = 1200;1516 std::string size = checkout::status(totalCents1200);std::string status(int cents)
4namespace checkout {5std::string status(int cents1200) {6 if (cents >= 1000) {if (cents >= 1000)
5std::string status(int cents) {6 if (cents1200 >= 1000) {7 return "large";8 }size ← large
16 std::string size→ large = checkout::status(totalCents1200);1718 std::cout << "totalCents=" << totalCents1200 << std::endl;19 std::cout << "status=" << sizelarge << std::endl;20 return 0;21}outputtotalCents=1200 status=large