- What do you mean by friend function in C++?
- A function that belongs to a class.
- A function which can access both private members and public members of a class.
- A function which is defined inside the class.
- A function that cannot access the members of a class.
Explanation:
The correct answer is option (b). A friend function is a non-member function, which is not part of a class but has 'special rights' of having access to a class and modifying the class's private or protected members.
- In C++, How can you declare a function as Friend function?
- friend function functionName;
- void friend functionName;
- friend void functionName;
- void functionName friend;
Explanation:
The correct answer is option (c). Friend keyword in C++ is mainly utilized to declare a function as friend function in a class and there also should be a return type for the function following functions name.
- What are the implications that occur as a result of one class declaring another class as a friend?
- It can access all members of the declaring class in the friend class.
- A friend class can only use the public functions and data members of the class that declared it as a friend.
- The friend class is also allowed to access the members that are declared as private in the declaring class.
- The friend class cannot access any members of the declaring class.
Explanation:
The correct answer is option (a). If a class is declared as friend to some other, all the functions members of the declaring class will gain the access to the private and protected members.
- Which of the following statement is correct about the friend function in C++?
- Friend functions have the same access rights as member functions.
- Friend functions can only access public members of the class.
- Friend functions can access private and protected members of the class.
- Friend functions must be defined inside the class.
Explanation:
The correct answer is option (c). This is because C++ provides the use of the friend keyword to allow the functions that require access to private and protected members of the function into it.
- Which of the following statement is not characteristic of friend functions:
- They can access private and protected members of the class.
- Friend functions are defined inside the class.
- They are not members of the class.
- They are declared anywhere in the class.
Explanation:
The correct answer is option (b). They are not defined inside the class. Friend functions are only declared inside the class.
- What is the output for the below following C++ code?
#include<iostream>
using namespace std;
class A
{
int x;
public:
A() : x(10) {}
friend void show(A &a);
};
void show(A &a)
{
cout << a.x;
}
int main()
{
A obj;
show(obj);
return 0;
}
- Garbage value
- Compilation error
Explanation:
The correct answer is option (b). In the above code, the show function is the friend function and it is declared to have access for the private members x of class A. Therefore, it will print the value 10 as output.
- What will be the output of the below following C++ code?
#include<iostream>
using namespace std;
class A
{
int x;
public:
A(int val) : x(val) {}
friend class B;
};
class B
{
public:
void display(A &a) {
cout << a.x;
}
};
int main()
{
A obj(20);
B b;
b.display(obj);
return 0;
}
- Garbage value
- Compilation error
Explanation:
The correct answer is option (b). In the above example, class B is the friend function for class A, and then the member function display of class B can have access to the private members of class A.
- What is the output of the below following C++ code?
#include<iostream>
using namespace std;
class A
{
int x;
protected:
int y;
public:
A() : x(10), y(20) {}
friend void show(A &a);
};
void show(A &a)
{
cout<<a.x<<a.y;
}
int main()
{
A obj;
show(obj);
return 0;
}
- 10 0
- 0 20
- 10 20
- Compilation error
Explanation:
The correct answer is option (c). In the above example, the friend function shows can access both the private member x and the protected member y of class A.