This section contains the C++ find output programs with their explanations on C++ Namespace (set 2).
Program 1:
#include <iostream>
#include <math.h>
using namespace std;
namespace NamespaceOuter {
int radius = 10;
namespace NamespaceInner {
int* ptr = &NamespaceOuter::radius;
}
}
int main()
{
float AreaOfCircle = 0.0F;
AreaOfCircle = 3.14 * pow(*NamespaceOuter::NamespaceInner::ptr, 2);
cout << "Area Of Circle: " << AreaOfCircle << endl;
return 0;
}
Program 2:
#include <iostream>
#include <math.h>
using namespace std;
namespace NamespaceOuter {
int radius = 10;
namespace NamespaceInner {
int* ptr = &NamespaceOuter::radius;
}
namespace NamespaceFun {
float calcuteArea()
{
float AreaOfCircle = 0.0F;
AreaOfCircle = 3.14 * pow(*NamespaceOuter::NamespaceInner::ptr, 2);
return AreaOfCircle;
}
}
}
int main()
{
cout << "Area Of Circle: " << NamespaceOuter::NamespaceFun::calcuteArea() << endl;
return 0;
}
Program 3:
#include <iostream>
#include <math.h>
using namespace std;
namespace NamespaceOuter {
int radius = 10;
namespace NamespaceInner {
int* ptr = &NamespaceOuter::radius;
}
namespace NamespaceFun {
float CalculateArea()
{
float AreaOfCircle = 0.0F;
AreaOfCircle = 3.14 * pow(*NamespaceOuter::NamespaceInner::ptr, 2);
return AreaOfCircle;
}
}
}
using namespace NamespaceOuter::NamespaceFun;
int main()
{
cout << "Area Of Circle: " << CalculateArea() << endl;
return 0;
}
Answer Program 1:
Output:
Explanation:
The above program will print "314" on the console screen. In the above program, we created nested namespace, the NamespaceOuter contains an integer variable radius and one another namespace NamespaceInner. The NamspaceInner contains an integer pointer ptr, pointer ptr initialized with the address of variable radius.
Now coming to the main() function, here we calculated area of a circle using below expression.
AreaOfCircle = 3.14*pow(*NamespaceOuter::NamespaceInner::ptr,2);
In the above statement, we used the pow() function to calculate power, which is present in math.h header file and we accessed the value of radius using the pointer, then print the final value of AreaOfCircle.
Answer Program 2:
Output:
Explanation:
The above program will print "314" on the console screen. In the above program, we created nested namespace, the NamespaceOuter contains an integer variable radius and two other namespaces NamespaceInner and NamespaceFun. The NamspaceInner contains an integer pointer ptr, pointer ptr initialized with the address of variable radius. And we defined function CalculateArea() inside the NamespaceFun.
Now coming to the main() function, here we called CalculateArea() function, and print the value returned by the function CalculateArea().
Answer Program 3:
Output:
Explanation:
The above program will print "314" on the console screen. In the above program, we created nested namespace, the NamespaceOuter contains an integer variable radius and two other namespaces NamespaceInner and NamespaceFun. The NamspaceInner contains an integer pointer ptr, pointer ptr initialized with the address of variable radius. And we defined function CalculateArea() inside the NamespaceFun.
Here we included NamespaceFun with the help of using namespace statement.
Now coming to the main() function, here we called CalculateArea() function, and print the value returned by the function CalculateArea().
need an explanation for this answer? contact us directly to get an explanation for this answer