C++ Programming - Objects and Classes - Discussion
Discussion Forum : Objects and Classes - General Questions (Q.No. 2)
2.
Which of the following statements is incorrect?
Discussion:
21 comments Page 1 of 3.
Durgadevi said:
1 decade ago
Because friend keyword does not declared in main() function only we use under the class.
Sweety said:
1 decade ago
friend keyword can be used on main() also i.e we can make main() as friend function.
Prem said:
1 decade ago
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.
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:
1 decade ago
Please explain why friend keyword is not used with main()?
Chaitanya said:
1 decade ago
Because main is entry point function.
Ranjeet Avghad said:
1 decade ago
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:
1 decade ago
Friend keyword can be used on main() ? please explain I didn't get ?
Thivakar R said:
1 decade ago
Friend function only initialize in-between main and class.
Sridharan said:
1 decade ago
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:
1 decade ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers