Namespaces in C++ | Set 4 (Overloading, and Exchange of Data in different Namespaces)

Prerequiste:

In this article, we will discuss the concept of data sharing between different namespaces, how to access and Overload standard operators to work for user-defined classes inside user-defined namespaces, and their implementation.
In C++, We can create classes inside different namespaces and the scope of those classes is limited to the namespace in which they are created. Hence we must access those classes using the scope resolution operator (::).

Below is the implementation to understand the declaration of classes inside a namespace and initialization of objects in the program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the basics
// of classes inside namespaces
#include <iostream>
#include <string>
  
// Declaration of namespace
namespace GeeksforGeeks {
  
// Output string
std::string out = "This is not from (GeeksforGeeks::string) class!";
  
// Class inside GeeksforGeeks namespace
class string {
private:
    std::string s = "Hello! ";
  
public:
    // Make string
    string(const std::string& str)
    {
        s += str;
    }
  
    // Function to get string
    std::string get_str()
    {
        return s;
    }
};
};
  
// Driver Code
int main()
{
    std::cout << GeeksforGeeks::out
              << std::endl;
  
    // Create an object of string class
    // of Test namespace
    GeeksforGeeks::string str("From namespace Test!");
  
    // Print the string
    std::cout << str.get_str()
              << std::endl;
    return 0;
}

chevron_right


Output:

This is not from (GeeksforGeeks::string) class!
Hello! From namespace Test!

In the above program, we can clearly see that scope of the class ‘string’ inside GeeksforGeeks namespace was only inside the namespace and the class was initialized by the parameterized constructor which also changed the private string ‘s’ of Class string inside the namespace Test.



The below program is the illustration of the Concepts of Overloading of Operators with different namespace Class Objects as Parameters and Accessing and Exchange of data from one namespace to another namespace:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to show overloading and
// accessing from different namespaces
#include <iostream>
#include <string>
  
// Declare namespace test_space1
namespace test_space1 {
  
const std::string const_prefix = "(test_space1::string) ";
  
// String class
class string {
private:
    std::string str = "";
  
    // Private default constructor
    string();
  
public:
    // Public parameterized constructor
    string(const std::string& s)
        : str(const_prefix + s)
    {
    }
  
    // Get string function
    std::string get_str() const
    {
        return str;
    }
};
};
  
// Declare namespace test_space2
namespace test_space2 {
  
const std::string const_prefix = "(test_space2::string) ";
class string {
private:
    std::string str = "";
    const std::string check_scope = "test_space2!";
    string();
  
public:
    // Public parameterized constructor
    string(const std::string& s)
        : str(const_prefix + s)
    {
    }
  
    std::string get_str() const
    {
        return str;
    }
    std::string getScopestr() const
    {
        return check_scope;
    }
};
};
  
// Declare namespace test_space3
namespace test_space3 {
  
// Object from another namespace
// (test_space2 in this case!)
const std::string const_prefix = "(test_space3::string) from both nmspaces test_space3 & ";
  
// Class inside namespace test_space3
class string {
    std::string str = "";
    string();
  
public:
    string(const test_space2::string& s)
        : str(const_prefix + s.getScopestr())
    {
    }
  
    // Access function from test_space2
    // and adding a private string of
    // test_space2:: string to str of
    // test_space3
    std::string get_str() const
    {
        return str;
    }
};
};
  
// Overloading << operator for test_space1
std::ostream& operator<<(std::ostream& os,
                         const test_space1::string& s1)
{
    os << s1.get_str();
    return os;
}
  
// Overloading << operator for test_space2
std::ostream& operator<<(std::ostream& os,
                         const test_space2::string& s2)
{
    os << s2.get_str();
    return os;
}
  
// Overloading << operator for test_space3
std::ostream& operator<<(std::ostream& os,
                         const test_space3::string& s3)
{
    os << s3.get_str();
    return os;
}
  
// Driver Code
int main()
{
  
    // String str
    const std::string str("This is a standard string");
  
    // Print the string str
    std::cout << str << std::endl;
  
    const std::string sample1("This is a test_space1 namespace string");
  
    // Declare a test_space1 namespace
    // string class object
    const test_space1::string s2(sample1);
    std::cout << s2 << std::endl;
  
    // Print test_space1 string
    const std::string sample2("This is a test_space2 namespace string");
  
    // std string
    test_space2::string s3(sample2);
  
    // Declare a test_space2 namespace
    // string class object
    std::cout << s3 << std::endl;
  
    // Print test_space2 string
    test_space3::string s4(s3);
  
    // Print string s4
    std::cout << s4 << std::endl;
    return 0;
}

chevron_right


Output:

This is a standard string
(test_space1::string) This is a test_space1 namespace string
(test_space2::string) This is a test_space2 namespace string
(test_space3::string) Accessing from both namespaces test_space3 and test_space2!

Explanation:

  1. In the above program, We have created three namespaces – test_space1, test_space2 and test_space3.
  2. All of these namespaces have a common class string in them. We have created their respective objects s2, s3 and s4 which take different parameters during initialization.
  3. The namespace test_space3 is used to access class members from inside the string class of namespace test_space2 and hence, test_space3::string’s constructor is different from the other two classes constructors.
  4. So, We are able to access the data from test_space2 and use it in test_space3. This shows the Accessibility and Exchange of Data between different Namespaces and their Classes.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.