C++ Programming - Objects and Classes - Discussion

Discussion :: Objects and Classes - General Questions (Q.No.2)

2. 

Which of the following statements is incorrect?

[A]. Friend keyword can be used in the class to allow access to another class.
[B]. Friend keyword can be used for a function in the public section of a class.
[C]. Friend keyword can be used for a function in the private section of a class.
[D]. Friend keyword can be used on main().

Answer: Option D

Explanation:

No answer description available for this question.

Durgadevi said: (Jul 2, 2012)  
Because friend keyword does not declared in main() function only we use under the class.

Sweety said: (Jan 17, 2013)  
friend keyword can be used on main() also i.e we can make main() as friend function.

Prem said: (Apr 1, 2013)  
Friend keyword is written inside one class(class a) to inform the same class(class a) that another class(class b) can access the private data of the other(class a).

eg: class a class b
{ {
private : int e; private : int f;
public : int x;
friend class b; }
}

In the above example, class b can access the private data of the class a.

Seema said: (Sep 26, 2013)  
Please explain why friend keyword is not used with main()?

Chaitanya said: (Jan 16, 2014)  
Because main is entry point function.

Ranjeet Avghad said: (Jun 17, 2014)  
Yes we can use friend keyword on main by defining main as member function of a class. So the data members of even though it is private can be accessible in main function. And program will execute properly.

Ananya said: (Jul 30, 2014)  
Friend keyword can be used on main() ? please explain I didn't get ?

Thivakar R said: (Sep 19, 2014)  
Friend function only initialize in-between main and class.

Sridharan said: (Mar 13, 2015)  
Friend functions can be applied only to functions which can access private members of a class. A non member function of a class can access private members using friend function.

Keerthana said: (May 8, 2015)  
Friend function is only declared in the class. But why can't it be defined in main function, because we are only accessing the private data. So it can be access from anywhere in the program.

Neha Rani said: (Aug 13, 2015)  
It is not the always the case, according to this link on stackoverflow:

It is required when a pointer of base class is cast to have derivedClas type, when we delete this pointer it is required to call derived class's destructor.

Himanshu Budhalakoti said: (Aug 23, 2015)  
A friend function is used to access private data of its friend class into out of class therefore the friend function can not be declared in main () function but it can be accessed in main class.

Akhila Sivan said: (Sep 6, 2015)  
A friend function that is a "friend" of a given class is allowed access to private and protected data in that class that it would not normally be able to as if the data was public.

Normally, a function that is defined outside of a class cannot access such information. Friend function can't declare in main() function.

Sudhankar Sharma said: (Sep 22, 2015)  
Because It is often useful for one class to see the private variables of another class, even though these variables should probably not be made part of the public interface that the class supports.

For instance, if you were writing a binary tree, you might want to use a Node class that has private data, but it would still be convenient for the functions that actually combine nodes together to be able to access the data directly without having to work through the Node interface.

At times, it may not even be appropriate for an accessors function to ever give even indirect access to the data.

Rohit Patil said: (Sep 27, 2015)  
Wrong answer.

Friend is only used in class not in main.

Parth Khandelwal said: (Feb 9, 2016)  
Friend function is used to access private data members of one class to another.

Yogesh said: (Feb 22, 2016)  
Friend function is not a member function of class, friend function is declared inside the class but it is defined outside the class.

Harmandeep Kaur said: (Oct 4, 2016)  
I thought the Friend function is used to access private members of any class.

Digambar Jadhav said: (Mar 14, 2018)  
@ALL.

The main is non-member fuction of a class so we cant use friend function in main().

Yes, we can access private data member of a class by using friend keyword in that class with its class name.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.