CPP/Namespaces

From ProgrammingExamples
< CPP
Jump to: navigation, search

Namespaces.cpp

#include <iostream>
using namespace std;
 
namespace Math{
	bool isOdd(const int n){ return (n & 1) == 1; }
	bool isEven(const int n){ return !isOdd(n); }
	static const float PI = 3.1415f;
	static const float DEGTORAD = PI/180.0f;
}
 
int main(){
	//use the scope operator, :: , to access a function or variable;
	for(int i = 1; i != 10; ++i){
		if( Math::isOdd(i) ){
			cout << i << " is odd " << endl;
		}
	}
 
	//use the using declaration
	{
		using Math::isEven; //isEven is only in scope after this semicolon
		if( isEven(1010112) ) cout << "\nIts even...\n";
	} //and is invalid after this closing brackets
 
	{
		//use everthing from the Math namespace 
		using namespace Math;
		cout << PI << endl; 
	} 
 
	return 0;
 
}