Namespaces in C++ | Set 4 (Overloading, and Exchange of Data in different Namespaces)
Last Updated :
17 May, 2021
Prerequisite:
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:
CPP
// 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;
}
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:
CPP
// 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;
}
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:
- In the above program, We have created three namespaces - test_space1, test_space2 and test_space3.
- 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.
- 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.
- 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.
Similar Reads
Why it is important to write "using namespace std" in C++ program? In this article, we will discuss the use of "using namespace std" in the C++ program.Need of Namespace in C++As the same name can't be given to multiple variables, functions, classes, etc. in the same scope. So, to overcome this situation, namespace is introduced.ExampleBelow is the C++ program illu
4 min read
What Are the Basic Rules and Idioms for Operator Overloading in C++? In C++, operator overloading is a form of compile-time polymorphism where an operator is redefined to provide a special meaning beyond its typical operation. It allows developers to use traditional operators with user-defined types (or classes). In this article, we will learn the basic rules and idi
3 min read
Overloads of the Different References in C++ This article focuses on function/method overloads by references, as well as the types of arguments that can be passed. Prerequisites: l-value references.r-value references.Move semantics - std::move(). Overview:l-value refers to a memory location that identifies an object. r-value refers to the data
12 min read
Overloading of function-call operator in C++ In this article, we will discuss the Overloading of the function-call operators in C++. The function call operator is denoted by â()â which is used to call function and pass parameters. It is overloaded by the instance of the class known as a function object.When the function call operator is overlo
3 min read
How to Create a Class with Private and Public Members in C++? In C++, the classes are blueprints for creating objects with specific properties and methods that provide a feature of access specifiers to the user through which they can control the access of the data members present in a class. In this article, we will learn how to create a class with private and
3 min read