lect03_unit04_NameSpaces
lect03_unit04_NameSpaces
A namespace is a declarative region that provides a scope to the identifiers (names of types,
functions, variables, etc.) inside it. It is used to organize code and avoid name collisions that might
occur especially in large codebases or when multiple libraries are used.
Namespaces help resolve ambiguity by explicitly specifying the context (namespace) in which an
identifier should be resolved.
namespace speedHOT {
// Declarations or definitions inside the namespace
int var;
void function1();
}
Here, the identifiers var and function belong to the namespace speedHOT. They can be accessed
using the scope resolution operator (::), like so: speedHOT::var or speedHOT::function1().
Example:
#include <iostream>
namespace speedHOT {
return a + b;
}
int main() {
int result = speedHOT::add(5, 3); // Accessing the function using the namespace
return 0;
3. Nested Namespace
namespace speed {
namespace HOT {
int multiply(int x, int y) {
return x * y;
}
}
}
int main() {
int result = speed::HOT::multiply(5, 4); // Accessing nested namespace function
std::cout << "Product: " << result << std::endl;
return 0;
}
If you want to access all members of a namespace without needing to qualify each name, you can
use the using namespace directive:
using namespace speedHOT; // All identifiers from Example can now be accessed
without the scope operator
int main() {
std::cout << add(5,6); // Equivalent to Example::value
return 0;
}
References:-
Online
1. Tutorialspoint OOP Tutorial: Comprehensive tutorials on OOP concepts with examples. Link
2. GeeksforGeeks OOP Concepts: Articles and examples on OOP principles and applications. Link
3. https://canvas.rku.ac.in/courses/3333/pages/specifying-a-class
Book
1. "Object-Oriented Analysis and Design with Applications" by Grady Booch: Classic book covering
fundamental OOP concepts and real-world examples.
2. "Object-Oriented Programming in C++" by Robert Lafore: Detailed guide to OOP in C++, explaining
concepts with a practical example