What result will the code produce if the friend function is defined within 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 accurate choice is alternative (a). The friend function display defined in the namespace ns can retrieve the private member x from the class A defined in the identical namespace. Consequently, it will display the value 15 as the result. What will be the result of the given 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 accurate choice is alternative (b). In this instance, the print method serves as a friend function within Class A, holding a constant reference to the class's object. It then proceeds to output the value of the private member variable x, which is set at 42.
- Is the provided syntax accurate for defining the friend function within 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 accurate choice is alternative (b). In C++, a friend function is not considered a class member; however, it is granted special privileges to access private and protected members of the class.
- What will be the result of the following C++ code snippet?
#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:
Option (b) is the accurate choice. Friend functions in C++ are employed to enable functions to access both private and public members of the class, simplifying the management of interactions between functions and the class.