- What will be the output of the following code if the friend function is declared in a namespace?
#include<iostream>
namespace ns
{
class A
{
int x;
public:
A(int val) : x(val) {}
friend void show(const A &a);
};
void show(const A &a)
{
std::cout << a.x;
}
}
int main()
{
ns::A obj(15);
ns::show(obj);
return 0;
}
- Compilation error
- Runtime error
Explanation:
The correct answer is option (a). The friend function shows declared in the namespace ns that can access the private member x of the class A declared in the same namespace. It will print value 15 as an output.
- What is the output of the below following C++ code?
#include<iostream>
using namespace std;
class A
{
int x;
public:
A(int val) : x(val) {}
friend void print(const A &a);
};
void print(const A &a)
{
cout << a.x;
}
int main()
{
A obj(42);
print(obj);
return 0;
}
- error
- Runtime error
Explanation:
The correct answer is option (b). In this example, the print function is a friend function that has a constant reference to the object of class A and prints the value of private member variable x that equals to 42.
- Is the syntax given correctly for declaring the friend function in Class A?
class A
{
public:
void friend display();
};
- Correct
- Incorrect
- Both
- None
Explanation:
The correct answer is option(b). In the right syntax there will be no keyword before friend , it must be like friend void display.
- Which of the following statement best describes the friend function's relationship to the class?
- Member function.
- Non-member function with special access.
- Protected member function.
- Private member function.
Explanation:
The correct answer is option (b). Friend function in C++ is not a member of the class but it has special access to the private and protected members of the class.
- What is the output of the below following C++ code?
#include<iostream>
using namespace std;
class A
{
int x;
static int y;
public:
A(int val) : x(val) {}
friend void display(const A &a);
};
int A::y = 100;
void display(const A &a)
{
cout << a.x << " " << A::y;
}
int main()
{
A obj(50);
display(obj);
return 0;
}
- 50 0
- 50 100
- 0 100
- Compilation error
Explanation:
The correct answer is option (b). The friend function display can access the private member of class A, known as x, and the static member y of class A. After that, it displays the values of the member variables.
- Which of the following statement is correct of the friend classes?
- A friend class can only access the public data of members of another class.
- A friend class can access private and protected members of another class.
- A friend class cannot access private members of another class.
- Friend classes do not exist in C++.
Explanation:
The correct answer is option (b). When a class is declared as a friend of the other class, it can access the private and protected members of the class.
- Which of the following statements about friend functions is incorrect?
- Friend functions are not members of the class.
- Friend functions can access private and protected members of the class.
- Friend functions are declared inside the class but defined outside.
- Friend functions must be defined within the class.
Explanation:
The correct answer is option (c). These are functions declared within the friend class even though they are defined in another class, but they should be defined outside the class.
- What is the purpose of using the friend keyword while declaring a function in C++?
- To allow a function to be part of the class.
- To allow function of one class access private and protected members of another class.
- To improve code readability.
- To restrict access to public members of the class.
Explanation:
The correct answer is option (b).Friend functions in C++ are used to allow functions for accessing both private and public members of the class so that it can be easy to control interaction between functions and class.