This example shows how to use namespaces to organize code and avoid naming conflicts.
cppCopy code#include <iostream>
namespace Math {
int add(int a, int b) {
return a + b;
}
}
namespace StringUtils {
void printHello() {
std::cout << "Hello, World!" << std::endl;
}
}
int main() {
std::cout << "Sum: " << Math::add(3, 4) << std::endl; // Output: Sum: 7
StringUtils::printHello(); // Output: Hello, World!
return 0;
}
Leave a Reply