0% found this document useful (0 votes)
9 views

lect03_unit04_NameSpaces

Uploaded by

fosefan745
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

lect03_unit04_NameSpaces

Uploaded by

fosefan745
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Paper Code: CIC-211

Subject: Object-Oriented Programming Using C++

Unit04, Lecture 03:

Topics Covered: Namespaces


1. What is a Namespace?

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.

2. Syntax for Declaring a Namespace

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 {

int add(int a, int b) {

return a + b;

}
int main() {

int result = speedHOT::add(5, 3); // Accessing the function using the namespace

std::cout << "Sum: " << result << std::endl;

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;
}

4. using namespace Declaration

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy