Sunday, December 4, 2011

Computer Programming: Namespace in C++





// 應用Namespace避免命名衝突
// Avoid naming conflicts with the namespace

#include

using namespace std;

namespace doSomething {
int calculation (int x, int z){
int n;
n = x * z;
n += 1;
return n;
}
}

int main()
{
int a = 6;
int b = 3;
cout << doSomething::calculation(a, b) << endl;
return 0;
}

=========================
// 試比較:
// Let's compare:




#include

using namespace std;

namespace doSomething {
int calculation (int x, int z){
int n;
n = x * z;
n += 1;
return n;
}
}

namespace doSomethingElse {
int calculation (int x, int z){
int n;
n = x * z;
n += 200;
return n;
}
}

int main()
{
int a = 6;
int b = 3;
cout << doSomething::calculation(a, b) << endl;
cout << doSomethingElse::calculation(a, b) << endl;
return 0;
}

No comments:

RSS