C++ Namespace | Find output programs | Set 1

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

Program 1:

#include <iostream>
using namespace std;

namespace MyNameSpace {
    void Fun()
    {
        cout << "Function from namespace" << endl;
    }
}
using namespace MyNameSpace;

int main()
{
    void (*fptr)() = Fun;

    fptr();

    return 0;
}

Output:

Function from namespace

Explanation:

The above code will print "Function from namespace" on the console screen. In the above program, we created namespace MyNameSpace that contains function Fun(). We included namespace MyNameSpace with the help of using statement to use function Fun().

Now coming to the main() function, here we created function pointer, that initialized with function name Fun. Then finally we call the function using a function pointer.

Program 2:

#include <iostream>
using namespace std;

using namespace MyNameSpace;

namespace MyNameSpace {
    void Fun()
    {
        cout << "Function from namespace" << endl;
    }
}

int main()
{
    void (*fptr)() = Fun;

    fptr();

    return 0;
}

Output:

main.cpp:4:17: error: ‘MyNameSpace’ is not a namespace-name
 using namespace MyNameSpace;
                 ^~~~~~~~~~~
main.cpp:4:28: error: expected namespace-name before ‘;’ token
 using namespace MyNameSpace;
                            ^
main.cpp: In function ‘int main()’:
main.cpp:15:22: error: ‘Fun’ was not declared in this scope
     void (*fptr)() = Fun;
                      ^~~
main.cpp:15:22: note: suggested alternative:
main.cpp:7:10: note:   ‘MyNameSpace::Fun’
     void Fun()
          ^~~

Explanation:

The above program will generate a syntax error. Because we included namespace before it's the definition then it will generate the errors.

Program 3:

#include <iostream>
using namespace std;

namespace MyNameSpace {
    void Fun()
    {
        cout << "Function from namespace" << endl;
    }
}

int main()
{
    void (*fptr)() = MyNameSpace.Fun;

    fptr();

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:13:33: error: expected primary-expression before ‘.’ token
     void (*fptr)() = MyNameSpace.Fun;
                                 ^

Explanation:

The above program will generate compilation error. Because here we tried to access member of namespace MyNameSpace in the below statement:

void (*fptr)()= MyNameSpace.Fun;

To access function Fun() we need to use scope resolution operator "::" instead of membership operator '.", which is given below:

void (*fptr)()= MyNameSpace::Fun;

Program 4:

#include <iostream>
#include <math.h>
using namespace std;

namespace MyNameSpace {
    int radius = 10;
}

int main()
{
    float AreaOfCircle = 0.0F;

    AreaOfCircle = 3.14 * pow(MyNameSpace::radius, 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 a namespace MyNameSpace that contains an integer variable radius.

Now coming to the main() function, here we calculated area of a circle using below expression.

AreaOfCircle = 3.14*pow(MyNameSpace::radius,2);

In the above statement, we used the pow() function to calculate power, which is present in math.h header file and we accessed radius using scope resolution operator from the namespace. Then print the calculated value on the console screen.

Program 5:

#include <iostream>
#include <math.h>
using namespace std;

namespace MyNameSpace {
    int radius = 10;
    int* ptr = &radius;
}

int main()
{
    float AreaOfCircle = 0.0F;

    AreaOfCircle = 3.14 * pow(MyNameSpace::*ptr, 2);

    cout << "Area Of Circle: " << AreaOfCircle << endl;

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:14:44: error: expected unqualified-id before ‘*’ token
     AreaOfCircle = 3.14 * pow(MyNameSpace::*ptr, 2);
                                            ^
main.cpp:14:45: error: ‘ptr’ was not declared in this scope
     AreaOfCircle = 3.14 * pow(MyNameSpace::*ptr, 2);
                                             ^~~
main.cpp:14:45: note: suggested alternative:
main.cpp:7:10: note:   ‘MyNameSpace::ptr’
     int* ptr = &radius;
          ^~~

Explanation:

The above code will generate an error. Because we did not access the pointer variable properly from the namespace in the below statement.

AreaOfCircle = 3.14*pow(MyNameSpace::*ptr,2);

The correct way to access the pointer variable from a namespace that is given below.

AreaOfCircle = 3.14*pow(*(MyNameSpace::ptr),2);




Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.