C++ Namespace | Find output programs | Set 2

This section contains the C++ find output programs with their explanations on C++ Namespace (set 2).
Submitted by Nidhi, on September 21, 2020

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;
}

Output:

Area Of Circle: 314

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.

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;
}

Output:

Area Of Circle: 314

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().

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;
}

Output:

Area Of Circle: 314

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().






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.