+ Reply to Thread
Results 1 to 2 of 2

Thread: What is namespace in C++?

  1. #1

    What is namespace in C++?

    What is namespace in C++?

    What are the uses of namespace?

    How can I use them in my projects?

  2. #2
    a. Namespace can be used to group similar functionality.
    b. This way you have better hierarchy in the big project.
    c. Another advantage is that by using namespace you can avoid nae collision that might occur if you use lots of third party code and have similar kind of user defined types.

    Below example solves the name collision issue:

    Code:
    namespace CMyNamespace1
    {
    	class CStudent
    	{
    	public:
    		int GetRoleID()
    		{
    			return 42 ;
    		}
    	} ;
    } ;
    
    namespace CMyNamespace2
    {
    	class CStudent
    	{
    	public:
    		int GetRoleID()
    		{
    			return 12345 ;
    		}
    	} ;
    } ;
    
    void UseNameSapce()
    {
    	CMyNamespace1::CStudent objStudent1 ;
    	int nRoleID1 = objStudent1.GetRoleID() ;
    
    	CMyNamespace2::CStudent objStudent2 ;
    	int nRoleID2 = objStudent2.GetRoleID() ;
    }1
    Hope this helps.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts