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.
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.
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.
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.
Neha Rani said:
1 decade ago
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.
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.
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.
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.
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)
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers