Skip to content

Latest commit

 

History

History
64 lines (54 loc) · 1.13 KB

Namespace.md

File metadata and controls

64 lines (54 loc) · 1.13 KB

A namespace in C++ is a way to group related identifiers (such as functions, classes, and variables) under a unique name, to avoid naming conflicts and to make it easier to organize and maintain code.

using namespace

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, world!" << endl;
    return 0;
}

using part of a namespace

#include <iostream>

using std::cout;
using std::endl;

int main() {
    cout << "Hello, world!" << endl;
    return 0;
}

Define a namespace

// mymath.h

namespace mymath {
    class Calculator {
    public:
        int add(int a, int b);
        int subtract(int a, int b);
    };
}
// mymath.cpp

#include "mymath.h"

namespace mymath {
    int Calculator::add(int a, int b) {
        return a + b;
    }

    int Calculator::subtract(int a, int b) {
        return a - b;
    }
}
// main.cpp

#include <iostream>
#include "mymath.h"

int main() {
    mymath::Calculator calculator;
    std::cout << "3 + 4 = " << calculator.add(3, 4) << std::endl;
    std::cout << "5 - 2 = " << calculator.subtract(5, 2) << std::endl;
    return 0;
}