-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathNamespace-I.cpp
38 lines (31 loc) · 859 Bytes
/
Namespace-I.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
#include <iostream>
using namespace std;
/*
Namespace in C++ - I
-It's a scope for the identifiers(functions, variables etc)
-It's use to organise the code into logical groups
-Prevent name collisions that can occur especially when your code base includes multiple libraries.
*/
namespace MyNameSpace {
int myData;
void myFunction() {
cout << "MyNamSpace myFunction" << endl;
}
class MyClass {
int data;
public:
explicit MyClass(int d) : data(d) {}
void display() {
cout << "MyClass data = " << data << endl;
}
};
}
int main() {
MyNameSpace::myData = 10;
cout << "MyNameSpace::myData = " << MyNameSpace::myData << endl;
myFunction(); //Error!!!!!!!!!!
MyNameSpace::myFunction();
MyNameSpace::MyClass obj(25);
obj.display();
return 0;
}