CodesJava

Easy learning with example program codes

CPP Namespaces


A namespace in CPP is used to define a scope in order to differentiate similar functions, classes, variables etc. to define the context in which names are defined for the same name available in different libraries.

Syntax: Defining Namespace

namespace namespace_name

{
// code declarations
}

 

Syntax: Calling the defined name

namespace_name  :: code;  // code could be variable or function.

 

Example:

#include <iostream>  
using namespace std;  
namespace str1 {    
void display() {   
cout<<"I am the First String!"<<endl;     	 
}    
}    
namespace str2  {    
void display() {   
cout<<"I am the Second String!"<<endl;   
}    
}   
int main()  
{  
str1::display();  
str2::display();  
return 0;  
}

Output

I am the First String!                                                                    
I am the Second String!
Posted in CPP   
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki