In: Computer Science
Create a Namespaces.h header file containing a namespace declaration yourname. The declaration should include:
a method with the signature void message (string, ostream &) that prints a string to the output stream.
a method with the signature void message (double, ostream &) that prints a double to the output stream.
Create a testNamespaces.cpp that uses the yourname
namespace, and invokes the message() string method with a message
of your choice and cout as input parameters, and invokes the
message double method with a double of your choice and cout as
input parameter.
NOTE: You should not use the “using namespace” directive in your
header file.
Modify Namespaces.h to define PI as 3.14159, and MYNAME as your name, outside of the yourname namespace.
Modify your testNamespaces.cpp and add a new message method that has the signature void message(double, ostream &). This message should also print the message that it is the message function in testNamespaces.cpp.
In addition to the previous calls to message, call this new message function with PI and cout as input parameters, and call message with MYNAME and cout as input parameters.
Solution :
Given below is the code for the question.
In both the files, replace all occurences of the word YOURNAME with
your actual name (for e.g. John). Let me know if you have any
issues.
Please do rate the answer if it was helpful. Thank you
Namespaces.h
------
#ifndef Namespaces_h
#define Namespaces_h
#include <iostream>
#define PI 3.1415
#define MYNAME "YOURNAME"
namespace YOURNAME{
void message(std::string str, std::ostream &out){
out << str << std::endl;
}
void message(double val, std::ostream &out){
out << val << std::endl;
}
}
#endif /* Namespaces_h */
testNamespaces.cpp
--------------
#include <iostream>
#include "Namespaces.h"
using namespace std;
int main(){
YOURNAME::message("hello world", cout);
YOURNAME::message(PI, cout);
YOURNAME::message(MYNAME, cout);
return 0;
}
output
-----
hello world
3.1415
YOURNAME
Thank you...!