C++ is using function for the minimum unit unlike Java and C# which means main function can be used without class. Sometime, this function name or class name can have limitation to use. For example, if one project is requiring to access Oracle and Mysql libraries, there is a chance to have same class name. In that case, we can use namespace. It's same concept of Java's package or C#'s namespace.
'using' can be declared as golobal for a namespace. It's same as Java's 'import' and C#'s 'using'. C++ has 'include' additionally. 'include' is a preprocessor to include a file, but 'using' is a keword to use a namespace without declaring name of object everytime.
We can use 'typedef to make '예약어' keyword from C++. So without 'using namespace', we can set the namespace using 'typedef'.
#include <stdio.h>
#include <iostream>
using namespace std;
// namespace A area
namespace A
{
// Test class
class Test
{
public:
void print()
{
cout << " namespace - A : " << " Test::print() " << endl;
}
};
}
// namespace B Area
namespace B
{
class Test
{
public:
void print()
{
cout << " namespace - B : " << " Test::print() " << endl;
}
};
}
typedef A::Test Test;
int main()
{
Test test;
//print() in Test class of 'A namespae'
test.print();
return 0;
}
'Prog&Algol' 카테고리의 다른 글
C++ | auto & decltype for type deduction (0) | 2022.03.20 |
---|---|
C++ | 초기화 리스트/initialize_list | after c++11 (0) | 2022.03.20 |
2019 Winter Kakao Internship - Hotel room (0) | 2020.04.14 |
Day-03. DP와 함께 계단 오르기 (0) | 2019.12.14 |
Day-02. 십년 만에 코딩 two sum (1) | 2019.12.09 |