Prepare
Practice
Interview
Aptitude
Reasoning
English
GD
Placement papers
HR
Current affairs
Engineering
MCA
MBA
Online test
Login
Non-member function cannot have access to private data of class - Functions
Home
>>
Category
>>
C++ (MCQ) questions and answers
>>
Functions
Q. In any ways, Non-member function cannot have access to the private data of the class.
- Published on 19 Jul 15
a.
True
b.
False
ANSWER: False
Related Content
Constructor and Destructor (
10
)
Operator Overloading (
11
)
Inheritance (
16
)
Polymorphism and Abstract Classes (
12
)
C++ basic concepts (
39
)
Exception Handling (
22
)
File Handling (
11
)
Functions (
20
)
Memory Management (
6
)
Pointers (
3
)
Discussion
Nihal
-Posted on 08 Oct 15
If a function is defined as a friend function then, it can access the private and protected data of that class.
To declare a friend function, declare its prototype within the class, preceding it with the keyword friend.
Example:
class Add {
int a, b;
public:
friend int sum(Add x);
void setValue(int i, int j)
{
a = i;
b = j;
}
};
// Note: sum() function is not a member function of any class.
int sum(Add x)
{
/* Because sum() is a friend function of myclass, it can
directly access private data a and b. */
return x.a + x.b;
}
int main()
{
Add addObj;
addObj.setValue(10, 20);
cout <<"Summation =:"<< sum(addObj);
return 0;
}
➨
Post your comment / Share knowledge
Required!
Required!
Invalid Email Id!
Required!
Enter the code shown above:
Please enter the code shown above
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)
MCQs
English
Tutorials
Download
▲