Interviewer And Interviewee Guide

C++ Friend Interview Question:

What is output of this program?

#include <iostream>
using namespace std;
class sample
{
private:
int a, b;
public:
void test()
{
a = 100;
b = 200;
}
friend int compute(sample e1);
};
int compute(sample e1)
{
return int(e1.a + e1.b) - 5;
}
int main()
{
sample e;
e.test();
cout << compute(e);
return 0;
}
a) 100
b) 200
c) 300
d) 295

Submitted by: Administrator
d) 295
Explanation:
In this program, we are finding a value from the given function by using the friend for compute function.
Output:
$ g++ friend4.cpp
$ a.out
295
Submitted by:

Read Online C++ Friend Job Interview Questions And Answers
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.