This section contains the C++ find output programs with their explanations on C++ Namespace (set 1).
Program 1
Copy
#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;
}
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;
}
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;
}
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;
}
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;
}
Answer Program 1:
Output:
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.
Answer Program 2:
Output:
Explanation:
The above program will generate a syntax error. Because we included namespace before it's the definition then it will generate the errors.
Answer Program 3:
Output:
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;
Answer Program 4:
Output:
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.
Answer Program 5:
Output:
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);
need an explanation for this answer? contact us directly to get an explanation for this answer