Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews C Plus Plus Language Interviews:C++ Access ControlC++ COM ActiveXC++ ConstructorsC++ ContainersC++ Exception HandlingC++ FriendC++ InheritanceC++ Inline FunctionC++ New And DeleteC++ Operator OverloadingC++ Pointers & FunctionsC++ ProgrammerC++ ReferencesC++ Static DataC++ SyntaxC++ TemplateC++ Type CheckingC++ Virtual Functions
Copyright © 2018. All Rights Reserved
C++ Friend Interview Question:
What are the C++ friend classes? Explain its uses with an example?
Submitted by: AdministratorA friend class and all its member functions have access to all the private members defined within other class.
#include <iostream>
using namespace std;
class Numbers
{
int a;
int b;
public:
Numbers(int i, int j)
{
a = i;
b = j;
}
friend class Average;
};
class Average
{
public:
int average(Numbers x);
};
int Average:: average(Numbers x)
{
return ((x.a + x.b)/2);
}
int main()
{
Numbers ob(23, 67);
Average avg;
cout<<"The average of the numbers is: "<<avg.average(ob);
return 0;
}
Note the friend class member function average is passed the object of Numbers class as parameter.
Submitted by:
#include <iostream>
using namespace std;
class Numbers
{
int a;
int b;
public:
Numbers(int i, int j)
{
a = i;
b = j;
}
friend class Average;
};
class Average
{
public:
int average(Numbers x);
};
int Average:: average(Numbers x)
{
return ((x.a + x.b)/2);
}
int main()
{
Numbers ob(23, 67);
Average avg;
cout<<"The average of the numbers is: "<<avg.average(ob);
return 0;
}
Note the friend class member function average is passed the object of Numbers class as parameter.
Submitted by:
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.