Question
1.
#include
2.
using namespace std;
3.
class base
4.
{
5.
int val1, val2;
6.
public:
7.
int get()
8.
{
9.
val1 = 100;
10.
val2 = 300;
11.
}
12.
friend float mean(base ob);
13.
};
14.
float mean(base ob)
15.
{
16.
return float(ob.val1 + ob.val2) / 2;
17.
}
18.
int main()
19.
{
20.
base obj;
21.
obj.get();
22.
cout << mean(obj);
23.
return 0;
24.
}
What is the output of this program?
1.
#include
2.
using namespace std;
3.
class base
4.
{
5.
int val1, val2;
6.
public:
7.
int get()
8.
{
9.
val1 = 100;
10.
val2 = 300;
11.
}
12.
friend float mean(base ob);
13.
};
14.
float mean(base ob)
15.
{
16.
return float(ob.val1 + ob.val2) / 2;
17.
}
18.
int main()
19.
{
20.
base obj;
21.
obj.get();
22.
cout << mean(obj);
23.
return 0;
24.
}
Answer: Option A
Was this answer helpful ?
In this program, We are finding the mean value by declaring the function mean as a friend of class base.
Output:
$ g++ friend3.cpp
$ a.out
200
Was this answer helpful ?
More Questions on This Topic :
Question 4.
What is the syntax of friend function?
....
Submit Solution