If a friend function outside the class declares itself friend of the class, this may violate the concept of ________.
AEncapsulation and data accessing
BEncapsulation and data hiding
CFriend member functions of the class
DInterface of the class
Friendship must be granted by the class; a function granting itself access would break encapsulation and data hiding.
2.
★ Final term 2017 (paper 1)
The prototype of friend function is written ________.
ABefore the class definition
BAs private member function
CAnywhere inside the class definition
DIn the main function
A friend declaration can appear anywhere in the class; access specifiers do not affect it.
3.
★ Final term 2017 (paper 2)
Friend class of the other class gets ______ access to ______.
APartial, data member and member functions.
BRead only, data member and member functions.
CComplete, data members only.
DComplete, all data members and member functions.
A friend class can access all private and protected members of the class that befriends it.
4.
★ Final term solved MCQs (Moaaz & Asad)
Which of the following statement is best regarding declaration of friend function?
AFriend function must be declared after public keyword.
BFriend function must be declared after private keyword.
CFriend function must be declared at the top within class definition.
DIt can be declared anywhere in class as these are not affected by the public and private keywords.
Access specifiers do not affect friend declarations, so they may appear anywhere in the class.
5.
★ Final term solved MCQs (Moaaz & Asad)
Like member functions, ______ can also access the private data members of a class.
ANon-member functions
BFriend functions
CAny function outside class
DNone of the given options
A friend function is granted access to the class's private members.
6.
★ Final term solved MCQs (Moaaz & Asad)
Friend function of a class is ______________ .
AMember function
BNon-member function
CPrivate function
DPublic function
A friend function is not a member of the class even though it has access to it.
7.
★ Final term solved MCQs (Moaaz & Asad)
Function implementation of friend function must be defined outside the class.
ATrue
BFalse
A friend function can also be defined inline inside the class body.
8.
★ Final term solved MCQs (Moaaz & Asad)
The prototype of friend functions must be written ____ the class and its definition must be written ____
Ainside, inside the class
Binside, outside the class
Coutside, inside the class
Doutside, outside the class
The friend declaration goes inside the class and the function is normally defined outside it.
9.
★ Final term solved MCQs (Moaaz & Asad)
A function can declare itself a friend of a class.
ATrue
BFalse
Friendship is granted by the class, not claimed by the function.
10.
★ Final term Fall 2005
A friend function of a class has access
ATo all data member and functions of the class
BOnly to other friend functions of the class
COnly to private data of the class
DOnly to public data of the class
A friend can use all members of the class, including private and protected ones.
11.
★ Final term Spring 2005
A friend function
Amust be having a prototype with no arguments
Bmust be invoked by the class that declares it a friend
Cmust be invoked by an object of the class that declares it a friend
Dcan access the private data of the class that declares it a friend
A friend is not a member but is allowed to use the class's private data.
No MCQs match this filter.Is filter mein koi MCQ nahi.
1.
★ Final term 2017 (paper 3)Lecture 292 marks
Friendship in classes/functions is granted or taken? How it is granted or taken?
Show answerJawab dekhein
Friendship is granted, not taken. A class grants it by declaring the function or class as a friend inside its own definition with the friend keyword, e.g. friend void show(Account &); or friend class Bank;. An outside function or class cannot make itself a friend.
2.
★ Final term subjective (Moaaz)Lecture 293 marks
What is limitation of the friendship between classes?
Show answerJawab dekhein
Friendship is granted, not taken, and it is limited: (1) it is one-way - if class A declares class B its friend, B can access A's private members but A cannot access B's; (2) it is not transitive - a friend of a friend is not a friend; (3) it is not inherited - derived classes of a friend are not friends. Friendship also weakens encapsulation, so it should be used sparingly.
1.
★ Final term subjective (Moaaz)Lecture 295 marks
Write the general syntax of a class that has one function as a friend of a class along with definition of friend function.
Show answerJawab dekhein
class ClassName
{
private:
int a, b;
public:
ClassName(int x, int y) { a = x; b = y; }
friend int compute(ClassName obj); // friend declaration
};
// definition: not a member, so no ClassName:: and no friend keyword
int compute(ClassName obj)
{
return obj.a + obj.b; // may access private members
}
2.
★ Final term Fall 2005Lecture 295 marks
What is a friend function? Explain with example?
Show answerJawab dekhein
A friend function is a non-member function that a class declares with the keyword friend inside its body. It is not called through an object (no this pointer), but it is allowed to access the private and protected members of that class. Friendship is granted by the class, it is not inherited and not mutual.
#include <iostream>
using namespace std;
class Box {
int width; // private
public:
Box(int w) { width = w; }
friend void showWidth(Box b); // friend declaration
};
void showWidth(Box b) { // ordinary function, not a member
cout << "Width: " << b.width << endl; // can read private data
}
int main() {
Box b(5);
showWidth(b); // called like a normal function
return 0;
}