Defining a namespace and include class - C++ Statement

C++ examples for Statement:namespace

Description

Defining a namespace and include class

Demo Code

#include <cstdlib> 
 #include <iostream> 

 using namespace std; 

 // Beginning of first namespace. 
 namespace anamespace 
 { //  w ww. jav a2 s  .  c o  m

 class point 
 { 
 public : 
     point() 
     { 
         x = y = 0; 
     } 

     void SetX(int xValue) 
     { 
         x = xValue; 
     } 

     int GetX(void) 
     { 
         return (x ); 
     } 

     void SetY(int yValue) 
     { 
        y = yValue; 
    } 
 
    int GetY(void) 
    { 
        return (y ); 
    } 
 
private: 
    int x ,y ; 
}; 
// End of first namespace. 
}; 
// Beginning of second namespace. 
namespace anothernamespace 
{ 
class point 
{ 
public : 
    point() 
    { 
        x = y = 0; 
    } 

    void SetX(int xValue) 
    { 
        x = xValue; 
    } 

    int GetX(void) 
    { 
        return (x ); 
    } 
 
    void SetY(int yValue) 
    { 
        y = yValue; 
    } 

    int GetY(void) 
    { 
        return (y ); 
    } 
    void Reset() 
    { 
        x = y = 0; 
    } 
private: 
    int x ,y ; 
}; 
// End of second namespace. 
}; 



 int main(int argc, char *argv []) 
 { 
     anamespace::point rightHere; 

     rightHere.SetX(10); 
     rightHere.SetY(20); 

     cout << "(x ,y )=(" << rightHere.GetX(); 
     cout << "," << rightHere.GetY() << ")"; 
     cout << endl; 

     anothernamespace::point rightThere; 
     rightThere.SetX(20); 
     rightThere.SetY(10); 

     cout << "(x ,y )=(" << rightThere.GetX(); 
     cout << "," << rightThere.GetY() << ")"; 
     cout << endl; 

     rightThere.Reset(); 

     cout << "(x ,y )=(" << rightThere.GetX(); 
     cout << "," << rightThere.GetY() << ")"; 
     cout << endl; 

   
     return (0); 
 }

Result


Related Tutorials