Namespaces & Scope
Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 4 of 104.
Namespaces group related code together and prevent naming conflicts. The :: scope resolution operator accesses items inside a namespace.
Creating a namespace
namespace MathTools {
int add(int a, int b) {
return a + b;
}
}Accessing with scope resolution ::
int result = MathTools::add(5, 3); // result = 8Using directive to avoid typing the namespace each time
using namespace MathTools;
int result = add(5, 3); // Same as MathTools::add(5, 3)The std namespace
std::cout << "Hello" << std::endl;
// or
using namespace std;
cout << "Hello" << endl;Namespaces organize code and prevent naming collisions. The std namespace contains all C++ standard library features like cout, cin, string, and endl.
Challenge
MediumCreate functions inside the MathTools namespace:
add(int a, int b)— returns the summultiply(int a, int b)— returns the product
Declare them in MathTools.h and implement in MathTools.cpp.
Cheat sheet
Namespaces group related code together and prevent naming conflicts. The :: scope resolution operator accesses items inside a namespace.
Creating a namespace:
namespace MathTools {
int add(int a, int b) {
return a + b;
}
}Accessing with scope resolution :::
int result = MathTools::add(5, 3); // result = 8Using directive to avoid typing the namespace each time:
using namespace MathTools;
int result = add(5, 3); // Same as MathTools::add(5, 3)The std namespace contains all C++ standard library features like cout, cin, string, and endl:
std::cout << "Hello" << std::endl;
// or
using namespace std;
cout << "Hello" << endl;Try it yourself
#include <iostream>
#include "MathTools.h"
int main() {
int a, b;
std::cin >> a >> b;
std::cout << "Add: " << MathTools::add(a, b) << std::endl;
std::cout << "Multiply: " << MathTools::multiply(a, b) << std::endl;
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesC++ Build & CompilationHeader Files & Source FilesNamespaces & ScopeIntroduction to OOP in C++Classes vs ObjectsThe 'this' PointerMethods (Member Functions)Attributes (Data Members)Ctors & Dtors BasicsRecap - Simple Calculator4Class Properties
Instance vs Static MembersGetters and SettersConst Member FunctionsMutable KeywordStatic Methods and VariablesFriend Functions & ClassesRecap - Bank Account Manager7Inheritance
Basic InheritanceInheritance Access LevelsCtor & Dtor Call OrderMethod OverridingVirtual Functions & VTableMultiple InheritanceVirtual InheritanceRecap - Employee Hierarchy2Memory Management
Stack vs Heap MemoryPointers and ReferencesDynamic Memory (new/delete)Smart Pointers in C++RAII in C++Recap - Dynamic Array Manager5Encapsulation
Access Specifiers in C++Access Specifiers In DepthInformation HidingStruct vs ClassNested & Inner ClassesRecap - Student Records System8Polymorphism
Compile vs Runtime PolymorphFunction OverloadingVirtual Functions RevisitedPure Virtual FunctionsAbstract ClassesInterface Design in C++Dynamic Casting & RTTIRecap - Shape Calculator3Constructors & Destructors
Default ConstructorParameterized ConstructorCopy ConstructorMove ConstructorConstructor Init ListsDelegating ConstructorsDestructor Deep DiveRule of Three / Five / ZeroRecap - String Class6Operator Overloading
Intro to Operator OverloadArithmetic Operator OverloadComparison Operator OverloadStream OperatorsAssignment Operator Overload[] and () Operator OverloadType Conversion OperatorsRecap - Matrix Class9Templates
Function TemplatesClass TemplatesTemplate SpecializationVariadic TemplatesSFINAE & Type Traits BasicsRecap - Generic Container