Copyright © Programs ++
Design by Dzignine
Friday, 4 May 2012

Namespace example in C++

// Program to demonstrate the use of namespaces

#include <iostream>

namespace other // defines a namesapce
          int sum(int,int); // function prototype

// function sum() that belong to the other namespace which returns the sum of two //integers
int other :: sum(int x,int y)
{     return x+y;
/* another function sum() that does not belong to the namespace 'other' and returns the
   difference of two integers passed as arguments */
int sum(int x,int y)
{   return x-y;
int main()
    int a,b;
    // prompts the user to enter two integer values a and b
    std::cout<<" Enter the value of a : ";
    std::cout<<" Enter the value of b : ";
    // calls the function declared under the 'other' namespace
    // notice the other:: is used to call this function
    std::cout<<"\n Sum() function of 'other' namespace : "<<other::sum(a,b);
    // normal sum() function that actually computes the difference of a and b
    std::cout<<"\n Normal Sum() function : "<<sum(a,b);
    return 0;
}// end of main
------ OUTPUT ------


Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.


Post a Comment

Comment Here....