Users Online
· Guests Online: 49
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C++ Program to Demonstrate the use of Namespaces
C++ Program to Demonstrate the use of Namespaces
This C++ Program which demonstrates the use of namespaces. The program creates two namespaces with a variable of same name defined in each of them but with different data type. The functions functionOne( ) and functionTwo( ) take an integer as an input and save in the variable “val”. The values of “val” are printed in the context of both namespaces.
Here is source code of the C++ program which demonstrates the use of namespaces. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
-
/*
-
* C++ Program to Demonstrate the use of Namespaces
-
*/
-
#include <iostream>
-
-
namespace One
-
{
-
int val;
-
}
-
namespace Two
-
{
-
char val;
-
}
-
-
void functionOne(void)
-
{
-
using namespace One;
-
/* Variable val is defined in context of namespace One */
-
std::cout << "Enter the value of val : ";
-
std::cin >> val;
-
std::cout << "functionOne()" << endl
-
<< "Value of val : "
-
<< val << endl;
-
}
-
-
void functionTwo(void)
-
{
-
using namespace Two;
-
/* Variable val is defined in context of namespace Two */
-
std::cout << "Enter the value of val : ";
-
std::cin >> val;
-
std::cout << "functionTwo()" << endl
-
<< "Value of val : "
-
<< val << endl;
-
}
-
-
int main()
-
{
-
functionOne();
-
functionTwo();
-
return 0;
-
}
$ g++ main.cpp $ ./a.out Enter the value of val : 5 functionOne() Value of val : 5 Enter the value of val : 5 functionTwo() Value of val : ♣
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.