-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathNamespace-II.cpp
48 lines (40 loc) · 1.09 KB
/
Namespace-II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstdlib>
/*
Namespace in C++ - II
If you redefine a function which is already in cstdlib, then you can no longer use it that original function inside cstdlib
or in any library.
How to tackle this ?
Namespace can help you in this.
*/
/*Commented for showing both usecase
int abs(int n) {
if(n < -128 || n > 128)
return 0;
if(n < 0)
return -n;
return n;
}
*/
namespace myNs {
int abs(int n) {
if(n < -128 || n > 128)
return 0;
if(n < 0)
return -n;
return n;
}
}
int main() {
std::cout << abs(-203) << " " << abs(-6) << " " << abs(77) << " " << abs(179) << std::endl; //(commented above)
/*
Output : 0 6 77 0
*/
//Once you add your abs, you cannot use the abs() from library! It is hidden and gone ! Dayummmmmmmm
//So, I will wrap my abs() inside a namespace and then use it.
std::cout << abs(-203) << " " << abs(-6) << " " << abs(77) << " " << abs(179) << std::endl; //(commented above)
/*
Output : 203 6 77 179
*/
return 0;
}