Why we using namespace std in C++ Programming


The program is a C++ program that demonstrates the use of namespaces. Namespaces are used to organize code and avoid naming conflicts. Let's break down the program line by line:

  • #include: This line is a preprocessor directive that includes the iostream library, which provides the cout and cin objects used to output and input data to/from the console.
  • using namespace std; : This line declares that we want to use the std namespace. The std namespace is used by the C++ Standard Library, which includes functions and objects that are commonly used in C++ programs.
  • namespace name1{}: This line creates a namespace called name1.
  • string name="ram";: This line defines a string variable name with the value "ram" inside the name1 namespace.
  • int age=25; : This line defines an integer variable age with the value 25 inside the name1 namespace.
  • namespace name2{}: This line creates a namespace called name2.
  • string name="ram";: This line defines a string variable name with the value "ram" inside the name2 namespace.
  • using namespace name1; : This line declares that we want to use all the names in the name1 namespace. This means that we can use the name and age variables without having to specify the namespace.
  • int main(): This is the main function of the program.
  • {: The opening curly brace marks the beginning of the main function.
  • cout<<name; : This line uses the cout object to output the value of the name variable from the name1 namespace, which is "ram".
  • cout<<age; : This line uses the cout object to output the value of the age variable from the name1 namespace, which is 25.
  • return 0; : This line returns an integer value of 0 to the operating system, indicating that the program has executed successfully.
  • }: The closing curly brace marks the end of the main function.

Note that the using namespace name1; statement allows us to use the name and age variables without having to specify the name1 namespace, whereas the using std::cout; and using std::cin; statements are commented out and not used in the program.

Source Code

#include<iostream>
 
/*using std::cout;
using std::cin;
*/
using namespace std;
 
namespace name1{
     string name="ram";
     int age=25;
}
namespace name2{
     string name="ram";
}
using namespace name1;
int main()
{
    /*int a;
    cout<<"Enter The Value of A : ";
    cin>>a;
    cout<<"A Value : "<<a;*/
   cout<<name;
    cout<<age;
 
 
    return 0;
}
 

Output

ram25
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples