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.
Malayor said:
3 years ago
The friend keyword cannot be used because it is a correct keyword c++ can code correct keywords only. So, c++ cannot code the friend keyword.
Amit shinde said:
3 years ago
#include<iostream>
using namespace std;
class abc
{
private:
int a=4;
public:
friend int main();
};
int main()
{
abc obj;
cout<<obj.a;
return 0;
}
This program runs and prints 4.
using namespace std;
class abc
{
private:
int a=4;
public:
friend int main();
};
int main()
{
abc obj;
cout<<obj.a;
return 0;
}
This program runs and prints 4.
Digambar Jadhav said:
7 years ago
@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.
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.
(1)
Harmandeep kaur said:
9 years ago
I thought the Friend function is used to access private members of any class.
Yogesh said:
10 years ago
Friend function is not a member function of class, friend function is declared inside the class but it is defined outside the class.
Parth khandelwal said:
10 years ago
Friend function is used to access private data members of one class to another.
(1)
Rohit patil said:
10 years ago
Wrong answer.
Friend is only used in class not in main.
Friend is only used in class not in main.
(1)
Sudhankar Sharma said:
10 years ago
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.
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.
Akhila sivan said:
10 years ago
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.
Normally, a function that is defined outside of a class cannot access such information. Friend function can't declare in main() function.
Himanshu budhalakoti said:
1 decade ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers